Search in sources :

Example 1 with ResponseStatsPerPlanBean

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

the class JdbcMetricsAccessor method getResponseStatsPerPlan.

/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsPerPlan(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@Override
public ResponseStatsPerPlanBean getResponseStatsPerPlan(String organizationId, String apiId, String version, DateTime from, DateTime to) {
    try {
        QueryRunner run = new QueryRunner(ds);
        // $NON-NLS-1$
        String sql = "SELECT plan, resp_type, count(*) FROM gw_requests WHERE api_org_id = ? AND api_id = ? AND api_version = ? AND rstart >= ? AND rstart < ? GROUP BY plan, resp_type";
        ResultSetHandler<ResponseStatsPerPlanBean> handler = new ResponseStatsPerPlanHandler();
        return run.query(sql, handler, organizationId, apiId, version, from.getMillis(), to.getMillis());
    } catch (SQLException e) {
        e.printStackTrace();
        return new ResponseStatsPerPlanBean();
    }
}
Also used : ResponseStatsPerPlanBean(io.apiman.manager.api.beans.metrics.ResponseStatsPerPlanBean) SQLException(java.sql.SQLException) ResponseStatsPerPlanHandler(io.apiman.manager.api.jdbc.handlers.ResponseStatsPerPlanHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 2 with ResponseStatsPerPlanBean

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

the class ResponseStatsPerPlanHandler method handle.

/**
 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
 */
@Override
public ResponseStatsPerPlanBean handle(ResultSet rs) throws SQLException {
    ResponseStatsPerPlanBean rval = new ResponseStatsPerPlanBean();
    while (rs.next()) {
        String plan = rs.getString(1);
        if (plan == null) {
            continue;
        }
        String rtype = rs.getString(2);
        long count = rs.getLong(3);
        ResponseStatsDataPoint dataPoint = rval.getData().get(plan);
        if (dataPoint == null) {
            dataPoint = new ResponseStatsDataPoint();
            rval.getData().put(plan, dataPoint);
        }
        if (rtype == null) {
            dataPoint.setTotal(dataPoint.getErrors() + dataPoint.getFailures() + count);
        } else if (rtype.equals("failure")) {
            // $NON-NLS-1$
            dataPoint.setTotal(dataPoint.getTotal() + count);
            dataPoint.setFailures(count);
        } else if (rtype.equals("error")) {
            // $NON-NLS-1$
            dataPoint.setTotal(dataPoint.getTotal() + count);
            dataPoint.setErrors(count);
        }
    }
    return rval;
}
Also used : ResponseStatsPerPlanBean(io.apiman.manager.api.beans.metrics.ResponseStatsPerPlanBean) ResponseStatsDataPoint(io.apiman.manager.api.beans.metrics.ResponseStatsDataPoint)

Example 3 with ResponseStatsPerPlanBean

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

the class JdbcMetricsAccessorTest method testGetResponseStatsPerPlan.

/**
 * Test for {@link JdbcMetricsAccessor#getResponseStatsPerPlan(String, String, String, DateTime, DateTime)}
 */
@Test
public void testGetResponseStatsPerPlan() {
    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");
    ResponseStatsPerPlanBean stats = accessor.getResponseStatsPerPlan("TestOrg", "TestApi", "1.0", from, to);
    ResponseStatsDataPoint dataPoint = stats.getData().get("Gold");
    Assert.assertNotNull(dataPoint);
    Assert.assertEquals(8, dataPoint.getTotal());
    Assert.assertEquals(1, dataPoint.getFailures());
    Assert.assertEquals(1, dataPoint.getErrors());
    dataPoint = stats.getData().get("Silver");
    Assert.assertNotNull(dataPoint);
    Assert.assertEquals(10, dataPoint.getTotal());
    Assert.assertEquals(2, dataPoint.getFailures());
    Assert.assertEquals(0, dataPoint.getErrors());
}
Also used : ResponseStatsPerPlanBean(io.apiman.manager.api.beans.metrics.ResponseStatsPerPlanBean) ResponseStatsDataPoint(io.apiman.manager.api.beans.metrics.ResponseStatsDataPoint) HashMap(java.util.HashMap) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with ResponseStatsPerPlanBean

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

the class EsMetricsAccessor method getResponseStatsPerPlan.

/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsPerPlan(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@Override
@SuppressWarnings("nls")
public ResponseStatsPerPlanBean getResponseStatsPerPlan(String organizationId, String apiId, String version, DateTime from, DateTime to) {
    ResponseStatsPerPlanBean rval = new ResponseStatsPerPlanBean();
    try {
        String query = "{" + "    \"query\": {" + "        \"bool\": {" + "            \"filter\": [{" + "                \"term\": {" + "                    \"apiOrgId\": \"{{apiOrgId}}\"" + "                }" + "            }, {" + "                \"term\": {" + "                    \"apiId\": \"{{apiId}}\"" + "                }" + "            }, {" + "                \"term\": {" + "                    \"apiVersion\": \"{{apiVersion}}\"" + "                }" + "            }, {" + "                \"range\": {" + "                    \"requestStart\": {" + "                        \"gte\": \"{{from}}\"," + "                        \"lte\": \"{{to}}\"" + "                    }" + "                }" + "            }]" + "        }" + "    }," + "    \"size\": 0," + "    \"aggs\": {" + "        \"by_plan\": {" + "            \"terms\": {" + "                \"field\": \"planId\"" + "            }," + "            \"aggs\": {" + "                \"total_failures\": {" + "                    \"filter\": {" + "                        \"term\": {" + "                            \"failure\": true" + "                        }" + "                    }" + "                }," + "                \"total_errors\": {" + "                    \"filter\": {" + "                        \"term\": {" + "                            \"error\": true" + "                        }" + "                    }" + "                }" + "            }" + "        }" + "    }" + "}";
        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("by_plan");
        if (aggregation != null) {
            List<ParsedStringTerms.ParsedBucket> buckets = (List<ParsedStringTerms.ParsedBucket>) aggregation.getBuckets();
            int counter = 0;
            for (ParsedStringTerms.ParsedBucket entry : buckets) {
                rval.addDataPoint(entry.getKeyAsString(), entry.getDocCount(), ((ParsedFilter) entry.getAggregations().get("total_failures")).getDocCount(), ((ParsedFilter) entry.getAggregations().get("total_errors")).getDocCount());
                counter++;
                if (counter > 10) {
                    break;
                }
            }
        }
    } catch (IOException e) {
        LOGGER.error(e);
    }
    return rval;
}
Also used : HashMap(java.util.HashMap) Aggregations(org.elasticsearch.search.aggregations.Aggregations) IOException(java.io.IOException) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) UsageDataPoint(io.apiman.manager.api.beans.metrics.UsageDataPoint) ResponseStatsDataPoint(io.apiman.manager.api.beans.metrics.ResponseStatsDataPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ResponseStatsPerPlanBean(io.apiman.manager.api.beans.metrics.ResponseStatsPerPlanBean) List(java.util.List)

Aggregations

ResponseStatsPerPlanBean (io.apiman.manager.api.beans.metrics.ResponseStatsPerPlanBean)4 ResponseStatsDataPoint (io.apiman.manager.api.beans.metrics.ResponseStatsDataPoint)3 HashMap (java.util.HashMap)2 UsageDataPoint (io.apiman.manager.api.beans.metrics.UsageDataPoint)1 ResponseStatsPerPlanHandler (io.apiman.manager.api.jdbc.handlers.ResponseStatsPerPlanHandler)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