Skip to content

Commit

Permalink
Refactorized query: getOwnerActionCount
Browse files Browse the repository at this point in the history
Some fixes
  • Loading branch information
mrmx committed Jan 31, 2011
1 parent 6945d2a commit b342bd6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/mongoste/core/StatsEngine.java
Expand Up @@ -41,7 +41,7 @@ public interface StatsEngine {
public List<StatAction> getActions(Query query) throws StatsEngineException;
public List<StatCounter> getTopTargets(Query query) throws StatsEngineException;
public Map<String,Long> getMultiTargetActionCount(Query query) throws StatsEngineException;
public Map<String,Long> getOwnerActionCount(String clientId,String targetType,String owner,String... tags) throws StatsEngineException;
public Map<String,Long> getOwnerActionCount(Query query) throws StatsEngineException;

public void setTargetOwners(String clientId,String targetType,String target,List<String> owners) throws StatsEngineException;
public void setTargetOwners(String clientId,String targetType,List<String> targets,List<String> owners) throws StatsEngineException;
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/org/mongoste/core/impl/mongodb/MongoStatsEngine.java
Expand Up @@ -289,17 +289,17 @@ EVENT_TARGET, getQueryValue(query,QueryField.TARGET)//new BasicDBObject("$in
}

@Override
public Map<String,Long> getOwnerActionCount(String clientId,String targetType,String owner,String... tags) throws StatsEngineException {
log.info("getOwnerActionCount for client: {} target type: {} owner: {}",new Object[]{clientId,targetType,owner});
DBObject query = MongoUtil.createDoc(
EVENT_CLIENT_ID,clientId,
EVENT_TARGET_TYPE,targetType,
EVENT_TARGET_OWNERS,owner
public Map<String,Long> getOwnerActionCount(Query query) throws StatsEngineException {
DBObject queryDoc = MongoUtil.createDoc(
EVENT_CLIENT_ID , getQueryValue(query,QueryField.CLIENT_ID),
EVENT_TARGET_TYPE,getQueryValue(query,QueryField.TARGET_TYPE),
EVENT_TARGET_OWNERS,getQueryValue(query,QueryField.TARGET_OWNER)
);
Object tags = getQueryValue(query,QueryField.TARGET_TAGS);
if(tags != null){
putSingleInDoc(query, EVENT_TARGET_TAGS, Arrays.asList(tags));
queryDoc.put(EVENT_TARGET_TAGS, tags);
}
return getActionCount(query);
return getActionCount(queryDoc);
}

/*
Expand Down Expand Up @@ -691,14 +691,16 @@ protected int getQueryOrder(Query query) {
protected Object getQueryValue(Query query,QueryField field) {
Object value = null;
QueryFilter filter = query.getFilter(field);
switch(filter.getOperation()) {
case IN:
value = new BasicDBObject("$in",filter.getValue());
break;
case EQ:
default:
value = filter.isEmpty() ? "" : filter.getValue();
break;
if(filter != null) {
switch(filter.getOperation()) {
case IN:
value = new BasicDBObject("$in",filter.getValue());
break;
case EQ:
default:
value = filter.isEmpty() ? "" : filter.getValue();
break;
}
}
return value;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/mongoste/query/DefaultQuery.java
Expand Up @@ -110,6 +110,7 @@ public Query filterBy(QueryField field,Object value) {
* @param value Value to apply
* @return
*/
@Override
public Query filterBy(QueryField field,QueryOp operation,Object value) {
return filterBy(field,new QueryFilter(operation,value));
}
Expand Down Expand Up @@ -155,6 +156,7 @@ public List<StatAction> getActions() throws StatsEngineException {
* @throws StatsEngineException
* @see StatCounter
*/
@Override
public List<StatCounter> getTopTargets() throws StatsEngineException {
assertNotEmpty(CLIENT_ID,TARGET_TYPE,ACTION);
log.debug("getTopTargets query {}",this);
Expand All @@ -167,12 +169,25 @@ public List<StatCounter> getTopTargets() throws StatsEngineException {
* @return a map of action->count
* @throws StatsEngineException
*/
@Override
public Map<String,Long> getMultiTargetActionCount() throws StatsEngineException {
assertNotEmpty(CLIENT_ID,TARGET_TYPE,TARGET);
log.debug("getMultiTargetActionCount query {}",this);
return statsEngine.getMultiTargetActionCount(this);
}

/**
* Returns the action counters of given target owner/s
* @return a map of action->count
* @throws StatsEngineException
*/
@Override
public Map<String,Long> getOwnerActionCount() throws StatsEngineException {
assertNotEmpty(CLIENT_ID,TARGET_TYPE,TARGET_OWNER);
log.debug("getOwnerActionCount query {}",this);
return statsEngine.getOwnerActionCount(this);
}


/**
* Checks if the provided fields has non-empty filters associated
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/mongoste/query/Query.java
Expand Up @@ -110,4 +110,11 @@ public interface Query extends Serializable {
*/
Map<String, Long> getMultiTargetActionCount() throws StatsEngineException;

/**
* Returns the action counters of given target owner/s
* @return a map of action->count
* @throws StatsEngineException
*/
Map<String, Long> getOwnerActionCount() throws StatsEngineException;

}
4 changes: 3 additions & 1 deletion src/main/java/org/mongoste/query/QueryField.java
Expand Up @@ -16,7 +16,7 @@
package org.mongoste.query;

/**
* Available query fields enumeration
* Available query fields
*
* @author mrmx
*/
Expand All @@ -25,5 +25,7 @@ public enum QueryField {
ACTION,
TARGET,
TARGET_TYPE,
TARGET_OWNER,
TARGET_TAGS,
DATE_FROM,DATE_TO
}
Expand Up @@ -95,7 +95,7 @@ public Map<String, Long> getMultiTargetActionCount(Query query) throws StatsEngi
}

@Override
public Map<String, Long> getOwnerActionCount(String clientId, String targetType, String owner, String... tags) throws StatsEngineException {
public Map<String, Long> getOwnerActionCount(Query query) throws StatsEngineException {
throw new UnsupportedOperationException("Not supported yet.");
}

Expand Down
Expand Up @@ -700,32 +700,32 @@ public void testGetOwnerActionCount() throws Exception {
engine.handleEvent(event);
String owner = event.getTargetOwners().get(0);
System.out.println("Search for owner:"+owner);
Map result = engine.getOwnerActionCount(event.getClientId(), event.getTargetType(), owner);
Query query = engine.createQuery();
query.filterBy(QueryField.CLIENT_ID, event.getClientId());
query.filterBy(QueryField.TARGET_TYPE, event.getTargetType());
query.filterBy(QueryField.TARGET_OWNER, event.getTargetOwners());
Map result = query.getOwnerActionCount();
assertNotNull(result);
assertEquals(1,result.size());

//No result
result = engine.getOwnerActionCount(
event.getClientId(), event.getTargetType(),
"unknown-owner"
);
query.filterBy(QueryField.TARGET_OWNER, "unknown-owner");
result = query.getOwnerActionCount();
assertNotNull(result);
assertEquals(0,result.size());
System.out.println("result:"+result);

//Search with tags
result = engine.getOwnerActionCount(
event.getClientId(),
event.getTargetType(), owner,
event.getTargetTags().toArray(new String[]{})
);
query.filterBy(QueryField.TARGET_OWNER, event.getTargetOwners());
query.filterBy(QueryField.TARGET_TAGS, event.getTargetTags());
result = query.getOwnerActionCount();
assertNotNull(result);
assertEquals(1,result.size());
System.out.println("result:"+result);

//Empty result search with unknown tag
result = engine.getOwnerActionCount(
event.getClientId(),
event.getTargetType(), owner,
"unknown-tag"
);
query.filterBy(QueryField.TARGET_TAGS, "unknown-tag");
result = query.getOwnerActionCount();
assertNotNull(result);
assertEquals(0,result.size());
System.out.println("result:"+result);
Expand Down

0 comments on commit b342bd6

Please sign in to comment.