use of fr.wseduc.mongodb.MongoUpdateBuilder in project statistics by OPEN-ENT-NG.
the class IndicatorFactory method getUniqueVisitorsIndicator.
// Unique visitors
public static Indicator getUniqueVisitorsIndicator(Collection<IndicatorFilterMongoImpl> filters, Date pWriteDate) {
Collection<IndicatorGroup> indicatorGroups = new ArrayList<IndicatorGroup>();
final IndicatorGroup profileIg = new IndicatorGroup(TRACE_FIELD_STRUCTURES).setArray(true).addAndReturnChild(TRACE_FIELD_PROFILE);
indicatorGroups.add(profileIg);
IndicatorMongoImpl indicator = new IndicatorMongoImpl(TRACE_TYPE_CONNEXION, filters, indicatorGroups) {
@Override
protected void customizePipeline(JsonArray pipeline) {
// Remove "count" from stage "$group" in pipeline, and add "userId" to field _id
for (int i = 0; i < pipeline.size(); i++) {
JsonObject stage = pipeline.get(i);
JsonObject group = stage.getObject("$group", null);
if (group != null) {
group.removeField("count");
JsonObject id = group.getObject("_id", null);
if (id != null) {
id.putString(TRACE_FIELD_USER, "$" + TRACE_FIELD_USER);
}
break;
}
}
// Add another "$group" stage in pipeline, to count unique visitors
JsonObject groupBy = new JsonObject().putObject("$group", new JsonObject().putObject("_id", getGroupByObject(new JsonObject(), profileIg)).putObject("count", new JsonObject().putNumber("$sum", 1)));
pipeline.addObject(groupBy);
}
@Override
protected // Set the indicator's value (instead of incrementing it)
void writeAction(MongoDBBuilder criteriaQuery, int resultsCount, Handler<Message<JsonObject>> handler) {
mongo.update(COLLECTIONS.stats.name(), MongoQueryBuilder.build(criteriaQuery), new MongoUpdateBuilder().set(this.getWriteKey(), resultsCount).build(), true, true, handler);
}
};
indicator.setWriteKey(STATS_FIELD_UNIQUE_VISITORS);
indicator.setWriteDate(pWriteDate);
return indicator;
}
use of fr.wseduc.mongodb.MongoUpdateBuilder in project statistics by OPEN-ENT-NG.
the class IndicatorCustomImpl method aggregate.
@Override
public void aggregate(final Handler<JsonObject> callBack) {
final Date start = new Date();
this.getAccounts(new Handler<Either<String, JsonArray>>() {
@Override
public void handle(Either<String, JsonArray> event) {
if (event.isLeft()) {
log.error(event.left().getValue());
callBack.handle(new JsonObject());
return;
}
try {
JsonArray results = event.right().getValue();
// If no documents found, write nothing
if (results.size() == 0) {
callBack.handle(new JsonObject());
return;
}
// Synchronization handler
final AtomicInteger countDown = new AtomicInteger(results.size());
Handler<Message<JsonObject>> synchroHandler = new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
if (!"ok".equals(message.body().getString("status"))) {
log.error("Error in method aggregate of IndicatorCustomImpl : " + message.body().toString());
}
if (countDown.decrementAndGet() == 0) {
final Date end = new Date();
log.info("[Aggregation]{" + IndicatorCustomImpl.this.getKey() + "} Took [" + (end.getTime() - start.getTime()) + "] ms");
callBack.handle(new JsonObject().putString("status", "ok"));
}
}
};
for (int i = 0; i < results.size(); i++) {
JsonObject jo = results.get(i);
String structure = jo.getString("structure");
String profile = jo.getString("profile");
Number accounts = jo.getNumber("accounts");
Number activatedAccounts = jo.getNumber("activatedAccounts");
String date = MongoDb.formatDate(IndicatorCustomImpl.this.getWriteDate());
MongoDBBuilder criteriaQuery = new MongoDBBuilder();
criteriaQuery.put(STATS_FIELD_DATE).is(date).put(STATS_FIELD_GROUPBY).is(TRACE_FIELD_STRUCTURES + "/" + TRACE_FIELD_PROFILE).put(PROFILE_ID).is(profile).put(STRUCTURES_ID).is(structure);
MongoUpdateBuilder update = new MongoUpdateBuilder().set(STATS_FIELD_ACCOUNTS, accounts).set(STATS_FIELD_ACTIVATED_ACCOUNTS, activatedAccounts);
// Upsert data in MongoDB
mongo.update(COLLECTIONS.stats.name(), MongoQueryBuilder.build(criteriaQuery), update.build(), true, true, synchroHandler);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
callBack.handle(new JsonObject());
}
}
});
}
Aggregations