Search in sources :

Example 1 with Timed

use of org.activityinfo.server.util.monitoring.Timed in project activityinfo by bedatadriven.

the class SitesResources method query.

@GET
@Timed(name = "api.rest.sites")
@Produces(MediaType.APPLICATION_JSON)
public String query(@QueryParam("activity") List<Integer> activityIds, @QueryParam("database") List<Integer> databaseIds, @QueryParam("indicator") List<Integer> indicatorIds, @QueryParam("partner") List<Integer> partnerIds, @QueryParam("attribute") List<Integer> attributeIds, @QueryParam("location") List<Integer> locationIds, @QueryParam("site") List<Integer> siteIds, @QueryParam("legacy") Boolean legacy, @QueryParam("format") String format) throws IOException {
    Filter filter = new Filter();
    filter.addRestriction(DimensionType.Activity, activityIds);
    filter.addRestriction(DimensionType.Database, databaseIds);
    filter.addRestriction(DimensionType.Indicator, indicatorIds);
    filter.addRestriction(DimensionType.Partner, partnerIds);
    filter.addRestriction(DimensionType.Attribute, attributeIds);
    filter.addRestriction(DimensionType.Location, locationIds);
    filter.addRestriction(DimensionType.Site, siteIds);
    GetSites command = new GetSites(filter);
    if (legacy != null) {
        command.setLegacyFetch(legacy);
    }
    List<SiteDTO> sites = dispatcher.execute(command).getData();
    StringWriter writer = new StringWriter();
    JsonGenerator json = Jackson.createJsonFactory(writer);
    json.setCodec(Jackson.createJsonMapper());
    writeJson(sites, json);
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(org.codehaus.jackson.JsonGenerator) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 2 with Timed

use of org.activityinfo.server.util.monitoring.Timed in project activityinfo by bedatadriven.

the class SitesResources method queryMonthlyReports.

@GET
@Path("{id}/monthlyReports")
@Produces("application/json")
@Timed(name = "api.rest.sites.monthly_reports")
public String queryMonthlyReports(@PathParam("id") int siteId) throws IOException {
    GetMonthlyReports command = new GetMonthlyReports(siteId, new Month(0, 1), new Month(Integer.MAX_VALUE, 12));
    MonthlyReportResult result = dispatcher.execute(command);
    // list all months
    Set<String> monthNames = Sets.newHashSet();
    for (IndicatorRowDTO row : result.getData()) {
        for (String propertyName : row.getPropertyNames()) {
            if (propertyName.startsWith("M")) {
                monthNames.add(propertyName);
            }
        }
    }
    // write out results per month
    StringWriter writer = new StringWriter();
    JsonGenerator json = Jackson.createJsonFactory(writer);
    json.writeStartObject();
    for (String monthName : monthNames) {
        json.writeArrayFieldStart(formatMonth(monthName));
        for (IndicatorRowDTO row : result.getData()) {
            if (row.get(monthName) instanceof Number) {
                json.writeStartObject();
                Number value = row.get(monthName);
                json.writeNumberField("indicatorId", row.getIndicatorId());
                json.writeStringField("indicatorName", row.getIndicatorName());
                json.writeNumberField("value", value.doubleValue());
                json.writeEndObject();
            }
        }
        json.writeEndArray();
    }
    json.writeEndObject();
    json.close();
    return writer.toString();
}
Also used : Month(org.activityinfo.model.type.time.Month) StringWriter(java.io.StringWriter) JsonGenerator(org.codehaus.jackson.JsonGenerator) MonthlyReportResult(org.activityinfo.legacy.shared.command.result.MonthlyReportResult) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 3 with Timed

use of org.activityinfo.server.util.monitoring.Timed in project activityinfo by bedatadriven.

the class CubeResource method pivot.

@GET
@Timed(name = "api.rest.sites.pivot")
@Produces("application/json")
public List<Bucket> pivot(@QueryParam("dimension") List<String> dimensions, @QueryParam("form") List<Integer> forms, @QueryParam("month") String monthName) {
    Filter filter = new Filter();
    if (forms.size() == 0) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Must specify at least one ?form={formId}").build());
    }
    filter.addRestriction(DimensionType.Activity, forms);
    if (monthName != null) {
        Month month = Month.parseMonth(monthName);
        filter.setEndDateRange(new DateUtilCalendarImpl().monthRange(month));
    }
    Set<Dimension> pivotDimensions = Sets.newHashSet();
    if (forms.size() == 0) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Must specify at least one ?dimension={indicator|form|database|...}").build());
    }
    for (String dimension : dimensions) {
        switch(dimension) {
            case "indicator":
                pivotDimensions.add(new Dimension(DimensionType.Indicator));
                break;
            case "site":
                pivotDimensions.add(new Dimension(DimensionType.Site));
                break;
            case "month":
                pivotDimensions.add(new DateDimension(DateUnit.MONTH));
                break;
            case "partner":
                pivotDimensions.add(new Dimension(DimensionType.Partner));
                break;
            default:
                throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Invalid dimension '" + dimension + "'").build());
        }
    }
    PivotSites query = new PivotSites();
    query.setFilter(filter);
    query.setDimensions(pivotDimensions);
    if (query.isTooBroad()) {
        return Lists.newArrayList();
    }
    PivotSites.PivotResult result = dispatcherSync.execute(query);
    return result.getBuckets();
}
Also used : Month(org.activityinfo.model.type.time.Month) PivotSites(org.activityinfo.legacy.shared.command.PivotSites) WebApplicationException(javax.ws.rs.WebApplicationException) Filter(org.activityinfo.legacy.shared.command.Filter) DateUtilCalendarImpl(org.activityinfo.server.report.util.DateUtilCalendarImpl) Dimension(org.activityinfo.legacy.shared.reports.model.Dimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) Produces(javax.ws.rs.Produces) Timed(org.activityinfo.server.util.monitoring.Timed) GET(javax.ws.rs.GET)

Example 4 with Timed

use of org.activityinfo.server.util.monitoring.Timed in project activityinfo by bedatadriven.

the class SiteHistoryProcessor method process.

@Timed(name = "updates.site_history")
public void process(Command<?> cmd, final int userId, final int siteId) {
    assert (cmd instanceof SiteCommand);
    LOGGER.fine("persisting site history (site: " + siteId + ", user: " + userId + ")");
    EntityManager em = entityManager.get();
    // It's important to use getOnlyReference() here rather
    // than find() becuase the site might not actually have
    // been sent to the database at this point
    Site site = em.getReference(Site.class, siteId);
    User user = em.getReference(User.class, userId);
    ChangeType type = ChangeType.getType(cmd);
    if (!type.isNew()) {
        Query q = em.createQuery("select count(*) from SiteHistory where site = :site");
        q.setParameter("site", site);
        Long count = (Long) q.getSingleResult();
        if (count == 0) {
            // update, but first entry -> repair history by adding baseline
            // record with complete site json
            LOGGER.fine("site is not new, but history was empty. Adding baseline record..");
            SiteResult siteResult = dispatcher.execute(GetSites.byId(siteId));
            SiteDTO siteDTO = siteResult.getData().get(0);
            String fulljson = JsonUtil.encodeMap(siteDTO.getProperties()).toString();
            SiteHistory baseline = new SiteHistory();
            baseline.setSite(site);
            baseline.setUser(user);
            baseline.setJson(fulljson);
            baseline.setTimeCreated(new Date().getTime());
            baseline.setInitial(false);
            persist(baseline);
        }
    }
    String json = null;
    if (type.isNewOrUpdate()) {
        Map<String, Object> changeMap = ((SiteCommand) cmd).getProperties().getTransientMap();
        if (!changeMap.isEmpty()) {
            json = JsonUtil.encodeMap(changeMap).toString();
        }
    } else if (type.isDelete()) {
        json = JSON_DELETE;
    }
    if (!Strings.isNullOrEmpty(json)) {
        persistHistory(site, user, type, json);
    }
}
Also used : Site(org.activityinfo.server.database.hibernate.entity.Site) User(org.activityinfo.server.database.hibernate.entity.User) Query(javax.persistence.Query) Date(java.util.Date) EntityManager(javax.persistence.EntityManager) SiteResult(org.activityinfo.legacy.shared.command.result.SiteResult) SiteCommand(org.activityinfo.legacy.shared.command.SiteCommand) SiteDTO(org.activityinfo.legacy.shared.model.SiteDTO) SiteHistory(org.activityinfo.server.database.hibernate.entity.SiteHistory) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 5 with Timed

use of org.activityinfo.server.util.monitoring.Timed in project activityinfo by bedatadriven.

the class ImageMapRenderer method drawAdminOverlay.

@Timed(name = "mapping.draw_admin_overlay")
protected void drawAdminOverlay(TiledMap map, Graphics2D g2d, AdminOverlay overlay) {
    List<AdminGeo> geometry = geometryProvider.getGeometries(overlay.getAdminLevelId());
    Color strokeColor = ColorUtil.colorFromString(overlay.getOutlineColor());
    g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    for (AdminGeo adminGeo : geometry) {
        AdminMarker polygon = overlay.getPolygon(adminGeo.getId());
        if (polygon != null) {
            GeneralPath path = PathUtils.toPath(map, adminGeo.getGeometry());
            g2d.setColor(bubbleFillColor(ColorUtil.colorFromString(polygon.getColor())));
            g2d.fill(path);
            g2d.setColor(strokeColor);
            g2d.draw(path);
        }
    }
}
Also used : AdminGeo(org.activityinfo.server.geo.AdminGeo) GeneralPath(com.google.code.appengine.awt.geom.GeneralPath) Timed(org.activityinfo.server.util.monitoring.Timed)

Aggregations

Timed (org.activityinfo.server.util.monitoring.Timed)14 JsonGenerator (org.codehaus.jackson.JsonGenerator)5 StringWriter (java.io.StringWriter)4 Date (java.util.Date)3 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 Filter (org.activityinfo.legacy.shared.command.Filter)2 Month (org.activityinfo.model.type.time.Month)2 LocationType (org.activityinfo.server.database.hibernate.entity.LocationType)2 NewAdminEntity (org.activityinfo.server.endpoint.rest.model.NewAdminEntity)2 GeometrySerializer (com.bedatadriven.geojson.GeometrySerializer)1 GeneralPath (com.google.code.appengine.awt.geom.GeneralPath)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 ParseException (com.vividsolutions.jts.io.ParseException)1 WKBReader (com.vividsolutions.jts.io.WKBReader)1 DataInputStream (java.io.DataInputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 MessagingException (javax.mail.MessagingException)1