Browse Source
Removed dependency on spring mvc in data-document-core to spring-data-document-examples\mongodb-myrestaurants-analyticspull/1/head
5 changed files with 0 additions and 411 deletions
@ -1,43 +0,0 @@
@@ -1,43 +0,0 @@
|
||||
package org.springframework.data.document.analytics; |
||||
|
||||
import java.util.Map; |
||||
|
||||
public class ControllerCounter { |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public double getCount() { |
||||
return count; |
||||
} |
||||
|
||||
public void setCount(double count) { |
||||
this.count = count; |
||||
} |
||||
|
||||
public Map<String, Double> getMethods() { |
||||
return methods; |
||||
} |
||||
|
||||
|
||||
public void setMethods(Map<String, Double> methods) { |
||||
this.methods = methods; |
||||
} |
||||
|
||||
private String name; |
||||
|
||||
private double count; |
||||
|
||||
private Map<String, Double> methods; |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "ControllerCounter [name=" + name + ", count=" + count |
||||
+ ", methods=" + methods + "]"; |
||||
} |
||||
} |
||||
@ -1,83 +0,0 @@
@@ -1,83 +0,0 @@
|
||||
package org.springframework.data.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
|
||||
|
||||
|
||||
} |
||||
@ -1,35 +0,0 @@
@@ -1,35 +0,0 @@
|
||||
package org.springframework.data.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; |
||||
} |
||||
|
||||
} |
||||
@ -1,246 +0,0 @@
@@ -1,246 +0,0 @@
|
||||
package org.springframework.data.document.mongodb.analytics; |
||||
|
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
import com.mongodb.BasicDBList; |
||||
import com.mongodb.BasicDBObject; |
||||
import com.mongodb.DBCollection; |
||||
import com.mongodb.DBObject; |
||||
import com.mongodb.Mongo; |
||||
import com.mongodb.QueryBuilder; |
||||
import com.mongodb.WriteResult; |
||||
import org.bson.types.ObjectId; |
||||
import org.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.data.document.analytics.ControllerCounter; |
||||
import org.springframework.data.document.analytics.MvcEvent; |
||||
import org.springframework.data.document.analytics.Parameters; |
||||
import org.springframework.data.document.mongodb.MongoTemplate; |
||||
import org.springframework.data.document.mongodb.convert.AbstractMongoConverter; |
||||
import org.springframework.data.document.mongodb.mapping.MongoPersistentEntity; |
||||
import org.springframework.data.document.mongodb.mapping.MongoPersistentProperty; |
||||
import org.springframework.data.document.mongodb.query.BasicQuery; |
||||
import org.springframework.data.mapping.model.MappingContext; |
||||
|
||||
public class MvcAnalyticsTests { |
||||
|
||||
private MongoTemplate mongoTemplate; |
||||
private MongoTemplate mongoDummyTemplate; |
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
Mongo m = new Mongo(); |
||||
mongoTemplate = new MongoTemplate(m, "mvc"); |
||||
mongoDummyTemplate = new MongoTemplate(mongoTemplate.getDbFactory(), new AbstractMongoConverter() { |
||||
public void write(Object t, DBObject dbo) { |
||||
} |
||||
|
||||
public <S> S read(Class<S> clazz, DBObject dbo) { |
||||
return null; |
||||
} |
||||
|
||||
public <T> T convertObjectId(ObjectId id, Class<T> targetType) { |
||||
return null; |
||||
} |
||||
|
||||
public ObjectId convertObjectId(Object id) { |
||||
return null; |
||||
} |
||||
|
||||
public MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> getMappingContext() { |
||||
return null; |
||||
} |
||||
|
||||
}); |
||||
} |
||||
|
||||
@Test |
||||
public void clean() { |
||||
mongoTemplate.dropCollection("mvc"); |
||||
mongoTemplate.createCollection("mvc"); |
||||
mongoTemplate.dropCollection("counters"); |
||||
mongoTemplate.createCollection("counters"); |
||||
} |
||||
|
||||
@Test |
||||
public void loadMvcEventData() { |
||||
|
||||
mongoTemplate.dropCollection("mvc"); |
||||
mongoTemplate.createCollection("mvc"); |
||||
// datasize, favoriteRestId
|
||||
createAndStoreMvcEvent(5, 1); |
||||
createAndStoreMvcEvent(6, 2); |
||||
createAndStoreMvcEvent(3, 3); |
||||
createAndStoreMvcEvent(8, 4); |
||||
|
||||
List<MvcEvent> mvcEvents = mongoTemplate.getCollection("mvc", MvcEvent.class); |
||||
Assert.assertEquals(22, mvcEvents.size()); |
||||
|
||||
mongoDummyTemplate.getCollection("mvc", MvcEvent.class); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void loadCounterData() { |
||||
for (int i = 0; i < 10; i++) { |
||||
storeCounterData("SignUpController", "createForm"); |
||||
storeCounterData("SignUpController", "create"); |
||||
storeCounterData("SignUpController", "show"); |
||||
storeCounterData("RestaurantController", "addFavoriteRestaurant"); |
||||
} |
||||
for (int i = 0; i < 5; i++) { |
||||
storeCounterData("RestaurantController", "list"); |
||||
storeCounterData("SignUpController", "show"); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void queryCounterData() { |
||||
DBObject query = QueryBuilder.start("name").is("SignUpController").get(); |
||||
for (DBObject dbo : mongoTemplate.getCollection("counters").find(query)) { |
||||
System.out.println(dbo); |
||||
} |
||||
List<ControllerCounter> counters = mongoTemplate.find("counters", |
||||
new BasicQuery("{ 'name' : 'SignUpController'} "), ControllerCounter.class); |
||||
for (ControllerCounter controllerCounter : counters) { |
||||
System.out.println(controllerCounter); |
||||
} |
||||
} |
||||
|
||||
/* |
||||
* |
||||
* 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 listAllMvcEvents() { |
||||
List<MvcEvent> mvcEvents = mongoTemplate.getCollection("mvc", MvcEvent.class); |
||||
for (MvcEvent mvcEvent : mvcEvents) { |
||||
System.out.println(mvcEvent.getDate()); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void groupQuery() { |
||||
// This circumvents exception translation
|
||||
DBCollection collection = mongoTemplate.getDb().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 (Object element : dbList) { |
||||
DBObject dbo = (DBObject) element; |
||||
System.out.println(dbo); |
||||
} |
||||
} |
||||
System.out.println(result); |
||||
} |
||||
|
||||
@Test |
||||
public void storeControllerCounterInfo() { |
||||
|
||||
BasicDBObject query = new BasicDBObject("name", "controller1"); |
||||
|
||||
BasicDBObject changes = new BasicDBObject(); |
||||
changes.put("$set", new BasicDBObject("name", "controller1")); |
||||
changes.put("$inc", new BasicDBObject("count", 1)); |
||||
|
||||
// mongoTemplate.upsertAndModify(dbo("key","value"), inc("count",1));
|
||||
// dbo(set("name","controller"), inc("count", 1));
|
||||
|
||||
// /mongoTemplate.update(collection("counters")
|
||||
|
||||
WriteResult r = mongoTemplate.getCollection("counters").update(query, changes, true, false); |
||||
// { "err" : "Modifiers and non-modifiers cannot be mixed" , "code" : 10154 , "n" : 0 , "ok" : 1.0}
|
||||
// { "err" : null , "updatedExisting" : false , "upserted" : { "$oid" : "4cba814a5a4900000000495d"} , "n" : 1 , "ok"
|
||||
// : 1.0}
|
||||
// { "err" : null , "updatedExisting" : true , "n" : 1 , "ok" : 1.0}
|
||||
System.out.println(r); |
||||
|
||||
// changes = new BasicDBObject("methods", new BasicDBObject("find", 1));
|
||||
// mongoTemplate.getCollection("counters").update(query, changes, true,
|
||||
// false);
|
||||
} |
||||
|
||||
@Test |
||||
public void updateMethodCounter() { |
||||
DBObject query = new BasicDBObject("name", "controller1"); |
||||
DBObject changes = new BasicDBObject("$inc", new BasicDBObject("methods.find", 1)); |
||||
mongoTemplate.getDb().getCollection("counters").update(query, changes, true, false); |
||||
} |
||||
|
||||
public void storeCounterData(String controllerName, String methodName) { |
||||
BasicDBObject query = new BasicDBObject("name", controllerName); |
||||
|
||||
BasicDBObject changes = new BasicDBObject(); |
||||
changes.put("$set", new BasicDBObject("name", controllerName)); |
||||
changes.put("$inc", new BasicDBObject("count", 1)); |
||||
|
||||
WriteResult r = mongoTemplate.getCollection("counters").update(query, changes, true, false); |
||||
System.out.println(r); |
||||
|
||||
changes = new BasicDBObject("$inc", new BasicDBObject("methods." + methodName, 1)); |
||||
r = mongoTemplate.getDb().getCollection("counters").update(query, changes, true, false); |
||||
System.out.println(r); |
||||
} |
||||
|
||||
private void createAndStoreMvcEvent(int dataSize, int p1) { |
||||
for (int i = 0; i < dataSize; i++) { |
||||
MvcEvent event = generateEvent(p1); |
||||
mongoTemplate.save("mvc", 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