Search in sources :

Example 1 with UsageHistogramBean

use of io.apiman.manager.api.beans.metrics.UsageHistogramBean in project apiman by apiman.

the class TestMetricsAccessor method getUsage.

/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getUsage(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, java.util.Date, java.util.Date)
 */
@Override
public UsageHistogramBean getUsage(String organizationId, String apiId, String version, HistogramIntervalType interval, DateTime from, DateTime to) {
    UsageHistogramBean rval = new UsageHistogramBean();
    rval.getData().add(new UsageDataPoint("2015-06-01T00:00:00.000Z", 17));
    rval.getData().add(new UsageDataPoint("2015-06-01T01:00:00.000Z", 1));
    rval.getData().add(new UsageDataPoint("2015-06-01T02:00:00.000Z", 1));
    rval.getData().add(new UsageDataPoint("2015-06-01T03:00:00.000Z", 29));
    rval.getData().add(new UsageDataPoint("2015-06-01T04:00:00.000Z", 19));
    rval.getData().add(new UsageDataPoint("2015-06-01T05:00:00.000Z", 52));
    rval.getData().add(new UsageDataPoint("2015-06-01T06:00:00.000Z", 6));
    rval.getData().add(new UsageDataPoint("2015-06-01T07:00:00.000Z", 4));
    rval.getData().add(new UsageDataPoint("2015-06-01T08:00:00.000Z", 5));
    rval.getData().add(new UsageDataPoint("2015-06-01T09:00:00.000Z", 27));
    rval.getData().add(new UsageDataPoint("2015-06-01T10:00:00.000Z", 19));
    rval.getData().add(new UsageDataPoint("2015-06-01T11:00:00.000Z", 52));
    rval.getData().add(new UsageDataPoint("2015-06-01T12:00:00.000Z", 6));
    rval.getData().add(new UsageDataPoint("2015-06-01T13:00:00.000Z", 4));
    rval.getData().add(new UsageDataPoint("2015-06-01T14:00:00.000Z", 2));
    rval.getData().add(new UsageDataPoint("2015-06-01T15:00:00.000Z", 17));
    rval.getData().add(new UsageDataPoint("2015-06-01T16:00:00.000Z", 1));
    rval.getData().add(new UsageDataPoint("2015-06-01T17:00:00.000Z", 1));
    rval.getData().add(new UsageDataPoint("2015-06-01T18:00:00.000Z", 29));
    return rval;
}
Also used : UsageHistogramBean(io.apiman.manager.api.beans.metrics.UsageHistogramBean) UsageDataPoint(io.apiman.manager.api.beans.metrics.UsageDataPoint)

Example 2 with UsageHistogramBean

use of io.apiman.manager.api.beans.metrics.UsageHistogramBean in project apiman by apiman.

the class JdbcMetricsAccessor method getUsage.

/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getUsage(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@Override
public UsageHistogramBean getUsage(String organizationId, String apiId, String version, HistogramIntervalType interval, DateTime from, DateTime to) {
    UsageHistogramBean rval = new UsageHistogramBean();
    Map<Long, UsageDataPoint> index = MetricsAccessorHelper.generateHistogramSkeleton(rval, from, to, interval, UsageDataPoint.class, Long.class);
    try {
        QueryRunner run = new QueryRunner(ds);
        String gbColumn = groupByColumn(interval);
        // $NON-NLS-1$ //$NON-NLS-2$
        String sql = "SELECT " + gbColumn + ", count(*) FROM gw_requests WHERE api_org_id = ? AND api_id = ? AND api_version = ? AND rstart >= ? AND rstart < ? GROUP BY " + gbColumn;
        ResultSetHandler<UsageHistogramBean> handler = new UsageHistogramHandler(rval, index);
        run.query(sql, handler, organizationId, apiId, version, from.getMillis(), to.getMillis());
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return rval;
}
Also used : UsageHistogramBean(io.apiman.manager.api.beans.metrics.UsageHistogramBean) UsageDataPoint(io.apiman.manager.api.beans.metrics.UsageDataPoint) SQLException(java.sql.SQLException) QueryRunner(org.apache.commons.dbutils.QueryRunner) UsageHistogramHandler(io.apiman.manager.api.jdbc.handlers.UsageHistogramHandler)

Example 3 with UsageHistogramBean

use of io.apiman.manager.api.beans.metrics.UsageHistogramBean in project apiman by apiman.

the class JdbcMetricsAccessorTest method testGetUsage.

/**
 * Test method for {@link io.apiman.manager.api.jdbc.JdbcMetricsAccessor#getUsage(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, org.joda.time.DateTime, org.joda.time.DateTime)}.
 */
@Test
public void testGetUsage() {
    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");
    UsageHistogramBean usage = accessor.getUsage("TestOrg", "TestApi", "1.0", HistogramIntervalType.day, from, to);
    Assert.assertNotNull(usage);
    UsageDataPoint dataPoint = usage.getData().get(9);
    Assert.assertNotNull(dataPoint);
    Assert.assertEquals(2, dataPoint.getCount());
    Assert.assertEquals("2016-02-10T00:00:00.000Z", dataPoint.getLabel());
    Assert.assertEquals(9, usage.getData().get(22).getCount());
    Assert.assertEquals(6, usage.getData().get(23).getCount());
    Assert.assertEquals(0, usage.getData().get(24).getCount());
    // No data for "UnknownApi"
    usage = accessor.getUsage("TestOrg", "UnknownApi", "1.0", HistogramIntervalType.day, from, to);
    Assert.assertNotNull(usage);
    for (UsageDataPoint dp : usage.getData()) {
        Assert.assertEquals(0, dp.getCount());
    }
    from = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-01-01T00:00:00Z");
    to = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-12-31T00:00:00Z");
    usage = accessor.getUsage("TestOrg", "TestApi", "1.0", HistogramIntervalType.month, from, to);
    Assert.assertNotNull(usage);
    Assert.assertEquals(18, usage.getData().get(1).getCount());
    Assert.assertEquals(0, usage.getData().get(2).getCount());
    from = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-02-10T00:00:00Z");
    to = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-02-12T00:00:00Z");
    usage = accessor.getUsage("TestOrg", "TestApi", "1.0", HistogramIntervalType.hour, from, to);
    Assert.assertNotNull(usage);
    Assert.assertEquals(2, usage.getData().get(8).getCount());
    Assert.assertEquals(1, usage.getData().get(36).getCount());
    from = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-02-10T07:00:00Z");
    to = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).parseDateTime("2016-02-11T00:00:00Z");
    usage = accessor.getUsage("TestOrg", "TestApi", "1.0", HistogramIntervalType.minute, from, to);
    Assert.assertNotNull(usage);
    for (UsageDataPoint dp : usage.getData()) {
        if ("2016-02-10T08:56:00.000Z".equals(dp.getLabel())) {
            Assert.assertEquals(2, dp.getCount());
        } else {
            Assert.assertEquals(0, dp.getCount());
        }
    }
}
Also used : UsageHistogramBean(io.apiman.manager.api.beans.metrics.UsageHistogramBean) HashMap(java.util.HashMap) UsageDataPoint(io.apiman.manager.api.beans.metrics.UsageDataPoint) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with UsageHistogramBean

use of io.apiman.manager.api.beans.metrics.UsageHistogramBean in project apiman by apiman.

the class EsMetricsAccessor method getUsage.

/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getUsage(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@SuppressWarnings("nls")
@Override
public UsageHistogramBean getUsage(String organizationId, String apiId, String version, HistogramIntervalType interval, DateTime from, DateTime to) {
    UsageHistogramBean rval = new UsageHistogramBean();
    Map<String, UsageDataPoint> index = MetricsAccessorHelper.generateHistogramSkeleton(rval, from, to, interval, UsageDataPoint.class);
    try {
        String query = "{" + "    \"query\": {" + "        \"bool\": {" + "            \"filter\": [{" + "                    \"term\": {" + "                        \"apiOrgId\": \"{{apiOrgId}}\"" + "                    }" + "                }, {" + "                    \"term\": {" + "                        \"apiId\": \"{{apiId}}\"" + "                    }" + "                }, {" + "                    \"term\": {" + "                        \"apiVersion\": \"{{apiVersion}}\"" + "                    }" + "                }," + "                {" + "                    \"range\": {" + "                        \"requestStart\": {" + "                            \"gte\": \"{{from}}\"," + "                            \"lte\": \"{{to}}\"" + "                        }" + "                    }" + "                }" + "            ]" + "        }" + "    }," + "    \"size\": 0," + "    \"aggs\": {" + "        \"histogram\": {" + "            \"date_histogram\": {" + "                \"field\": \"requestStart\"," + "                \"calendar_interval\": \"{{interval}}\"" + "            }" + "        }" + "    }" + "}";
        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('"', '_'));
        params.put("interval", interval.name());
        SearchResponse response = this.doSearchTemplateRequest(query, params);
        ParsedDateHistogram aggregation = (ParsedDateHistogram) response.getAggregations().asMap().get("histogram");
        if (aggregation != null) {
            List<ParsedDateHistogram.ParsedBucket> buckets = (List<ParsedDateHistogram.ParsedBucket>) aggregation.getBuckets();
            for (ParsedDateHistogram.ParsedBucket entry : buckets) {
                String keyAsString = entry.getKeyAsString();
                if (index.containsKey(keyAsString)) {
                    index.get(keyAsString).setCount(entry.getDocCount());
                }
            }
        }
    } catch (IOException e) {
        LOGGER.error(e);
    }
    return rval;
}
Also used : UsageHistogramBean(io.apiman.manager.api.beans.metrics.UsageHistogramBean) UsageDataPoint(io.apiman.manager.api.beans.metrics.UsageDataPoint) HashMap(java.util.HashMap) ParsedDateHistogram(org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram) IOException(java.io.IOException) SearchResponse(org.elasticsearch.action.search.SearchResponse) List(java.util.List)

Aggregations

UsageDataPoint (io.apiman.manager.api.beans.metrics.UsageDataPoint)4 UsageHistogramBean (io.apiman.manager.api.beans.metrics.UsageHistogramBean)4 HashMap (java.util.HashMap)2 UsageHistogramHandler (io.apiman.manager.api.jdbc.handlers.UsageHistogramHandler)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 ParsedDateHistogram (org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.Test)1