Search in sources :

Example 6 with Handler

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

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

Example 8 with Handler

use of org.vertx.java.core.Handler 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 9 with Handler

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

Example 10 with Handler

use of org.vertx.java.core.Handler in project fabric8 by jboss-fuse.

the class HttpGatewayHandler method doRouteRequest.

protected void doRouteRequest(Map<String, MappedServices> mappingRules, final HttpServerRequest request) {
    String uri = request.uri();
    String uri2 = uri;
    if (addMissingTrailingSlashes) {
        uri2 = normalizeUri(uri);
    }
    HttpClient client = null;
    String remaining = null;
    String prefix = null;
    String proxyServiceUrl = null;
    String reverseServiceUrl = null;
    MappedServices mappedServices = null;
    URL clientURL = null;
    Set<Map.Entry<String, MappedServices>> entries = mappingRules.entrySet();
    for (Map.Entry<String, MappedServices> entry : entries) {
        String path = entry.getKey();
        mappedServices = entry.getValue();
        String pathPrefix = path;
        boolean uriMatches = uri.startsWith(pathPrefix);
        boolean uri2Matches = uri2 != null && uri2.startsWith(pathPrefix);
        if (uriMatches || uri2Matches) {
            int pathPrefixLength = pathPrefix.length();
            if (uri2Matches && pathPrefixLength < uri2.length()) {
                remaining = uri2.substring(pathPrefixLength);
            } else if (pathPrefixLength < uri.length()) {
                remaining = uri.substring(pathPrefixLength);
            } else {
                remaining = null;
            }
            // now lets pick a service for this path
            proxyServiceUrl = mappedServices.chooseService(request);
            if (proxyServiceUrl != null) {
                // lets create a client for this request...
                try {
                    clientURL = new URL(proxyServiceUrl);
                    client = createClient(clientURL);
                    prefix = clientURL.getPath();
                    reverseServiceUrl = request.absoluteURI().resolve(pathPrefix).toString();
                    if (reverseServiceUrl.endsWith("/")) {
                        reverseServiceUrl = reverseServiceUrl.substring(0, reverseServiceUrl.length() - 1);
                    }
                    break;
                } catch (MalformedURLException e) {
                    LOG.warn("Failed to parse URL: " + proxyServiceUrl + ". " + e, e);
                }
            }
        }
    }
    if (client != null) {
        String servicePath = prefix != null ? prefix : "";
        // we should usually end the prefix path with a slash for web apps at least
        if (servicePath.length() > 0 && !servicePath.endsWith("/")) {
            servicePath += "/";
        }
        if (remaining != null) {
            servicePath += remaining;
        }
        client.exceptionHandler(new Handler<Throwable>() {

            @Override
            public void handle(Throwable throwable) {
                if (throwable instanceof ConnectTimeoutException) {
                    request.response().setStatusCode(504);
                    request.response().end();
                } else {
                    LOG.error("Unhandled exception", throwable);
                }
            }
        });
        client.setConnectTimeout(connectionTimeout);
        LOG.info("Proxying request {} to service path: {} on service: {} reverseServiceUrl: {}", uri, servicePath, proxyServiceUrl, reverseServiceUrl);
        final HttpClient finalClient = client;
        Handler<HttpClientResponse> responseHandler = new Handler<HttpClientResponse>() {

            public void handle(HttpClientResponse clientResponse) {
                LOG.debug("Proxying response: {}", clientResponse.statusCode());
                request.response().setStatusCode(clientResponse.statusCode());
                request.response().headers().set(clientResponse.headers());
                applyChunkedEncoding(request.response());
                clientResponse.dataHandler(new Handler<Buffer>() {

                    public void handle(Buffer data) {
                        LOG.debug("Proxying response body: {}", data);
                        request.response().write(data);
                    }
                });
                clientResponse.endHandler(new VoidHandler() {

                    public void handle() {
                        request.response().end();
                        finalClient.close();
                    }
                });
            }
        };
        if (mappedServices != null) {
            ProxyMappingDetails proxyMappingDetails = new ProxyMappingDetails(proxyServiceUrl, reverseServiceUrl, servicePath);
            responseHandler = mappedServices.wrapResponseHandlerInPolicies(request, responseHandler, proxyMappingDetails);
        }
        final HttpClientRequest clientRequest = client.request(request.method(), servicePath, responseHandler);
        clientRequest.headers().set(request.headers());
        clientRequest.setChunked(true);
        if (requestTimeout != DEFAULT_REQUEST_TIMEOUT) {
            clientRequest.exceptionHandler(new Handler<Throwable>() {

                @Override
                public void handle(Throwable throwable) {
                    if (throwable instanceof TimeoutException) {
                        request.response().setStatusCode(504);
                        request.response().end();
                    } else {
                        LOG.error("Unhandled exception", throwable);
                    }
                }
            });
            clientRequest.setTimeout(requestTimeout);
        }
        request.dataHandler(new Handler<Buffer>() {

            public void handle(Buffer data) {
                LOG.debug("Proxying request body: {}", data);
                clientRequest.write(data);
            }
        });
        request.endHandler(new VoidHandler() {

            public void handle() {
                LOG.debug("end of the request");
                clientRequest.end();
            }
        });
    } else {
        // lets return a 404
        LOG.info("Could not find matching proxy path for {} from paths: {}", uri, mappingRules.keySet());
        request.response().setStatusCode(404);
        request.response().end();
    }
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) VoidHandler(org.vertx.java.core.VoidHandler) MalformedURLException(java.net.MalformedURLException) VoidHandler(org.vertx.java.core.VoidHandler) Handler(org.vertx.java.core.Handler) URL(java.net.URL) HttpClientRequest(org.vertx.java.core.http.HttpClientRequest) HttpClient(org.vertx.java.core.http.HttpClient) HttpClientResponse(org.vertx.java.core.http.HttpClientResponse) HashMap(java.util.HashMap) Map(java.util.Map) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException)

Aggregations

Handler (org.vertx.java.core.Handler)12 JsonObject (org.vertx.java.core.json.JsonObject)6 JsonArray (org.vertx.java.core.json.JsonArray)5 Either (fr.wseduc.webutils.Either)4 HttpClientResponse (org.vertx.java.core.http.HttpClientResponse)4 SecuredAction (fr.wseduc.security.SecuredAction)3 FutureHandler (io.fabric8.gateway.handlers.detecting.FutureHandler)3 HttpGatewayHandler (io.fabric8.gateway.handlers.http.HttpGatewayHandler)3 UserInfos (org.entcore.common.user.UserInfos)3 Test (org.junit.Test)3 MongoUpdateBuilder (fr.wseduc.mongodb.MongoUpdateBuilder)2 Post (fr.wseduc.rs.Post)2 MalformedURLException (java.net.MalformedURLException)2 MongoDBBuilder (org.entcore.common.aggregation.filters.dbbuilders.MongoDBBuilder)2 AsyncResult (org.vertx.java.core.AsyncResult)2 NetClient (org.vertx.java.core.net.NetClient)2 NetSocket (org.vertx.java.core.net.NetSocket)2 ApiDoc (fr.wseduc.rs.ApiDoc)1 Get (fr.wseduc.rs.Get)1 ServiceDetails (io.fabric8.gateway.ServiceDetails)1