use of io.apiman.manager.api.beans.metrics.ResponseStatsHistogramBean in project apiman by apiman.
the class JdbcMetricsAccessor method getResponseStats.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStats(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, org.joda.time.DateTime, org.joda.time.DateTime)
*/
@Override
public ResponseStatsHistogramBean getResponseStats(String organizationId, String apiId, String version, HistogramIntervalType interval, DateTime from, DateTime to) {
ResponseStatsHistogramBean rval = new ResponseStatsHistogramBean();
Map<Long, ResponseStatsDataPoint> index = MetricsAccessorHelper.generateHistogramSkeleton(rval, from, to, interval, ResponseStatsDataPoint.class, Long.class);
try {
QueryRunner run = new QueryRunner(ds);
String gbColumn = groupByColumn(interval);
// $NON-NLS-1$ //$NON-NLS-2$
String sql = "SELECT " + gbColumn + ", resp_type, count(*) FROM gw_requests WHERE api_org_id = ? AND api_id = ? AND api_version = ? AND rstart >= ? AND rstart < ? GROUP BY resp_type," + gbColumn;
ResultSetHandler<ResponseStatsHistogramBean> handler = new ResponseStatsHistogramHandler(rval, index);
run.query(sql, handler, organizationId, apiId, version, from.getMillis(), to.getMillis());
} catch (SQLException e) {
e.printStackTrace();
}
return rval;
}
use of io.apiman.manager.api.beans.metrics.ResponseStatsHistogramBean in project apiman by apiman.
the class JdbcMetricsAccessorTest method testGetResponseStats.
/**
* Test method for {@link io.apiman.manager.api.jdbc.JdbcMetricsAccessor#getResponseStats(String, String, String, HistogramIntervalType, DateTime, DateTime)}.
*/
@Test
public void testGetResponseStats() {
Map<String, String> config = new HashMap<>();
config.put("datasource.jndi-location", DB_JNDI_LOC);
JdbcMetricsAccessor accessor = new JdbcMetricsAccessor(config);
DateTime from = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-01-01T00:00:00Z");
DateTime to = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-12-31T00:00:00Z");
ResponseStatsHistogramBean stats = accessor.getResponseStats("TestOrg", "TestApi", "1.0", HistogramIntervalType.month, from, to);
Assert.assertNotNull(stats);
for (ResponseStatsDataPoint dataPoint : stats.getData()) {
if (dataPoint.getLabel().equals("2016-02-01T00:00:00.000Z")) {
Assert.assertEquals(18, dataPoint.getTotal());
Assert.assertEquals(1, dataPoint.getErrors());
Assert.assertEquals(3, dataPoint.getFailures());
} else {
Assert.assertEquals(0, dataPoint.getTotal());
Assert.assertEquals(0, dataPoint.getErrors());
Assert.assertEquals(0, dataPoint.getFailures());
}
}
from = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-02-01T00:00:00Z");
to = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-03-01T00:00:00Z");
stats = accessor.getResponseStats("TestOrg", "TestApi", "1.0", HistogramIntervalType.day, from, to);
Assert.assertNotNull(stats);
ResponseStatsDataPoint dataPoint = stats.getData().get(9);
Assert.assertEquals(2, dataPoint.getTotal());
Assert.assertEquals(0, dataPoint.getErrors());
Assert.assertEquals(0, dataPoint.getFailures());
dataPoint = stats.getData().get(10);
Assert.assertEquals(1, dataPoint.getTotal());
Assert.assertEquals(0, dataPoint.getErrors());
Assert.assertEquals(0, dataPoint.getFailures());
dataPoint = stats.getData().get(22);
Assert.assertEquals(9, dataPoint.getTotal());
Assert.assertEquals(1, dataPoint.getErrors());
Assert.assertEquals(1, dataPoint.getFailures());
dataPoint = stats.getData().get(23);
Assert.assertEquals(6, dataPoint.getTotal());
Assert.assertEquals(0, dataPoint.getErrors());
Assert.assertEquals(2, dataPoint.getFailures());
dataPoint = stats.getData().get(5);
Assert.assertEquals(0, dataPoint.getTotal());
Assert.assertEquals(0, dataPoint.getErrors());
Assert.assertEquals(0, dataPoint.getFailures());
dataPoint = stats.getData().get(19);
Assert.assertEquals(0, dataPoint.getTotal());
Assert.assertEquals(0, dataPoint.getErrors());
Assert.assertEquals(0, dataPoint.getFailures());
}
use of io.apiman.manager.api.beans.metrics.ResponseStatsHistogramBean in project apiman by apiman.
the class EsMetricsAccessor method getResponseStats.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStats(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, org.joda.time.DateTime, org.joda.time.DateTime)
*/
@SuppressWarnings("nls")
@Override
public ResponseStatsHistogramBean getResponseStats(String organizationId, String apiId, String version, HistogramIntervalType interval, DateTime from, DateTime to) {
ResponseStatsHistogramBean rval = new ResponseStatsHistogramBean();
Map<String, ResponseStatsDataPoint> index = MetricsAccessorHelper.generateHistogramSkeleton(rval, from, to, interval, ResponseStatsDataPoint.class);
try {
String query = "{" + " \"query\": {" + " \"bool\": {" + " \"filter\": [{" + " \"term\": {" + " \"apiOrgId\": \"{{apiOrgId}}\"" + " }" + " }, {" + " \"term\": {" + " \"apiId\": \"{{apiId}}\"" + " }" + " }, {" + " \"term\": {" + " \"apiVersion\": \"{{apiVersion}}\"" + " }" + " }, {" + " \"range\": {" + " \"requestStart\": {" + " \"gte\": \"{{from}}\"," + " \"lte\": \"{{to}}\"" + " }" + " }" + " }]" + " }" + " }," + " \"size\": 0," + " \"aggs\": {" + " \"histogram\": {" + " \"date_histogram\": {" + " \"field\": \"requestStart\"," + " \"calendar_interval\": \"{{interval}}\"" + " }," + " \"aggs\": {" + " \"total_failures\": {" + " \"filter\": {" + " \"term\": {" + " \"failure\": true" + " }" + " }" + " }," + " \"total_errors\": {" + " \"filter\": {" + " \"term\": {" + " \"error\": true" + " }" + " }" + " }" + " }" + " }" + " }" + "}";
Map<String, Object> params = new HashMap<>();
params.put("from", MetricsAccessorHelper.formatDate(from));
params.put("to", MetricsAccessorHelper.formatDate(to));
params.put("apiOrgId", organizationId.replace('"', '_'));
params.put("apiId", apiId.replace('"', '_'));
params.put("apiVersion", version.replace('"', '_'));
params.put("interval", interval.name());
SearchResponse searchResponse = this.doSearchTemplateRequest(query, params);
ParsedDateHistogram aggregation = (ParsedDateHistogram) searchResponse.getAggregations().asMap().get("histogram");
if (aggregation != null) {
List<ParsedDateHistogram.ParsedBucket> buckets = (List<ParsedDateHistogram.ParsedBucket>) aggregation.getBuckets();
for (ParsedDateHistogram.ParsedBucket entry : buckets) {
String keyAsString = entry.getKeyAsString();
if (index.containsKey(keyAsString)) {
ParsedFilter totalFailuresAgg = entry.getAggregations().get("total_failures");
ParsedFilter totalErrorsAgg = entry.getAggregations().get("total_errors");
long failures = totalFailuresAgg.getDocCount();
long errors = totalErrorsAgg.getDocCount();
ResponseStatsDataPoint point = index.get(keyAsString);
point.setTotal(entry.getDocCount());
point.setFailures(failures);
point.setErrors(errors);
}
}
}
} catch (IOException e) {
LOGGER.error(e);
}
return rval;
}
Aggregations