@SuppressWarnings("nls")
    @Override
    public ResponseStatsHistogramBean getResponseStats(String organizationId, String apiId,
            String version, HistogramIntervalType interval, DateTime from, DateTime to) {
        ResponseStatsHistogramBean rval = new ResponseStatsHistogramBean();
        Map<String, ResponseStatsDataPoint> index = generateHistogramSkeleton(rval, from, to, interval, ResponseStatsDataPoint.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\"," +
                    "                \"interval\": \"${interval}\"" +
                    "            }," +
                    "            \"aggs\": {" +
                    "                \"total_failures\": {" +
                    "                    \"filter\": {" +
                    "                        \"term\": {" +
                    "                            \"failure\": true" +
                    "                        }" +
                    "                    }" +
                    "                }," +
                    "                \"total_errors\": {" +
                    "                    \"filter\": {" +
                    "                        \"term\": {" +
                    "                            \"error\": true" +
                    "                        }" +
                    "                    }" +
                    "                }" +
                    "            }" +
                    "        }" +
                    "    }" +
                    "}";

            Map<String, String> params = new HashMap<>();
            params.put("from", formatDate(from));
            params.put("to", formatDate(to));
            params.put("apiOrgId", organizationId.replace('"', '_'));
            params.put("apiId", apiId.replace('"', '_'));
            params.put("apiVersion", version.replace('"', '_'));
            params.put("interval", interval.name());
            StrSubstitutor ss = new StrSubstitutor(params);
            query = ss.replace(query);

            Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
            SearchResult response = getEsClient().execute(search);
            MetricAggregation aggregations = response.getAggregations();
            DateHistogramAggregation aggregation = aggregations.getDateHistogramAggregation("histogram");
            if (aggregation != null) {
                List<DateHistogram> buckets = aggregation.getBuckets();
                for (DateHistogram entry : buckets) {
                    String keyAsString = entry.getTimeAsString();
                    if (index.containsKey(keyAsString)) {
                        FilterAggregation totalFailuresAgg = entry.getFilterAggregation("total_failures");
                        FilterAggregation totalErrorsAgg = entry.getFilterAggregation("total_errors");
                        long failures = totalFailuresAgg.getCount();
                        long errors = totalErrorsAgg.getCount();
                        ResponseStatsDataPoint point = index.get(keyAsString);
                        point.setTotal(entry.getCount());
                        point.setFailures(failures);
                        point.setErrors(errors);
                    }
                }
            }
        } catch (IOException e) {
            log.error(e);
        }

        return rval;
    }
@ SuppressWarnings ( "nls" ) @ Override public ResponseStatsHistogramBean getResponseStats ( String organizationId , String apiId , String version , HistogramIntervalType interval , DateTime from , DateTime to ) { ResponseStatsHistogramBean rval = new ResponseStatsHistogramBean ( ) ; Map < String , ResponseStatsDataPoint > index = generateHistogramSkeleton ( rval , from , to , interval , ResponseStatsDataPoint . 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\"," + "                \"interval\": \"${interval}\"" + "            }," + "            \"aggs\": {" + "                \"total_failures\": {" + "                    \"filter\": {" + "                        \"term\": {" + "                            \"failure\": true" + "                        }" + "                    }" + "                }," + "                \"total_errors\": {" + "                    \"filter\": {" + "                        \"term\": {" + "                            \"error\": true" + "                        }" + "                    }" + "                }" + "            }" + "        }" + "    }" + "}" ; Map < String , String > params = new HashMap < > ( ) ; params . put ( "from" , formatDate ( from ) ) ; params . put ( "to" , formatDate ( to ) ) ; params . put ( "apiOrgId" , organizationId . replace ( '"' , '_' ) ) ; params . put ( "apiId" , apiId . replace ( '"' , '_' ) ) ; params . put ( "apiVersion" , version . replace ( '"' , '_' ) ) ; params . put ( "interval" , interval . name ( ) ) ; StrSubstitutor ss = new StrSubstitutor ( params ) ; query = ss . replace ( query ) ; Search search = new Search . Builder ( query ) . addIndex ( INDEX_NAME ) . addType ( "request" ) . build ( ) ; SearchResult response = getEsClient ( ) . execute ( search ) ; MetricAggregation aggregations = response . getAggregations ( ) ; DateHistogramAggregation aggregation = aggregations . getDateHistogramAggregation ( "histogram" ) ; if ( aggregation != null ) { List < DateHistogram > buckets = aggregation . getBuckets ( ) ; for ( DateHistogram entry : buckets ) { String keyAsString = entry . getTimeAsString ( ) ; if ( index . containsKey ( keyAsString ) ) { FilterAggregation totalFailuresAgg = entry . getFilterAggregation ( "total_failures" ) ; FilterAggregation totalErrorsAgg = entry . getFilterAggregation ( "total_errors" ) ; long failures = totalFailuresAgg . getCount ( ) ; long errors = totalErrorsAgg . getCount ( ) ; ResponseStatsDataPoint point = index . get ( keyAsString ) ; point . setTotal ( entry . getCount ( ) ) ; point . setFailures ( failures ) ; point . setErrors ( errors ) ; } } } } catch ( IOException e ) { log . error ( e ) ; } return rval ; }
@see io.apiman.manager.api.core.IMetricsAccessor#getResponseStats(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, org.joda.time.DateTime, org.joda.time.DateTime)