Search in sources :

Example 1 with AggregateTask

use of net.atos.entng.statistics.aggregation.AggregateTask 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 2 with AggregateTask

use of net.atos.entng.statistics.aggregation.AggregateTask in project statistics by OPEN-ENT-NG.

the class Statistics method start.

@Override
public void start() {
    super.start();
    JsonArray accessModules = container.config().getArray("access-modules", null);
    if (accessModules == null || accessModules.size() == 0) {
        log.error("Parameter access-modules is null or empty");
        return;
    }
    // 1) Schedule daily aggregation
    /* By default, fire aggregate-cron at 1:15am every day.
		 * Be careful when setting fire times between midnight and 1:00 AM
		 * - "daylight savings" can cause a skip or a repeat depending on whether the time moves back or jumps forward.
		 */
    final String aggregateEventsCron = container.config().getString("aggregate-cron", "0 15 1 ? * * *");
    AggregateTask aggTask = new AggregateTask();
    try {
        new CronTrigger(vertx, aggregateEventsCron).schedule(aggTask);
    } catch (ParseException e) {
        log.fatal(e.getMessage(), e);
        return;
    }
    // In development environment, launch aggregations if parameter "aggregateOnStart" is set to true in module configuration
    if ("dev".equals(container.config().getString("mode", null)) && container.config().getBoolean("aggregateOnStart", false)) {
        Date startDate = new Date();
        // Thu, 30 Apr 2015 00:00:00 GMT
        startDate.setTime(1430352000000L);
        Date endDate = new Date();
        // Fri, 01 May 2015 00:00:00 GMT
        endDate.setTime(1430438400000L);
        this.aggregateEvents(vertx, startDate, endDate);
    }
    // 2) Init controller
    addController(new StatisticsController(vertx, MongoConstants.COLLECTIONS.stats.toString(), accessModules));
    MongoDbConf.getInstance().setCollection(MongoConstants.COLLECTIONS.stats.toString());
// FIXME : the statistics will be kept one year to antoher. No repositoryEvents management
}
Also used : JsonArray(org.vertx.java.core.json.JsonArray) AggregateTask(net.atos.entng.statistics.aggregation.AggregateTask) CronTrigger(fr.wseduc.cron.CronTrigger) StatisticsController(net.atos.entng.statistics.controllers.StatisticsController) ParseException(java.text.ParseException) Date(java.util.Date)

Aggregations

ParseException (java.text.ParseException)2 AggregateTask (net.atos.entng.statistics.aggregation.AggregateTask)2 CronTrigger (fr.wseduc.cron.CronTrigger)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 StatisticsController (net.atos.entng.statistics.controllers.StatisticsController)1 Handler (org.vertx.java.core.Handler)1 JsonArray (org.vertx.java.core.json.JsonArray)1 JsonObject (org.vertx.java.core.json.JsonObject)1