Search in sources :

Example 1 with UsagePerPlanBean

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;
}
Also used : UsagePerPlanBean(io.apiman.manager.api.beans.metrics.UsagePerPlanBean)

Example 2 with UsagePerPlanBean

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());
}
Also used : UsagePerPlanBean(io.apiman.manager.api.beans.metrics.UsagePerPlanBean) HashMap(java.util.HashMap) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with UsagePerPlanBean

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;
}
Also used : UsagePerPlanBean(io.apiman.manager.api.beans.metrics.UsagePerPlanBean) HashMap(java.util.HashMap) Aggregations(org.elasticsearch.search.aggregations.Aggregations) IOException(java.io.IOException) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) SearchResponse(org.elasticsearch.action.search.SearchResponse) List(java.util.List)

Example 4 with UsagePerPlanBean

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();
    }
}
Also used : UsagePerPlanBean(io.apiman.manager.api.beans.metrics.UsagePerPlanBean) SQLException(java.sql.SQLException) UsagePerPlanHandler(io.apiman.manager.api.jdbc.handlers.UsagePerPlanHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 5 with 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;
}
Also used : UsagePerPlanBean(io.apiman.manager.api.beans.metrics.UsagePerPlanBean)

Aggregations

UsagePerPlanBean (io.apiman.manager.api.beans.metrics.UsagePerPlanBean)5 HashMap (java.util.HashMap)2 UsagePerPlanHandler (io.apiman.manager.api.jdbc.handlers.UsagePerPlanHandler)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 QueryRunner (org.apache.commons.dbutils.QueryRunner)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 Aggregations (org.elasticsearch.search.aggregations.Aggregations)1 ParsedStringTerms (org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.Test)1