use of io.apiman.manager.api.beans.metrics.ClientUsagePerApiBean in project apiman by apiman.
the class JdbcMetricsAccessorTest method testGetClientUsagePerApi.
/**
* Test for {@link JdbcMetricsAccessor#getClientUsagePerApi(String, String, String, DateTime, DateTime)}
*/
@Test
public void testGetClientUsagePerApi() {
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");
ClientUsagePerApiBean usage = accessor.getClientUsagePerApi("TestOrg", "TestClient", "1.0", from, to);
Map<String, Long> expectedData = new HashMap<>();
expectedData.put("TestApi", 15L);
expectedData.put("OtherApi", 4L);
Assert.assertEquals(expectedData, usage.getData());
}
use of io.apiman.manager.api.beans.metrics.ClientUsagePerApiBean in project apiman by apiman.
the class JdbcMetricsAccessor method getClientUsagePerApi.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getClientUsagePerApi(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
*/
@Override
public ClientUsagePerApiBean getClientUsagePerApi(String organizationId, String clientId, String version, DateTime from, DateTime to) {
try {
QueryRunner run = new QueryRunner(ds);
// $NON-NLS-1$
String sql = "SELECT api_id, count(*) FROM gw_requests WHERE client_org_id = ? AND client_id = ? AND client_version = ? AND rstart >= ? AND rstart < ? GROUP BY api_id";
ResultSetHandler<ClientUsagePerApiBean> handler = new ClientUsagePerApiHandler();
return run.query(sql, handler, organizationId, clientId, version, from.getMillis(), to.getMillis());
} catch (SQLException e) {
e.printStackTrace();
return new ClientUsagePerApiBean();
}
}
use of io.apiman.manager.api.beans.metrics.ClientUsagePerApiBean in project apiman by apiman.
the class ClientUsagePerApiHandler method handle.
/**
* @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
*/
@Override
public ClientUsagePerApiBean handle(ResultSet rs) throws SQLException {
ClientUsagePerApiBean rval = new ClientUsagePerApiBean();
while (rs.next()) {
String apiId = rs.getString(1);
long count = rs.getLong(2);
rval.getData().put(apiId, count);
}
return rval;
}
use of io.apiman.manager.api.beans.metrics.ClientUsagePerApiBean in project apiman by apiman.
the class EsMetricsAccessor method getClientUsagePerApi.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getClientUsagePerApi(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
*/
@Override
@SuppressWarnings("nls")
public ClientUsagePerApiBean getClientUsagePerApi(String organizationId, String clientId, String version, DateTime from, DateTime to) {
ClientUsagePerApiBean rval = new ClientUsagePerApiBean();
try {
String query = "{" + " \"query\": {" + " \"bool\": {" + " \"filter\": [{" + " \"term\": {" + " \"clientOrgId\": \"{{clientOrgId}}\"" + " }" + " }," + " {" + " \"term\": {" + " \"clientId\": \"{{clientId}}\"" + " }" + " }," + " {" + " \"term\": {" + " \"clientVersion\": \"{{clientVersion}}\"" + " }" + " }," + " {" + " \"range\": {" + " \"requestStart\": {" + " \"gte\": \"{{from}}\"," + " \"lte\": \"{{to}}\"" + " }" + " }" + " }" + " ]" + " }" + " }," + " \"size\": 0," + " \"aggs\": {" + " \"usage_by_api\": {" + " \"terms\": {" + " \"field\": \"apiId\"" + " }" + " }" + " }" + "}";
Map<String, Object> params = new HashMap<>();
params.put("from", MetricsAccessorHelper.formatDate(from));
params.put("to", MetricsAccessorHelper.formatDate(to));
params.put("clientOrgId", organizationId.replace('"', '_'));
params.put("clientId", clientId.replace('"', '_'));
params.put("clientVersion", version.replace('"', '_'));
SearchResponse response = this.doSearchTemplateRequest(query, params);
ParsedStringTerms aggregation = response.getAggregations().get("usage_by_api");
if (aggregation != null) {
List<ParsedStringTerms.ParsedBucket> buckets = (List<ParsedStringTerms.ParsedBucket>) aggregation.getBuckets();
for (ParsedStringTerms.ParsedBucket entry : buckets) {
rval.getData().put(entry.getKeyAsString(), entry.getDocCount());
}
}
} catch (IOException e) {
LOGGER.error(e);
}
return rval;
}
Aggregations