10 changed files with 674 additions and 34 deletions
@ -0,0 +1,84 @@ |
|||||||
|
package org.springframework.datastore.document.analytics; |
||||||
|
|
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
public class MvcEvent { |
||||||
|
|
||||||
|
private String controller; |
||||||
|
|
||||||
|
private String action; |
||||||
|
|
||||||
|
private Parameters parameters; |
||||||
|
|
||||||
|
private Date date; |
||||||
|
|
||||||
|
private String requestUri; |
||||||
|
|
||||||
|
private String requestAddress; |
||||||
|
|
||||||
|
private String remoteUser; |
||||||
|
|
||||||
|
private String view; |
||||||
|
|
||||||
|
public String getController() { |
||||||
|
return controller; |
||||||
|
} |
||||||
|
|
||||||
|
public void setController(String controller) { |
||||||
|
this.controller = controller; |
||||||
|
} |
||||||
|
|
||||||
|
public String getAction() { |
||||||
|
return action; |
||||||
|
} |
||||||
|
|
||||||
|
public void setAction(String action) { |
||||||
|
this.action = action; |
||||||
|
} |
||||||
|
|
||||||
|
public Parameters getParameters() { |
||||||
|
return parameters; |
||||||
|
} |
||||||
|
|
||||||
|
public void setParameters(Parameters parameters) { |
||||||
|
this.parameters = parameters; |
||||||
|
} |
||||||
|
|
||||||
|
public Date getDate() { |
||||||
|
return date; |
||||||
|
} |
||||||
|
|
||||||
|
public void setDate(Date date) { |
||||||
|
this.date = date; |
||||||
|
} |
||||||
|
|
||||||
|
public String getRequestUri() { |
||||||
|
return requestUri; |
||||||
|
} |
||||||
|
|
||||||
|
public void setRequestUri(String requestUri) { |
||||||
|
this.requestUri = requestUri; |
||||||
|
} |
||||||
|
|
||||||
|
public String getRequestAddress() { |
||||||
|
return requestAddress; |
||||||
|
} |
||||||
|
|
||||||
|
public void setRequestAddress(String requestAddress) { |
||||||
|
this.requestAddress = requestAddress; |
||||||
|
} |
||||||
|
|
||||||
|
public String getRemoteUser() { |
||||||
|
return remoteUser; |
||||||
|
} |
||||||
|
|
||||||
|
public void setRemoteUser(String remoteUser) { |
||||||
|
this.remoteUser = remoteUser; |
||||||
|
} |
||||||
|
|
||||||
|
//TODO
|
||||||
|
//Map sessionAttributes
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
package org.springframework.datastore.document.analytics; |
||||||
|
|
||||||
|
public class Parameters { |
||||||
|
|
||||||
|
private String p1; |
||||||
|
|
||||||
|
private String p2; |
||||||
|
|
||||||
|
private String p3; |
||||||
|
|
||||||
|
public String getP1() { |
||||||
|
return p1; |
||||||
|
} |
||||||
|
|
||||||
|
public void setP1(String p1) { |
||||||
|
this.p1 = p1; |
||||||
|
} |
||||||
|
|
||||||
|
public String getP2() { |
||||||
|
return p2; |
||||||
|
} |
||||||
|
|
||||||
|
public void setP2(String p2) { |
||||||
|
this.p2 = p2; |
||||||
|
} |
||||||
|
|
||||||
|
public String getP3() { |
||||||
|
return p3; |
||||||
|
} |
||||||
|
|
||||||
|
public void setP3(String p3) { |
||||||
|
this.p3 = p3; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2010 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.datastore.document.mongodb; |
||||||
|
|
||||||
|
public class Person { |
||||||
|
|
||||||
|
private String firstName; |
||||||
|
|
||||||
|
private Person friend; |
||||||
|
|
||||||
|
|
||||||
|
public String getFirstName() { |
||||||
|
return firstName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setFirstName(String firstName) { |
||||||
|
this.firstName = firstName; |
||||||
|
} |
||||||
|
|
||||||
|
public Person getFriend() { |
||||||
|
return friend; |
||||||
|
} |
||||||
|
|
||||||
|
public void setFriend(Person friend) { |
||||||
|
this.friend = friend; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,51 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2010 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.datastore.document.mongodb; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
public class Portfolio { |
||||||
|
|
||||||
|
private String portfolioName; |
||||||
|
private User user; |
||||||
|
private List trades; |
||||||
|
|
||||||
|
public Portfolio() { |
||||||
|
trades = new ArrayList(); |
||||||
|
} |
||||||
|
public String getPortfolioName() { |
||||||
|
return portfolioName; |
||||||
|
} |
||||||
|
public void setPortfolioName(String portfolioName) { |
||||||
|
this.portfolioName = portfolioName; |
||||||
|
} |
||||||
|
public List getTrades() { |
||||||
|
return trades; |
||||||
|
} |
||||||
|
public void setTrades(List trades) { |
||||||
|
this.trades = trades; |
||||||
|
} |
||||||
|
public User getUser() { |
||||||
|
return user; |
||||||
|
} |
||||||
|
public void setUser(User user) { |
||||||
|
this.user = user; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,62 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2010 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.datastore.document.mongodb; |
||||||
|
|
||||||
|
public class Trade { |
||||||
|
|
||||||
|
private String ticker; |
||||||
|
|
||||||
|
private long quantity; |
||||||
|
|
||||||
|
private double price; |
||||||
|
|
||||||
|
private String orderType; |
||||||
|
|
||||||
|
public String getOrderType() { |
||||||
|
return orderType; |
||||||
|
} |
||||||
|
|
||||||
|
public void setOrderType(String orderType) { |
||||||
|
this.orderType = orderType; |
||||||
|
} |
||||||
|
|
||||||
|
public double getPrice() { |
||||||
|
return price; |
||||||
|
} |
||||||
|
|
||||||
|
public void setPrice(double price) { |
||||||
|
this.price = price; |
||||||
|
} |
||||||
|
|
||||||
|
public long getQuantity() { |
||||||
|
return quantity; |
||||||
|
} |
||||||
|
|
||||||
|
public void setQuantity(long quantity) { |
||||||
|
this.quantity = quantity; |
||||||
|
} |
||||||
|
|
||||||
|
public String getTicker() { |
||||||
|
return ticker; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTicker(String ticker) { |
||||||
|
this.ticker = ticker; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,74 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2010 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.datastore.document.mongodb; |
||||||
|
|
||||||
|
public class User { |
||||||
|
|
||||||
|
@Override |
||||||
|
public int hashCode() { |
||||||
|
final int prime = 31; |
||||||
|
int result = 1; |
||||||
|
result = prime * result |
||||||
|
+ ((accountName == null) ? 0 : accountName.hashCode()); |
||||||
|
result = prime * result |
||||||
|
+ ((userName == null) ? 0 : userName.hashCode()); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean equals(Object obj) { |
||||||
|
if (this == obj) |
||||||
|
return true; |
||||||
|
if (obj == null) |
||||||
|
return false; |
||||||
|
if (getClass() != obj.getClass()) |
||||||
|
return false; |
||||||
|
User other = (User) obj; |
||||||
|
if (accountName == null) { |
||||||
|
if (other.accountName != null) |
||||||
|
return false; |
||||||
|
} else if (!accountName.equals(other.accountName)) |
||||||
|
return false; |
||||||
|
if (userName == null) { |
||||||
|
if (other.userName != null) |
||||||
|
return false; |
||||||
|
} else if (!userName.equals(other.userName)) |
||||||
|
return false; |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private String accountName; |
||||||
|
|
||||||
|
private String userName; |
||||||
|
|
||||||
|
public String getAccountName() { |
||||||
|
return accountName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setAccountName(String accountName) { |
||||||
|
this.accountName = accountName; |
||||||
|
} |
||||||
|
|
||||||
|
public String getUserName() { |
||||||
|
return userName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setUserName(String userName) { |
||||||
|
this.userName = userName; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,148 @@ |
|||||||
|
package org.springframework.datastore.document.mongodb.analytics; |
||||||
|
|
||||||
|
import java.net.UnknownHostException; |
||||||
|
import java.util.Calendar; |
||||||
|
import java.util.Date; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.datastore.document.analytics.MvcEvent; |
||||||
|
import org.springframework.datastore.document.analytics.Parameters; |
||||||
|
import org.springframework.datastore.document.mongodb.MongoTemplate; |
||||||
|
import org.springframework.datastore.document.mongodb.query.Query; |
||||||
|
//import org.springframework.datastore.document.mongodb.query.QueryBuilder;
|
||||||
|
|
||||||
|
import com.mongodb.BasicDBList; |
||||||
|
import com.mongodb.BasicDBObject; |
||||||
|
import com.mongodb.DB; |
||||||
|
import com.mongodb.DBCollection; |
||||||
|
import com.mongodb.DBObject; |
||||||
|
import com.mongodb.Mongo; |
||||||
|
import com.mongodb.MongoException; |
||||||
|
import com.mongodb.QueryBuilder; |
||||||
|
|
||||||
|
public class MvcAnalyticsTests { |
||||||
|
|
||||||
|
private MongoTemplate mongoTemplate; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() throws Exception { |
||||||
|
Mongo m = new Mongo(); |
||||||
|
DB db = m.getDB("mvc"); |
||||||
|
mongoTemplate = new MongoTemplate(db); |
||||||
|
mongoTemplate.setDefaultCollectionName("mvc"); |
||||||
|
mongoTemplate.afterPropertiesSet(); |
||||||
|
//mongoTemplate.dropCollection("mvc");
|
||||||
|
//mongoTemplate.createCollection("mvc");
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void loadData() { |
||||||
|
|
||||||
|
//datasize, favoriteRestId
|
||||||
|
createAndStoreForP1(5, 1); |
||||||
|
createAndStoreForP1(6, 2); |
||||||
|
createAndStoreForP1(3, 3); |
||||||
|
createAndStoreForP1(8, 4); |
||||||
|
|
||||||
|
|
||||||
|
List<MvcEvent> mvcEvents = mongoTemplate.queryForCollection("mvc", MvcEvent.class); |
||||||
|
Assert.assertEquals(22, mvcEvents.size()); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
|
||||||
|
var start = new Date(2010,9,1); |
||||||
|
var end = new Date(2010,11,1); |
||||||
|
db.mvc.group( |
||||||
|
{ cond: {"action": "addFavoriteRestaurant", "date": {$gte: start, $lt: end}} |
||||||
|
, key: {"parameters.p1": true} |
||||||
|
, initial: {count: 0} |
||||||
|
, reduce: function(doc, out){ out.count++; } |
||||||
|
} ); |
||||||
|
|
||||||
|
*/ |
||||||
|
|
||||||
|
@Test |
||||||
|
public void listAll() { |
||||||
|
List<MvcEvent> mvcEvents = mongoTemplate.queryForCollection("mvc", MvcEvent.class); |
||||||
|
for (MvcEvent mvcEvent : mvcEvents) { |
||||||
|
System.out.println(mvcEvent.getDate()); |
||||||
|
} |
||||||
|
//System.out.println(mvcEvents);
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void groupQuery() { |
||||||
|
//This circumvents exception translation
|
||||||
|
DBCollection collection = mongoTemplate.getConnection().getCollection("mvc"); |
||||||
|
|
||||||
|
//QueryBuilder qb = new QueryBuilder();
|
||||||
|
//qb.start("date").greaterThan(object)
|
||||||
|
Calendar startDate = Calendar.getInstance(); |
||||||
|
startDate.clear(); |
||||||
|
startDate.set(Calendar.YEAR, 2010); |
||||||
|
startDate.set(Calendar.MONTH, 5); |
||||||
|
Calendar endDate = Calendar.getInstance(); |
||||||
|
endDate.clear(); |
||||||
|
endDate.set(Calendar.YEAR, 2010); |
||||||
|
endDate.set(Calendar.MONTH, 12); |
||||||
|
|
||||||
|
/* |
||||||
|
QueryBuilder qb = new QueryBuilder(); |
||||||
|
Query q = qb.find("date").gte(startDate.getTime()).lt(endDate.getTime()).and("action").is("addFavoriteRestaurant").build(); |
||||||
|
DBObject cond2 = q.getQueryObject(); |
||||||
|
*/ |
||||||
|
DBObject cond = QueryBuilder.start("date").greaterThanEquals(startDate.getTime()).lessThan(endDate.getTime()).and("action").is("addFavoriteRestaurant").get(); |
||||||
|
DBObject key = new BasicDBObject("parameters.p1", true); |
||||||
|
/* |
||||||
|
DBObject dateQ = new BasicDBObject(); |
||||||
|
dateQ.put("$gte", startDate.getTime()); |
||||||
|
dateQ.put("$lt", endDate.getTime()); |
||||||
|
DBObject cond = new BasicDBObject(); |
||||||
|
cond.put("action", "addFavoriteRestaurant"); |
||||||
|
cond.put("date", dateQ);*/ |
||||||
|
|
||||||
|
DBObject intitial = new BasicDBObject("count", 0); |
||||||
|
DBObject result = collection.group(key, cond, intitial, "function(doc, out){ out.count++; }"); |
||||||
|
if (result instanceof BasicDBList) { |
||||||
|
BasicDBList dbList = (BasicDBList) result; |
||||||
|
for (Iterator iterator = dbList.iterator(); iterator.hasNext();) { |
||||||
|
DBObject dbo = (DBObject) iterator.next(); |
||||||
|
System.out.println(dbo); |
||||||
|
} |
||||||
|
} |
||||||
|
Map resultMap = result.toMap(); |
||||||
|
System.out.println(result); |
||||||
|
} |
||||||
|
|
||||||
|
private void createAndStoreForP1(int dataSize, int p1) { |
||||||
|
for (int i = 0; i < dataSize; i++) { |
||||||
|
MvcEvent event = generateEvent(p1); |
||||||
|
mongoTemplate.save(event); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private MvcEvent generateEvent(Integer p1) { |
||||||
|
MvcEvent event = new MvcEvent(); |
||||||
|
|
||||||
|
event.setController("RestaurantController"); |
||||||
|
event.setAction("addFavoriteRestaurant"); |
||||||
|
event.setDate(new Date()); |
||||||
|
event.setRemoteUser("mpollack"); |
||||||
|
event.setRequestAddress("127.0.0.1"); |
||||||
|
event.setRequestUri("/myrestaurants-analytics/restaurants"); |
||||||
|
Parameters params = new Parameters(); |
||||||
|
params.setP1(p1.toString()); |
||||||
|
params.setP2("2"); |
||||||
|
event.setParameters(params); |
||||||
|
|
||||||
|
return event; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
Loading…
Reference in new issue