use of io.apiman.manager.api.beans.metrics.UsagePerClientBean in project apiman by apiman.
the class TestMetricsAccessor method getUsagePerClient.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getUsagePerClient(java.lang.String, java.lang.String, java.lang.String, java.util.Date, java.util.Date)
*/
@Override
public UsagePerClientBean getUsagePerClient(String organizationId, String apiId, String version, DateTime from, DateTime to) {
UsagePerClientBean rval = new UsagePerClientBean();
rval.getData().put("my-client", 120384L);
rval.getData().put("foo-client", 1263L);
rval.getData().put("bar-client", 726392L);
return rval;
}
use of io.apiman.manager.api.beans.metrics.UsagePerClientBean in project apiman by apiman.
the class JdbcMetricsAccessor method getUsagePerClient.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getUsagePerClient(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
*/
@Override
public UsagePerClientBean getUsagePerClient(String organizationId, String apiId, String version, DateTime from, DateTime to) {
try {
QueryRunner run = new QueryRunner(ds);
// $NON-NLS-1$
String sql = "SELECT client_id, count(*) FROM gw_requests WHERE api_org_id = ? AND api_id = ? AND api_version = ? AND rstart >= ? AND rstart < ? GROUP BY client_id";
ResultSetHandler<UsagePerClientBean> handler = new UsagePerClientHandler();
return run.query(sql, handler, organizationId, apiId, version, from.getMillis(), to.getMillis());
} catch (SQLException e) {
e.printStackTrace();
return new UsagePerClientBean();
}
}
use of io.apiman.manager.api.beans.metrics.UsagePerClientBean in project apiman by apiman.
the class UsagePerClientHandler method handle.
/**
* @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
*/
@Override
public UsagePerClientBean handle(ResultSet rs) throws SQLException {
UsagePerClientBean rval = new UsagePerClientBean();
while (rs.next()) {
String clientId = rs.getString(1);
if (clientId == null) {
continue;
}
long count = rs.getLong(2);
rval.getData().put(clientId, count);
}
return rval;
}
use of io.apiman.manager.api.beans.metrics.UsagePerClientBean in project apiman by apiman.
the class JdbcMetricsAccessorTest method testGetUsagePerClient.
/**
* Test for {@link JdbcMetricsAccessor#getUsagePerClient(String, String, String, DateTime, DateTime)}
*/
@Test
public void testGetUsagePerClient() {
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");
UsagePerClientBean usage = accessor.getUsagePerClient("TestOrg", "TestApi", "1.0", from, to);
Map<String, Long> expectedData = new HashMap<>();
expectedData.put("TestClient", 16L);
expectedData.put("OtherClient", 2L);
Assert.assertEquals(expectedData, usage.getData());
}
use of io.apiman.manager.api.beans.metrics.UsagePerClientBean in project apiman by apiman.
the class EsMetricsAccessor method getUsagePerClient.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getUsagePerClient(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
*/
@SuppressWarnings("nls")
@Override
public UsagePerClientBean getUsagePerClient(String organizationId, String apiId, String version, DateTime from, DateTime to) {
UsagePerClientBean rval = new UsagePerClientBean();
try {
String query = "{" + " \"query\": {" + " \"bool\": {" + " \"filter\": [{" + " \"term\": {" + " \"apiOrgId\": \"{{apiOrgId}}\"" + " }" + " }, {" + " \"term\": {" + " \"apiId\": \"{{apiId}}\"" + " }" + " }, {" + " \"term\": {" + " \"apiVersion\": \"{{apiVersion}}\"" + " }" + " }, {" + " \"range\": {" + " \"requestStart\": {" + " \"gte\": \"{{from}}\"," + " \"lte\": \"{{to}}\"" + " }" + " }" + " }]" + " }" + " }," + " \"size\": 0," + " \"aggs\": {" + " \"usage_by_client\": {" + " \"terms\": {" + " \"field\": \"clientId\"" + " }" + " }" + " }" + "}";
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("usage_by_client")).getBuckets();
int counter = 0;
for (ParsedStringTerms.ParsedBucket entry : buckets) {
rval.getData().put(entry.getKeyAsString(), entry.getDocCount());
counter++;
if (counter > 5) {
break;
}
}
} catch (IOException e) {
LOGGER.error(e);
}
return rval;
}
Aggregations