use of io.apiman.manager.api.beans.metrics.ResponseStatsPerClientBean in project apiman by apiman.
the class JdbcMetricsAccessor method getResponseStatsPerClient.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsPerClient(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
*/
@Override
public ResponseStatsPerClientBean getResponseStatsPerClient(String organizationId, String apiId, String version, DateTime from, DateTime to) {
try {
QueryRunner run = new QueryRunner(ds);
// $NON-NLS-1$
String sql = "SELECT client_id, resp_type, count(*) FROM gw_requests WHERE api_org_id = ? AND api_id = ? AND api_version = ? AND rstart >= ? AND rstart < ? GROUP BY client_id, resp_type";
ResultSetHandler<ResponseStatsPerClientBean> handler = new ResponseStatsPerClientHandler();
return run.query(sql, handler, organizationId, apiId, version, from.getMillis(), to.getMillis());
} catch (SQLException e) {
e.printStackTrace();
return new ResponseStatsPerClientBean();
}
}
use of io.apiman.manager.api.beans.metrics.ResponseStatsPerClientBean in project apiman by apiman.
the class EsMetricsAccessor method getResponseStatsPerClient.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsPerClient(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
*/
@Override
@SuppressWarnings("nls")
public ResponseStatsPerClientBean getResponseStatsPerClient(String organizationId, String apiId, String version, DateTime from, DateTime to) {
ResponseStatsPerClientBean rval = new ResponseStatsPerClientBean();
try {
String query = "{" + " \"query\": {" + " \"bool\": {" + " \"filter\": [{" + " \"term\": {" + " \"apiOrgId\": \"{{apiOrgId}}\"" + " }" + " }, {" + " \"term\": {" + " \"apiId\": \"{{apiId}}\"" + " }" + " }, {" + " \"term\": {" + " \"apiVersion\": \"{{apiVersion}}\"" + " }" + " }, {" + " \"range\": {" + " \"requestStart\": {" + " \"gte\": \"{{from}}\"," + " \"lte\": \"{{to}}\"" + " }" + " }" + " }" + " ]" + " }" + " }," + " \"aggs\": {" + " \"by_client\": {" + " \"terms\": {" + " \"field\": \"clientId\"" + " }," + " \"aggs\": {" + " \"total_failures\": {" + " \"filter\": {" + " \"term\": {" + " \"failure\": true" + " }" + " }" + " }," + " \"total_errors\": {" + " \"filter\": {" + " \"term\": {" + " \"error\": true" + " }" + " }" + " }" + " }" + " }" + " }," + " \"size\": 0" + "}";
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('"', '_'));
SearchResponse response = this.doSearchTemplateRequest(query, params);
List<ParsedStringTerms.ParsedBucket> buckets = (List<ParsedStringTerms.ParsedBucket>) ((ParsedStringTerms) response.getAggregations().get("by_client")).getBuckets();
int counter = 0;
for (ParsedStringTerms.ParsedBucket entry : buckets) {
rval.addDataPoint(entry.getKeyAsString(), entry.getDocCount(), ((ParsedFilter) entry.getAggregations().get("total_failures")).getDocCount(), ((ParsedFilter) entry.getAggregations().get("total_errors")).getDocCount());
counter++;
if (counter > 10) {
break;
}
}
} catch (IOException e) {
LOGGER.error(e);
}
return rval;
}
use of io.apiman.manager.api.beans.metrics.ResponseStatsPerClientBean in project apiman by apiman.
the class ResponseStatsPerClientHandler method handle.
/**
* @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
*/
@Override
public ResponseStatsPerClientBean handle(ResultSet rs) throws SQLException {
ResponseStatsPerClientBean rval = new ResponseStatsPerClientBean();
while (rs.next()) {
String client = rs.getString(1);
if (client == null) {
continue;
}
String rtype = rs.getString(2);
long count = rs.getLong(3);
ResponseStatsDataPoint dataPoint = rval.getData().get(client);
if (dataPoint == null) {
dataPoint = new ResponseStatsDataPoint();
rval.getData().put(client, dataPoint);
}
if (rtype == null) {
dataPoint.setTotal(dataPoint.getErrors() + dataPoint.getFailures() + count);
} else if (rtype.equals("failure")) {
// $NON-NLS-1$
dataPoint.setTotal(dataPoint.getTotal() + count);
dataPoint.setFailures(count);
} else if (rtype.equals("error")) {
// $NON-NLS-1$
dataPoint.setTotal(dataPoint.getTotal() + count);
dataPoint.setErrors(count);
}
}
return rval;
}
use of io.apiman.manager.api.beans.metrics.ResponseStatsPerClientBean in project apiman by apiman.
the class JdbcMetricsAccessorTest method testGetResponseStatsPerClient.
/**
* Test for {@link JdbcMetricsAccessor#getResponseStatsPerClient(String, String, String, DateTime, DateTime)}
*/
@Test
public void testGetResponseStatsPerClient() {
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-02-01T00:00:00Z");
DateTime to = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-03-01T00:00:00Z");
ResponseStatsPerClientBean stats = accessor.getResponseStatsPerClient("TestOrg", "TestApi", "1.0", from, to);
ResponseStatsDataPoint dataPoint = stats.getData().get("TestClient");
Assert.assertNotNull(dataPoint);
Assert.assertEquals(16, dataPoint.getTotal());
Assert.assertEquals(3, dataPoint.getFailures());
Assert.assertEquals(1, dataPoint.getErrors());
dataPoint = stats.getData().get("OtherClient");
Assert.assertNotNull(dataPoint);
Assert.assertEquals(2, dataPoint.getTotal());
Assert.assertEquals(0, dataPoint.getFailures());
Assert.assertEquals(0, dataPoint.getErrors());
}
Aggregations