MetricsQueue { public boolean offer(RequestMetric metric) { queue.offer(metric); while (queue.size() > MAX_ENTRIES) { queue.removeFirst(); } StatusCodeGroup statusCode = StatusCodeGroup.valueOf(metric.getStatusCode()); if (!statistics.containsKey(statusCode)) { statistics.putIfAbsent(statusCode, new RequestMetricSummary()); } RequestMetricSummary totals = statistics.get(statusCode); long time = metric.getRequestCompleteTime() - metric.getRequestStartTime(); totals.add(time, time < metric.getUriGroup().getLimit(), metric.getNrOfDatabaseQueries(), metric.getDatabaseQueryTime(), metric.getQueries().stream().filter(QueryMetric::isIntolerable).count(), metric.getQueries().stream().filter(QueryMetric::isIntolerable).mapToLong(q -> q.getRequestCompleteTime()-q.getRequestStartTime()).sum() ); return true; } MetricsQueue(); @JsonCreator  MetricsQueue(@JsonProperty("lastRequests") ConcurrentLinkedDeque<RequestMetric> queue,
                        @JsonProperty("detailed") Map<StatusCodeGroup, RequestMetricSummary> statistics); boolean offer(RequestMetric metric); Map<StatusCodeGroup, RequestMetricSummary> getDetailed(); ConcurrentLinkedDeque<RequestMetric> getLastRequests(); @JsonProperty("summary") RequestMetricSummary getTotals(); static final int MAX_ENTRIES; }
@Test void offer() { queue = new MetricsQueue(); RequestMetric metric = RequestMetric.start("uri",uriGroup,0); metric.addQuery(new QueryMetric("query1", 0, 2, true)); metric.addQuery(new QueryMetric("query1", 0, 2, true)); metric.addQuery(new QueryMetric("query2", 0, 2, false)); metric.stop(200, 2); queue.offer(metric); RequestMetricSummary totals = queue.getTotals(); assertEquals(3, totals.getDatabaseQueryCount()); assertEquals(2, totals.getDatabaseIntolerableQueryCount()); }