@Override
    @SuppressWarnings("nls")
    public ResponseStatsPerClientBean getResponseStatsPerClient(String organizationId, String apiId,
            String version, DateTime from, DateTime to) {
        ResponseStatsPerClientBean rval = new ResponseStatsPerClientBean();

        try {
            String query =
                    "{" +
                    "    \"query\": {" +
                    "        \"bool\": {" +
                    "            \"filter\": [{" +
                    "                    \"term\": {" +
                    "                        \"apiOrgId\": \"{{apiOrgId}}\"" +
                    "                    }" +
                    "                }, {" +
                    "                    \"term\": {" +
                    "                        \"apiId\": \"{{apiId}}\"" +
                    "                    }" +
                    "                }, {" +
                    "                    \"term\": {" +
                    "                        \"apiVersion\": \"{{apiVersion}}\"" +
                    "                    }" +
                    "                }, {" +
                    "                    \"range\": {" +
                    "                        \"requestStart\": {" +
                    "                            \"gte\": \"{{from}}\"," +
                    "                            \"lte\": \"{{to}}\"" +
                    "                        }" +
                    "                    }" +
                    "                }" +
                    "            ]" +
                    "        }" +
                    "    }," +
                    "    \"aggs\": {" +
                    "        \"by_client\": {" +
                    "            \"terms\": {" +
                    "                \"field\": \"clientId\"" +
                    "            }," +
                    "            \"aggs\": {" +
                    "                \"total_failures\": {" +
                    "                    \"filter\": {" +
                    "                        \"term\": {" +
                    "                            \"failure\": true" +
                    "                        }" +
                    "                    }" +
                    "                }," +
                    "                \"total_errors\": {" +
                    "                    \"filter\": {" +
                    "                        \"term\": {" +
                    "                            \"error\": true" +
                    "                        }" +
                    "                    }" +
                    "                }" +
                    "            }" +
                    "        }" +
                    "    }," +
                    "    \"size\": 0" +
                    "}";

            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("by_client")).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;
    }
@see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsPerClient(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
manager/api/es/src/main/java/io/apiman/manager/api/es/EsMetricsAccessor.java