Skip to content

Commit

Permalink
fixed an NPE when no metrics have yet been pushed to the metrics server
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Jul 23, 2015
1 parent 792d966 commit a817ce7
Showing 1 changed file with 63 additions and 53 deletions.
Expand Up @@ -134,12 +134,13 @@ public UsageHistogramBean getUsage(String organizationId, String serviceId, Stri
SearchResult response = getEsClient().execute(search);
MetricAggregation aggregations = response.getAggregations();
DateHistogramAggregation aggregation = aggregations.getDateHistogramAggregation("histogram");

List<DateHistogram> buckets = aggregation.getBuckets();
for (DateHistogram entry : buckets) {
String keyAsString = entry.getTimeAsString();
if (index.containsKey(keyAsString)) {
index.get(keyAsString).setCount(entry.getCount());
if (aggregation != null) {
List<DateHistogram> buckets = aggregation.getBuckets();
for (DateHistogram entry : buckets) {
String keyAsString = entry.getTimeAsString();
if (index.containsKey(keyAsString)) {
index.get(keyAsString).setCount(entry.getCount());
}
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -286,14 +287,16 @@ public UsagePerAppBean getUsagePerApp(String organizationId, String serviceId, S
Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
SearchResult response = getEsClient().execute(search);
MetricAggregation aggregations = response.getAggregations();
ApimanTermsAggregation termsAggregation = aggregations.getAggregation("usage_by_app", ApimanTermsAggregation.class); //$NON-NLS-1$
List<ApimanTermsAggregation.Entry> buckets = termsAggregation.getBuckets();
int counter = 0;
for (ApimanTermsAggregation.Entry entry : buckets) {
rval.getData().put(entry.getKey(), entry.getCount());
counter++;
if (counter > 5) {
break;
ApimanTermsAggregation aggregation = aggregations.getAggregation("usage_by_app", ApimanTermsAggregation.class); //$NON-NLS-1$
if (aggregation != null) {
List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
int counter = 0;
for (ApimanTermsAggregation.Entry entry : buckets) {
rval.getData().put(entry.getKey(), entry.getCount());
counter++;
if (counter > 5) {
break;
}
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -355,10 +358,12 @@ public UsagePerPlanBean getUsagePerPlan(String organizationId, String serviceId,
Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
SearchResult response = getEsClient().execute(search);
MetricAggregation aggregations = response.getAggregations();
ApimanTermsAggregation termsAggregation = aggregations.getAggregation("usage_by_plan", ApimanTermsAggregation.class); //$NON-NLS-1$
List<ApimanTermsAggregation.Entry> buckets = termsAggregation.getBuckets();
for (ApimanTermsAggregation.Entry entry : buckets) {
rval.getData().put(entry.getKey(), entry.getCount());
ApimanTermsAggregation aggregation = aggregations.getAggregation("usage_by_plan", ApimanTermsAggregation.class); //$NON-NLS-1$
if (aggregation != null) {
List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
for (ApimanTermsAggregation.Entry entry : buckets) {
rval.getData().put(entry.getKey(), entry.getCount());
}
}
} catch (IOException e) {
log.error(e);
Expand Down Expand Up @@ -431,19 +436,20 @@ public ResponseStatsHistogramBean getResponseStats(String organizationId, String
SearchResult response = getEsClient().execute(search);
MetricAggregation aggregations = response.getAggregations();
DateHistogramAggregation aggregation = aggregations.getDateHistogramAggregation("histogram");

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);
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) {
Expand Down Expand Up @@ -576,17 +582,19 @@ public ResponseStatsPerAppBean getResponseStatsPerApp(String organizationId, Str
Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
SearchResult response = getEsClient().execute(search);
MetricAggregation aggregations = response.getAggregations();
ApimanTermsAggregation termsAggregation = aggregations.getAggregation("by_app", ApimanTermsAggregation.class); //$NON-NLS-1$
List<ApimanTermsAggregation.Entry> buckets = termsAggregation.getBuckets();
int counter = 0;
for (ApimanTermsAggregation.Entry entry : buckets) {
ResponseStatsDataPoint point = new ResponseStatsDataPoint();
point.setTotal(entry.getCount());
rval.addDataPoint(entry.getKey(), entry.getCount(), entry.getFilterAggregation("total_failures").getCount(),
entry.getFilterAggregation("total_errors").getCount());
counter++;
if (counter > 10) {
break;
ApimanTermsAggregation aggregation = aggregations.getAggregation("by_app", ApimanTermsAggregation.class); //$NON-NLS-1$
if (aggregation != null) {
List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
int counter = 0;
for (ApimanTermsAggregation.Entry entry : buckets) {
ResponseStatsDataPoint point = new ResponseStatsDataPoint();
point.setTotal(entry.getCount());
rval.addDataPoint(entry.getKey(), entry.getCount(), entry.getFilterAggregation("total_failures").getCount(),
entry.getFilterAggregation("total_errors").getCount());
counter++;
if (counter > 10) {
break;
}
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -656,17 +664,19 @@ public ResponseStatsPerPlanBean getResponseStatsPerPlan(String organizationId, S
Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
SearchResult response = getEsClient().execute(search);
MetricAggregation aggregations = response.getAggregations();
ApimanTermsAggregation termsAggregation = aggregations.getAggregation("by_plan", ApimanTermsAggregation.class); //$NON-NLS-1$
List<ApimanTermsAggregation.Entry> buckets = termsAggregation.getBuckets();
int counter = 0;
for (ApimanTermsAggregation.Entry entry : buckets) {
ResponseStatsDataPoint point = new ResponseStatsDataPoint();
point.setTotal(entry.getCount());
rval.addDataPoint(entry.getKey(), entry.getCount(), entry.getFilterAggregation("total_failures").getCount(),
entry.getFilterAggregation("total_errors").getCount());
counter++;
if (counter > 10) {
break;
ApimanTermsAggregation aggregation = aggregations.getAggregation("by_plan", ApimanTermsAggregation.class); //$NON-NLS-1$
if (aggregation != null) {
List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
int counter = 0;
for (ApimanTermsAggregation.Entry entry : buckets) {
ResponseStatsDataPoint point = new ResponseStatsDataPoint();
point.setTotal(entry.getCount());
rval.addDataPoint(entry.getKey(), entry.getCount(), entry.getFilterAggregation("total_failures").getCount(),
entry.getFilterAggregation("total_errors").getCount());
counter++;
if (counter > 10) {
break;
}
}
}
} catch (IOException e) {
Expand Down

0 comments on commit a817ce7

Please sign in to comment.