Search in sources :

Example 1 with MongoDBBuilder

use of org.entcore.common.aggregation.filters.dbbuilders.MongoDBBuilder 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;
}
Also used : JsonArray(org.vertx.java.core.json.JsonArray) IndicatorGroup(org.entcore.common.aggregation.groups.IndicatorGroup) MongoDBBuilder(org.entcore.common.aggregation.filters.dbbuilders.MongoDBBuilder) ArrayList(java.util.ArrayList) JsonObject(org.vertx.java.core.json.JsonObject) Handler(org.vertx.java.core.Handler) IndicatorMongoImpl(org.entcore.common.aggregation.indicators.mongo.IndicatorMongoImpl) MongoUpdateBuilder(fr.wseduc.mongodb.MongoUpdateBuilder)

Example 2 with MongoDBBuilder

use of org.entcore.common.aggregation.filters.dbbuilders.MongoDBBuilder 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());
            }
        }
    });
}
Also used : Message(org.vertx.java.core.eventbus.Message) MongoDBBuilder(org.entcore.common.aggregation.filters.dbbuilders.MongoDBBuilder) JsonObject(org.vertx.java.core.json.JsonObject) Handler(org.vertx.java.core.Handler) Neo4jResult.validResultHandler(org.entcore.common.neo4j.Neo4jResult.validResultHandler) Date(java.util.Date) JsonArray(org.vertx.java.core.json.JsonArray) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Either(fr.wseduc.webutils.Either) MongoUpdateBuilder(fr.wseduc.mongodb.MongoUpdateBuilder)

Aggregations

MongoUpdateBuilder (fr.wseduc.mongodb.MongoUpdateBuilder)2 MongoDBBuilder (org.entcore.common.aggregation.filters.dbbuilders.MongoDBBuilder)2 Handler (org.vertx.java.core.Handler)2 JsonArray (org.vertx.java.core.json.JsonArray)2 JsonObject (org.vertx.java.core.json.JsonObject)2 Either (fr.wseduc.webutils.Either)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IndicatorGroup (org.entcore.common.aggregation.groups.IndicatorGroup)1 IndicatorMongoImpl (org.entcore.common.aggregation.indicators.mongo.IndicatorMongoImpl)1 Neo4jResult.validResultHandler (org.entcore.common.neo4j.Neo4jResult.validResultHandler)1 Message (org.vertx.java.core.eventbus.Message)1