Search in sources :

Example 1 with UsagePerClientBean

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

Example 2 with UsagePerClientBean

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

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

Example 4 with UsagePerClientBean

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

Example 5 with UsagePerClientBean

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;
}
Also used : HashMap(java.util.HashMap) 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) List(java.util.List) UsagePerClientBean(io.apiman.manager.api.beans.metrics.UsagePerClientBean)

Aggregations

UsagePerClientBean (io.apiman.manager.api.beans.metrics.UsagePerClientBean)5 HashMap (java.util.HashMap)2 ResponseStatsDataPoint (io.apiman.manager.api.beans.metrics.ResponseStatsDataPoint)1 UsageDataPoint (io.apiman.manager.api.beans.metrics.UsageDataPoint)1 UsagePerClientHandler (io.apiman.manager.api.jdbc.handlers.UsagePerClientHandler)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 ParsedStringTerms (org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.Test)1