package com.nitorcreations.willow.metrics;

import java.nio.charset.UnmappableCharacterException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.InternalTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;

public abstract class TagListMetric implements Metric {
  private final String tagPrefix;

  public TagListMetric(String tagPrefix) {
    this.tagPrefix = tagPrefix;
  }

  @Override
  public List<String> calculateMetric(Client client, HttpServletRequest req) {
    long start = Long.parseLong(req.getParameter("start"));
    long stop = Long.parseLong(req.getParameter("stop"));
    String[] types = req.getParameterValues("type");
    SearchResponse response = client.prepareSearch(MetricUtils.getIndexes(start, stop, client)).setTypes(types).setQuery(QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("timestamp").from(start).to(stop))).setSize(0).addAggregation(AggregationBuilders.terms("tags").field("tags").include(tagPrefix + "_.*")).get();
    ArrayList<String> ret = new ArrayList<>();
    InternalTerms agg = response.getAggregations().get("tags");
    for (Bucket next : agg.getBuckets()) {
      ret.add(next.getKey());
    }
    return ret;
  }
}
@@ -1347,24 +1347,92 @@
 ayList%3C%3E();%0A
+    if (response != null && response.getAggregations() != null) %7B%0A  
     Internal
@@ -1479,24 +1479,53 @@
 et(%22tags%22);%0A
+      if (agg != null) %7B%0A    
     for (Buc
@@ -1561,16 +1561,20 @@
 %7B%0A      
+    
 ret.add(
@@ -1585,24 +1585,42 @@
 .getKey());%0A
+        %7D%0A      %7D%0A
     %7D%0A    re

Remove nullpointer in some cases