use of io.apiman.manager.api.beans.metrics.UsagePerPlanBean in project apiman by apiman.
the class TestMetricsAccessor method getUsagePerPlan.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getUsagePerPlan(java.lang.String, java.lang.String, java.lang.String, java.util.Date, java.util.Date)
*/
@Override
public UsagePerPlanBean getUsagePerPlan(String organizationId, String apiId, String version, DateTime from, DateTime to) {
UsagePerPlanBean rval = new UsagePerPlanBean();
rval.getData().put("Gold", 120384L);
rval.getData().put("Silver", 921263L);
rval.getData().put("Platinum", 726392L);
return rval;
}
use of io.apiman.manager.api.beans.metrics.UsagePerPlanBean in project apiman by apiman.
the class JdbcMetricsAccessorTest method testGetUsagePerPlan.
/**
* Test for {@link JdbcMetricsAccessor#getUsagePerPlan(String, String, String, DateTime, DateTime)}
*/
@Test
public void testGetUsagePerPlan() {
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");
UsagePerPlanBean usage = accessor.getUsagePerPlan("TestOrg", "TestApi", "1.0", from, to);
Map<String, Long> expectedData = new HashMap<>();
expectedData.put("Silver", 10L);
expectedData.put("Gold", 8L);
Assert.assertEquals(expectedData, usage.getData());
}
use of io.apiman.manager.api.beans.metrics.UsagePerPlanBean in project apiman by apiman.
the class EsMetricsAccessor method getUsagePerPlan.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getUsagePerPlan(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
*/
@SuppressWarnings("nls")
@Override
public UsagePerPlanBean getUsagePerPlan(String organizationId, String apiId, String version, DateTime from, DateTime to) {
UsagePerPlanBean rval = new UsagePerPlanBean();
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_plan\": {" + " \"terms\": {" + " \"field\": \"planId\"" + " }" + " }" + " }" + "}";
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);
Aggregations aggregations = response.getAggregations();
// $NON-NLS-1$
ParsedStringTerms aggregation = aggregations.get("usage_by_plan");
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;
}
use of io.apiman.manager.api.beans.metrics.UsagePerPlanBean in project apiman by apiman.
the class JdbcMetricsAccessor method getUsagePerPlan.
/**
* @see io.apiman.manager.api.core.IMetricsAccessor#getUsagePerPlan(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
*/
@Override
public UsagePerPlanBean getUsagePerPlan(String organizationId, String apiId, String version, DateTime from, DateTime to) {
try {
QueryRunner run = new QueryRunner(ds);
// $NON-NLS-1$
String sql = "SELECT plan, count(*) FROM gw_requests WHERE api_org_id = ? AND api_id = ? AND api_version = ? AND rstart >= ? AND rstart < ? GROUP BY plan";
ResultSetHandler<UsagePerPlanBean> handler = new UsagePerPlanHandler();
return run.query(sql, handler, organizationId, apiId, version, from.getMillis(), to.getMillis());
} catch (SQLException e) {
e.printStackTrace();
return new UsagePerPlanBean();
}
}
use of io.apiman.manager.api.beans.metrics.UsagePerPlanBean in project apiman by apiman.
the class UsagePerPlanHandler method handle.
/**
* @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
*/
@Override
public UsagePerPlanBean handle(ResultSet rs) throws SQLException {
UsagePerPlanBean rval = new UsagePerPlanBean();
while (rs.next()) {
String planId = rs.getString(1);
long count = rs.getLong(2);
if (planId != null) {
rval.getData().put(planId, count);
}
}
return rval;
}
Aggregations