Search in sources :

Example 11 with JsonObject

use of org.vertx.java.core.json.JsonObject in project statistics by OPEN-ENT-NG.

the class AggregateTask method handle.

@Override
public void handle(Long event) {
    log.info("Execute AggregateTask.");
    AggregationProcessing aggProcessing = new AggregationProcessingSequentialImpl();
    IndicatorHelper.addIndicators(from, to, aggProcessing);
    Handler<JsonObject> handler = (this.handler != null) ? this.handler : new Handler<JsonObject>() {

        @Override
        public void handle(JsonObject event) {
            try {
                if (!"ok".equals(event.getString("status", null))) {
                    log.error("Error in AggregateTask : status is different from ok." + event.toString());
                }
            } catch (Exception e) {
                log.error("Error in AggregateTask when checking status", e);
            }
        }
    };
    aggProcessing.process(handler);
}
Also used : AggregationProcessing(org.entcore.common.aggregation.processing.AggregationProcessing) JsonObject(org.vertx.java.core.json.JsonObject)

Example 12 with JsonObject

use of org.vertx.java.core.json.JsonObject 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)

Example 13 with JsonObject

use of org.vertx.java.core.json.JsonObject in project statistics by OPEN-ENT-NG.

the class StatisticsController method generation.

@Get("/generation")
@SecuredAction(value = "", type = ActionType.RESOURCE)
@ResourceFilter(SuperAdminFilter.class)
public /**
 * Generation of statistics to mongoDB database. Calls the same treatment as the one called by cron
 * Before the generation, deletes the records who will be regenerated.
 */
void generation(final HttpServerRequest request) {
    RequestUtils.bodyToJson(request, pathPrefix + "schoolquery", new Handler<JsonObject>() {

        @Override
        public void handle(JsonObject data) {
            // parameters from the model (schoolIdArray / indicator / startDate / endDate / module)
            final JsonObject params = data;
            UserUtils.getUserInfos(eb, request, new Handler<UserInfos>() {

                @Override
                public void handle(final UserInfos user) {
                    final Date startDate = new Date(params.getLong("startDate") * 1000L);
                    final Date endDate = new Date(params.getLong("endDate") * 1000L);
                    // final Date startDate = new Date(Long.parseLong(request.params().get(PARAM_START_DATE)) * 1000L);
                    // final Date endDate = new Date(Long.parseLong(request.params().get(PARAM_END_DATE)) * 1000L);
                    deleteRegeneratedStatistics(startDate, endDate, new Handler<Either<String, JsonArray>>() {

                        @Override
                        public void handle(Either<String, JsonArray> event) {
                            if (event.isLeft()) {
                                log.error(event.left().getValue());
                                renderError(request);
                            } else {
                                if (user != null) {
                                    Statistics st = new Statistics();
                                    st.aggregateEvents(StatisticsController.this.vertx, startDate, endDate);
                                    JsonArray jarray = new JsonArray();
                                    jarray.add(String.valueOf(params.getInteger("startDate")));
                                    renderJson(request, jarray);
                                }
                            }
                        }
                    });
                }
            });
        }
    });
}
Also used : JsonArray(org.vertx.java.core.json.JsonArray) JsonObject(org.vertx.java.core.json.JsonObject) Handler(org.vertx.java.core.Handler) Either(fr.wseduc.webutils.Either) UserInfos(org.entcore.common.user.UserInfos) Statistics(net.atos.entng.statistics.Statistics) ResourceFilter(org.entcore.common.http.filter.ResourceFilter) SecuredAction(fr.wseduc.security.SecuredAction) Get(fr.wseduc.rs.Get)

Example 14 with JsonObject

use of org.vertx.java.core.json.JsonObject in project statistics by OPEN-ENT-NG.

the class StructureServiceNeo4jImpl method getAttachedStructureslist.

public void getAttachedStructureslist(String structureId, Handler<Either<String, JsonArray>> handler) {
    String query = "MATCH (s:Structure)<-[:HAS_ATTACHMENT*0..]-(s2:Structure) where s.id = {structureId} RETURN s2.id";
    JsonObject params = new JsonObject().putString("structureId", structureId);
    neo4j.execute(query, params, validResultHandler(handler));
}
Also used : JsonObject(org.vertx.java.core.json.JsonObject)

Example 15 with JsonObject

use of org.vertx.java.core.json.JsonObject in project statistics by OPEN-ENT-NG.

the class AggregationProcessingSequentialImpl method process.

@Override
public void process(final Handler<JsonObject> callBack) {
    if (indicators == null && indicators.isEmpty()) {
        log.warn("indicators is empty. Nothing was processed");
        return;
    }
    final Iterator<Indicator> it = indicators.iterator();
    Indicator indicator = it.next();
    // Agregate indicators one after the other
    indicator.aggregate(new Handler<JsonObject>() {

        @Override
        public void handle(JsonObject event) {
            if (it.hasNext()) {
                it.next().aggregate(this);
            } else {
                callBack.handle(event);
            }
        }
    });
}
Also used : JsonObject(org.vertx.java.core.json.JsonObject) Indicator(org.entcore.common.aggregation.indicators.Indicator)

Aggregations

JsonObject (org.vertx.java.core.json.JsonObject)15 JsonArray (org.vertx.java.core.json.JsonArray)8 Handler (org.vertx.java.core.Handler)6 Either (fr.wseduc.webutils.Either)4 SecuredAction (fr.wseduc.security.SecuredAction)3 UserInfos (org.entcore.common.user.UserInfos)3 MongoUpdateBuilder (fr.wseduc.mongodb.MongoUpdateBuilder)2 Post (fr.wseduc.rs.Post)2 ArrayList (java.util.ArrayList)2 MongoDBBuilder (org.entcore.common.aggregation.filters.dbbuilders.MongoDBBuilder)2 IndicatorGroup (org.entcore.common.aggregation.groups.IndicatorGroup)2 IndicatorMongoImpl (org.entcore.common.aggregation.indicators.mongo.IndicatorMongoImpl)2 Message (org.vertx.java.core.eventbus.Message)2 BasicDBList (com.mongodb.BasicDBList)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 QueryBuilder (com.mongodb.QueryBuilder)1 MongoDb (fr.wseduc.mongodb.MongoDb)1 MongoQueryBuilder (fr.wseduc.mongodb.MongoQueryBuilder)1 ApiDoc (fr.wseduc.rs.ApiDoc)1