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;
  }
}
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<>();
    if (response != null && response.getAggregations() != null) {
      InternalTerms agg = response.getAggregations().get("tags");
      if (agg != null) {
        for (Bucket next : agg.getBuckets()) {
          ret.add(next.getKey());
        }
      }
    }
    return ret;
  }
}
Remove nullpointer in some cases