Search in sources :

Example 1 with ClientUsagePerApiBean

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

the class JdbcMetricsAccessorTest method testGetClientUsagePerApi.

/**
 * Test for {@link JdbcMetricsAccessor#getClientUsagePerApi(String, String, String, DateTime, DateTime)}
 */
@Test
public void testGetClientUsagePerApi() {
    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");
    ClientUsagePerApiBean usage = accessor.getClientUsagePerApi("TestOrg", "TestClient", "1.0", from, to);
    Map<String, Long> expectedData = new HashMap<>();
    expectedData.put("TestApi", 15L);
    expectedData.put("OtherApi", 4L);
    Assert.assertEquals(expectedData, usage.getData());
}
Also used : HashMap(java.util.HashMap) ClientUsagePerApiBean(io.apiman.manager.api.beans.metrics.ClientUsagePerApiBean) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 2 with ClientUsagePerApiBean

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

the class JdbcMetricsAccessor method getClientUsagePerApi.

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

Example 3 with ClientUsagePerApiBean

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

the class ClientUsagePerApiHandler method handle.

/**
 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
 */
@Override
public ClientUsagePerApiBean handle(ResultSet rs) throws SQLException {
    ClientUsagePerApiBean rval = new ClientUsagePerApiBean();
    while (rs.next()) {
        String apiId = rs.getString(1);
        long count = rs.getLong(2);
        rval.getData().put(apiId, count);
    }
    return rval;
}
Also used : ClientUsagePerApiBean(io.apiman.manager.api.beans.metrics.ClientUsagePerApiBean)

Example 4 with ClientUsagePerApiBean

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

the class EsMetricsAccessor method getClientUsagePerApi.

/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getClientUsagePerApi(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@Override
@SuppressWarnings("nls")
public ClientUsagePerApiBean getClientUsagePerApi(String organizationId, String clientId, String version, DateTime from, DateTime to) {
    ClientUsagePerApiBean rval = new ClientUsagePerApiBean();
    try {
        String query = "{" + "    \"query\": {" + "        \"bool\": {" + "            \"filter\": [{" + "                    \"term\": {" + "                        \"clientOrgId\": \"{{clientOrgId}}\"" + "                    }" + "                }," + "                {" + "                    \"term\": {" + "                        \"clientId\": \"{{clientId}}\"" + "                    }" + "                }," + "                {" + "                    \"term\": {" + "                        \"clientVersion\": \"{{clientVersion}}\"" + "                    }" + "                }," + "                {" + "                    \"range\": {" + "                        \"requestStart\": {" + "                            \"gte\": \"{{from}}\"," + "                            \"lte\": \"{{to}}\"" + "                        }" + "                    }" + "                }" + "            ]" + "        }" + "    }," + "    \"size\": 0," + "    \"aggs\": {" + "        \"usage_by_api\": {" + "            \"terms\": {" + "                \"field\": \"apiId\"" + "            }" + "        }" + "    }" + "}";
        Map<String, Object> params = new HashMap<>();
        params.put("from", MetricsAccessorHelper.formatDate(from));
        params.put("to", MetricsAccessorHelper.formatDate(to));
        params.put("clientOrgId", organizationId.replace('"', '_'));
        params.put("clientId", clientId.replace('"', '_'));
        params.put("clientVersion", version.replace('"', '_'));
        SearchResponse response = this.doSearchTemplateRequest(query, params);
        ParsedStringTerms aggregation = response.getAggregations().get("usage_by_api");
        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 : HashMap(java.util.HashMap) List(java.util.List) IOException(java.io.IOException) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) ClientUsagePerApiBean(io.apiman.manager.api.beans.metrics.ClientUsagePerApiBean) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

ClientUsagePerApiBean (io.apiman.manager.api.beans.metrics.ClientUsagePerApiBean)4 HashMap (java.util.HashMap)2 ClientUsagePerApiHandler (io.apiman.manager.api.jdbc.handlers.ClientUsagePerApiHandler)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