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();
}
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();
}
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();
}
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);
}
}
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);
}
}
}
Aggregations