Search in sources :

Example 1 with JsonObject

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

the class IndicatorCustomImpl method getAccounts.

private void getAccounts(Handler<Either<String, JsonArray>> handler) {
    // Number of accounts and activated accounts per profile and structure
    String query = "MATCH (u:User)-[:IN]->(:ProfileGroup)-[:DEPENDS]->(s:Structure)" + " RETURN s.id AS structure, HEAD(u.profiles) AS profile, count(distinct u) AS accounts," + " count(distinct u.password) AS activatedAccounts ORDER by structure, profile";
    neo.execute(query, new JsonObject(), validResultHandler(handler));
}
Also used : JsonObject(org.vertx.java.core.json.JsonObject)

Example 2 with JsonObject

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

the class IndicatorFactory method getActivationIndicator.

// 1) Indicators that are incremented every day
// UserAccount Activation
public static Indicator getActivationIndicator(Collection<IndicatorFilterMongoImpl> filters, Date pWriteDate) {
    Collection<IndicatorGroup> indicatorGroups = new ArrayList<>();
    indicatorGroups.add(new IndicatorGroup(TRACE_FIELD_STRUCTURES).setArray(true).addAndReturnChild(TRACE_FIELD_PROFILE));
    IndicatorMongoImpl indicator = new IndicatorMongoImpl(TRACE_TYPE_ACTIVATION, filters, indicatorGroups) {

        @Override
        protected void customizePipeline(JsonArray pipeline) {
            int triggerSize = 0;
            // Remove "count" from stage "$group" in pipeline, add userCount instead, 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.size() == 0) {
                        id.putString(TRACE_FIELD_USER, "$" + TRACE_FIELD_USER);
                    } else if (id != null && id.size() > 0) {
                        // We are in a "structures" stats query
                        id.putString(TRACE_FIELD_STRUCTURES, "$" + TRACE_FIELD_STRUCTURES).putString(TRACE_FIELD_PROFILE, "$" + TRACE_FIELD_PROFILE).putString(TRACE_FIELD_USER, "$" + TRACE_FIELD_USER);
                        triggerSize = 1;
                    }
                    group.putObject("userCount", new JsonObject().putNumber("$min", 1));
                    break;
                }
            }
            // Add another "$group" stage in pipeline, to count unique activation per user
            JsonObject groupByActiv = new JsonObject();
            if (triggerSize == 0) {
                groupByActiv.putObject("$group", new JsonObject().putObject("_id", new JsonObject()).putObject("count", new JsonObject().putString("$sum", "$userCount")));
            } else {
                groupByActiv.putObject("$group", new JsonObject().putObject("_id", new JsonObject().putString(TRACE_FIELD_STRUCTURES, "$_id." + TRACE_FIELD_STRUCTURES).putString(TRACE_FIELD_PROFILE, "$_id." + TRACE_FIELD_PROFILE)).putObject("count", new JsonObject().putString("$sum", "$userCount")));
            }
            pipeline.addObject(groupByActiv);
        }
    };
    indicator.setWriteDate(pWriteDate);
    return indicator;
}
Also used : JsonArray(org.vertx.java.core.json.JsonArray) IndicatorGroup(org.entcore.common.aggregation.groups.IndicatorGroup) ArrayList(java.util.ArrayList) JsonObject(org.vertx.java.core.json.JsonObject) IndicatorMongoImpl(org.entcore.common.aggregation.indicators.mongo.IndicatorMongoImpl)

Example 3 with JsonObject

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

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

the class StatisticsController method deleteRegeneratedStatistics.

// delete all the statisticcs from mongoDB who will be generated again.
private void deleteRegeneratedStatistics(Date startDate, Date endDate, Handler<Either<String, JsonArray>> handler) {
    MongoDb mongo = MongoDb.getInstance();
    JsonObject criteria = new JsonObject();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00.00.000");
    String strStartDate = df.format(startDate);
    String strEndDate = df.format(endDate);
    // first condition
    final JsonArray cond = new JsonArray().addObject(new JsonObject().putObject("date", new JsonObject().putString("$gte", strStartDate)));
    // second condition
    cond.addObject(new JsonObject().putObject("date", new JsonObject().putString("$lt", strEndDate)));
    // query = condition1 AND condition2
    final JsonObject query = new JsonObject().putArray("$and", cond);
    // launch deletion
    mongo.delete("stats", query, MongoDbResult.validResultsHandler(handler));
}
Also used : JsonArray(org.vertx.java.core.json.JsonArray) JsonObject(org.vertx.java.core.json.JsonObject) MongoDb(fr.wseduc.mongodb.MongoDb) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with JsonObject

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

the class StatisticsController method getData.

@Post("/data")
@SecuredAction("statistics.get.data")
public void getData(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) {
                        log.debug("User not found in session.");
                        unauthorized(request);
                        return;
                    }
                    JsonArray arr = params.getArray("schoolIdArray");
                    List<String> schoolIds = new ArrayList<String>();
                    for (int i = 0; i < arr.size(); i++) {
                        schoolIds.add((String) arr.get(i));
                    }
                    // final List<String> schoolIds = Arrays.asList(params.getArray("schoolIdArray"));
                    if (schoolIds == null || schoolIds.size() == 0) {
                        String errorMsg = i18n.translate("statistics.bad.request.invalid.schools", getHost(request), acceptLanguage(request));
                        badRequest(request, errorMsg);
                        return;
                    }
                    // final String indicator = request.params().get(PARAM_INDICATOR);
                    final String indicator = params.getString("indicator");
                    if (indicator == null || indicator.trim().isEmpty() || !indicators.contains(indicator)) {
                        String errorMsg = i18n.translate("statistics.bad.request.invalid.indicator", getHost(request), acceptLanguage(request));
                        badRequest(request, errorMsg);
                        return;
                    }
                    String module = "";
                    if (TRACE_TYPE_SVC_ACCESS.equals(indicator)) {
                        // module = request.params().get(PARAM_MODULE);
                        module = params.getString("module");
                        if (module != null && !module.trim().isEmpty() && !accessModules.contains(module)) {
                            String errorMsg = i18n.translate("statistics.bad.request.invalid.module", getHost(request), acceptLanguage(request));
                            badRequest(request, errorMsg);
                            return;
                        }
                    // Else (when module is not specified) return data for all modules
                    }
                    // String startDate = request.params().get(PARAM_START_DATE);
                    // String endDate = request.params().get(PARAM_END_DATE);
                    String startDate = String.valueOf(params.getInteger("startDate"));
                    String endDate = String.valueOf(params.getInteger("endDate"));
                    long start, end;
                    try {
                        start = DateUtils.parseStringDate(startDate);
                        end = DateUtils.parseStringDate(endDate);
                        if (end < start || end < 0L || start < 0L) {
                            String errorMsg = i18n.translate("statistics.bad.request.invalid.dates", getHost(request), acceptLanguage(request));
                            badRequest(request, errorMsg);
                            return;
                        }
                    } catch (Exception e) {
                        log.error("Error when casting startDate or endDate to long", e);
                        String errorMsg = i18n.translate("statistics.bad.request.invalid.date.format", getHost(request), acceptLanguage(request));
                        badRequest(request, errorMsg);
                        return;
                    }
                    final JsonObject params = new JsonObject();
                    params.putString(PARAM_INDICATOR, indicator).putNumber(PARAM_START_DATE, start).putNumber(PARAM_END_DATE, end).putString(PARAM_MODULE, module);
                    if (schoolIds.size() == 1) {
                        // if the structure choosed is not a school, we need to explore all the attached schools from the graph base
                        structureService.getAttachedStructureslist(schoolIds.get(0), new Handler<Either<String, JsonArray>>() {

                            @Override
                            public void handle(Either<String, JsonArray> either) {
                                if (either.isLeft()) {
                                    log.error(either.left().getValue());
                                    renderError(request);
                                } else {
                                    final List<String> attachedSchoolsList;
                                    final JsonArray result = either.right().getValue();
                                    if (result != null) {
                                        attachedSchoolsList = new ArrayList<String>(result.size());
                                        for (int i = 0; i < result.size(); i++) {
                                            Object obj = result.get(i);
                                            if (obj instanceof JsonObject) {
                                                final JsonObject jo = (JsonObject) obj;
                                                attachedSchoolsList.add(jo.getString("s2.id", ""));
                                            }
                                        }
                                    } else {
                                        attachedSchoolsList = new ArrayList<String>(0);
                                    }
                                    formatting(attachedSchoolsList, params, indicator, request);
                                }
                            }
                        });
                    } else {
                        formatting(schoolIds, params, indicator, request);
                    }
                }
            });
        }
    });
}
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) JsonObject(org.vertx.java.core.json.JsonObject) SecuredAction(fr.wseduc.security.SecuredAction) Post(fr.wseduc.rs.Post)

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