Search in sources :

Example 6 with JsonObject

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

the class StatisticsController method getStructures.

@Post("/structures")
@ApiDoc("Get structures' names, UAIs and cities")
@SecuredAction("statistics.get.structures")
public void getStructures(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) {
                    if (user != null) {
                        JsonArray arr = params.getArray("schoolIdArray");
                        List<String> schoolIds = new ArrayList<String>();
                        for (int i = 0; i < arr.size(); i++) {
                            JsonObject obj = arr.get(i);
                            schoolIds.add((String) obj.getString("id"));
                        }
                        if (schoolIds == null || schoolIds.size() == 0) {
                            String errorMsg = i18n.translate("statistics.bad.request.invalid.schools", getHost(request), acceptLanguage(request));
                            badRequest(request, errorMsg);
                            return;
                        }
                        JsonArray structureIds = new JsonArray();
                        for (String school : schoolIds) {
                            structureIds.addString(school);
                        }
                        structureService.list(structureIds, new Handler<Either<String, JsonArray>>() {

                            @Override
                            public void handle(Either<String, JsonArray> event) {
                                if (event.isLeft()) {
                                    log.error(event.left().getValue());
                                    renderError(request);
                                } else {
                                    renderJson(request, event.right().getValue());
                                }
                            }
                        });
                    }
                // end if
                }
            });
        }
    });
}
Also used : JsonObject(org.vertx.java.core.json.JsonObject) Handler(org.vertx.java.core.Handler) UserInfos(org.entcore.common.user.UserInfos) JsonArray(org.vertx.java.core.json.JsonArray) Either(fr.wseduc.webutils.Either) SecuredAction(fr.wseduc.security.SecuredAction) Post(fr.wseduc.rs.Post) ApiDoc(fr.wseduc.rs.ApiDoc)

Example 7 with JsonObject

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

the class StatisticsServiceMongoImpl method getStatistics.

private void getStatistics(final List<String> schoolIds, final JsonObject params, final Handler<Either<String, JsonArray>> handler, boolean isExport) {
    if (schoolIds == null || schoolIds.isEmpty()) {
        throw new IllegalArgumentException("schoolIds is null or empty");
    }
    String indicator = params.getString(PARAM_INDICATOR);
    Long start = (Long) params.getNumber(PARAM_START_DATE);
    Long end = (Long) params.getNumber(PARAM_END_DATE);
    boolean isActivatedAccountsIndicator = STATS_FIELD_ACTIVATED_ACCOUNTS.equals(indicator);
    boolean isAccessIndicator = TRACE_TYPE_SVC_ACCESS.equals(indicator);
    String groupedBy = isAccessIndicator ? "module/structures/profil" : "structures/profil";
    final QueryBuilder criteriaQuery = QueryBuilder.start(STATS_FIELD_GROUPBY).is(groupedBy).and(STATS_FIELD_DATE).greaterThanEquals(formatTimestamp(start)).lessThan(formatTimestamp(end)).and(indicator).exists(true);
    String module = params.getString(PARAM_MODULE);
    boolean moduleIsEmpty = module == null || module.trim().isEmpty();
    boolean isAccessAllModules = isAccessIndicator && moduleIsEmpty;
    if (isAccessIndicator && !moduleIsEmpty) {
        criteriaQuery.and(MODULE_ID).is(module);
    }
    if (schoolIds.size() > 1) {
        criteriaQuery.and(STRUCTURES_ID).in(schoolIds);
    } else {
        criteriaQuery.and(STRUCTURES_ID).is(schoolIds.get(0));
        // When getting data for only one module, a "find" is enough (no need to aggregate data)
        if (!isExport && !isAccessAllModules) {
            JsonObject projection = new JsonObject();
            projection.putNumber("_id", 0).putNumber(indicator, 1).putNumber(PROFILE_ID, 1).putNumber(STATS_FIELD_DATE, 1);
            if (isActivatedAccountsIndicator) {
                projection.putNumber(STATS_FIELD_ACCOUNTS, 1);
            }
            mongo.find(collection, MongoQueryBuilder.build(criteriaQuery), sortByDateProfile, projection, MongoDbResult.validResultsHandler(handler));
            return;
        }
    }
    // Aggregate data
    final JsonObject aggregation = new JsonObject();
    JsonArray pipeline = new JsonArray();
    aggregation.putString("aggregate", collection).putBoolean("allowDiskUse", true).putArray("pipeline", pipeline);
    pipeline.addObject(new JsonObject().putObject("$match", MongoQueryBuilder.build(criteriaQuery)));
    JsonObject id = new JsonObject().putString(PROFILE_ID, "$" + PROFILE_ID);
    if (isAccessAllModules && !isExport) {
        // Case : get JSON data for indicator "access to all modules"
        id.putString(MODULE_ID, "$" + MODULE_ID);
    } else {
        id.putString(STATS_FIELD_DATE, "$" + STATS_FIELD_DATE);
    }
    JsonObject group = new JsonObject().putObject("_id", id).putObject(indicator, new JsonObject().putString("$sum", "$" + indicator));
    if (isActivatedAccountsIndicator) {
        group.putObject(STATS_FIELD_ACCOUNTS, new JsonObject().putString("$sum", "$" + STATS_FIELD_ACCOUNTS));
    }
    JsonObject groupBy = new JsonObject().putObject("$group", group);
    pipeline.addObject(groupBy);
    QueryBuilder projection = QueryBuilder.start("_id").is(0).and(PROFILE_ID).is("$_id." + PROFILE_ID);
    if (isActivatedAccountsIndicator) {
        projection.and(STATS_FIELD_ACCOUNTS).is(1);
    }
    if (!isExport) {
        projection.and(indicator).is(1);
        if (isAccessAllModules) {
            projection.and(MODULE_ID).is("$_id." + MODULE_ID);
        } else {
            projection.and(STATS_FIELD_DATE).is("$_id." + STATS_FIELD_DATE);
        }
        // Sum stats for all structure_ids
        pipeline.addObject(new JsonObject().putObject("$project", MongoQueryBuilder.build(projection)));
    } else {
        // Projection : keep 'yyyy-MM' from 'yyyy-MM-dd HH:mm.ss.SSS'
        DBObject dateSubstring = new BasicDBObject();
        BasicDBList dbl = new BasicDBList();
        dbl.add("$_id." + STATS_FIELD_DATE);
        dbl.add(0);
        dbl.add(7);
        dateSubstring.put("$substr", dbl);
        projection.and(STATS_FIELD_DATE).is(dateSubstring).and("indicatorValue").is(// Replace indicatorName by label "indicatorValue", so that the mustache template can be generic
        "$" + indicator);
        JsonObject sort = sortByStructureDateProfile;
        // Export stats for each structure_id
        id.putString(STRUCTURES_ID, "$" + STRUCTURES_ID);
        projection.and(STRUCTURES_ID).is("$_id." + STRUCTURES_ID);
        if (isAccessIndicator) {
            if (isAccessAllModules) {
                sort = sort.copy().putNumber(MODULE_ID, 1);
            }
            // Export stats for each module_id
            id.putString(MODULE_ID, "$" + MODULE_ID);
            projection.and(MODULE_ID).is("$_id." + MODULE_ID);
        }
        pipeline.addObject(new JsonObject().putObject("$project", MongoQueryBuilder.build(projection)));
        pipeline.addObject(new JsonObject().putObject("$sort", sort));
    }
    mongo.command(aggregation.toString(), new Handler<Message<JsonObject>>() {

        @Override
        public void handle(Message<JsonObject> message) {
            if ("ok".equals(message.body().getString("status")) && message.body().getObject("result", new JsonObject()).getInteger("ok") == 1) {
                JsonArray result = message.body().getObject("result").getArray("result");
                handler.handle(new Either.Right<String, JsonArray>(result));
            } else {
                String error = message.body().toString();
                handler.handle(new Either.Left<String, JsonArray>(error));
            }
        }
    });
}
Also used : Message(org.vertx.java.core.eventbus.Message) JsonObject(org.vertx.java.core.json.JsonObject) QueryBuilder(com.mongodb.QueryBuilder) MongoQueryBuilder(fr.wseduc.mongodb.MongoQueryBuilder) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) JsonArray(org.vertx.java.core.json.JsonArray) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList)

Example 8 with JsonObject

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

the class StructureServiceNeo4jImpl method getListStructuresForUser.

public void getListStructuresForUser(String userId, Handler<Either<String, JsonArray>> handler) {
    String query = "match (u:User)-[IN]->(pg:ProfileGroup)-[DEPENDS]->(s:Structure)<-[:HAS_ATTACHMENT*0..]-(s2:Structure) " + " where u.id = {userId} return distinct s2.id as id, s2.name + case when exists(s2.UAI) then '(' + s2.UAI +')' else '' end as name";
    JsonObject params = new JsonObject().putString("userId", userId);
    neo4j.execute(query, params, validResultHandler(handler));
}
Also used : JsonObject(org.vertx.java.core.json.JsonObject)

Example 9 with JsonObject

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

the class StructureServiceNeo4jImpl method list.

@Override
public void list(JsonArray structureIds, Handler<Either<String, JsonArray>> handler) {
    String query = "MATCH (s:Structure) WHERE s.id IN {structureIds} RETURN s.id as id, s.name as name, s.UAI as uai, s.city as city";
    JsonObject params = new JsonObject().putArray("structureIds", structureIds);
    neo4j.execute(query, params, validResultHandler(handler));
}
Also used : JsonObject(org.vertx.java.core.json.JsonObject)

Example 10 with JsonObject

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

the class Statistics method aggregateEvents.

// Aggregate documents of collection "events" for each day, from startDate to endDate
public void aggregateEvents(Vertx vertx, final Date startDate, final Date endDate) {
    final Calendar fromCal = Calendar.getInstance();
    fromCal.setTime(startDate);
    final Calendar toCal = (Calendar) fromCal.clone();
    toCal.add(Calendar.DAY_OF_MONTH, 1);
    // Launch aggregations sequentially, one after the other
    vertx.setTimer(1000L, new Handler<Long>() {

        @Override
        public void handle(final Long event) {
            Handler<JsonObject> handler = new Handler<JsonObject>() {

                @Override
                public void handle(JsonObject aggregateEvent) {
                    try {
                        if (!"ok".equals(aggregateEvent.getString("status", null))) {
                            log.error("Error in AggregateTask : status is different from ok." + aggregateEvent.toString());
                        } else {
                            // Increment dates
                            fromCal.add(Calendar.DAY_OF_MONTH, 1);
                            toCal.add(Calendar.DAY_OF_MONTH, 1);
                            if (fromCal.getTime().before(endDate)) {
                                AggregateTask aggTask = new AggregateTask(fromCal.getTime(), toCal.getTime(), this);
                                aggTask.handle(0L);
                            }
                        }
                    } catch (Exception e) {
                        log.error("Error in AggregateTask when checking status", e);
                    }
                }
            };
            AggregateTask aggTask = new AggregateTask(fromCal.getTime(), toCal.getTime(), handler);
            aggTask.handle(event);
        }
    });
}
Also used : AggregateTask(net.atos.entng.statistics.aggregation.AggregateTask) Calendar(java.util.Calendar) Handler(org.vertx.java.core.Handler) JsonObject(org.vertx.java.core.json.JsonObject) ParseException(java.text.ParseException)

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