com.nitorcreations.willow.metrics.AbstractMetric Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of willow-servers Show documentation
Show all versions of willow-servers Show documentation
Willow operational servlets and servers
The newest version!
package com.nitorcreations.willow.metrics;
import java.util.Arrays;
import java.util.List;
import javax.inject.Inject;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import com.nitorcreations.willow.messages.metrics.MetricConfig;
public abstract class AbstractMetric implements Metric {
public static final BuilderCustomizer ONE = new OneResultBuilderCustomizer();
@Inject
protected Client client;
public SearchResponse executeQuery(Client client, MetricConfig conf, String type, List fields) {
return this.executeQuery(conf, type, fields, null);
}
public SearchResponse executeQuery(MetricConfig conf, String type, List fields, BuilderCustomizer customizer) {
SearchRequestBuilder builder = client.prepareSearch(MetricUtils.getIndexes(conf.getStart(), conf.getStop(), client))
.setTypes(type).setSearchType(SearchType.QUERY_AND_FETCH)
.setSize((int) (conf.getStop() - conf.getStart()) / 10);
for (String next : fields) {
builder.addField(next);
}
BoolQueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("timestamp").from(conf.getStart() - conf.getStep()).to(conf.getStop()).includeLower(false).includeUpper(true));
for (String tag : conf.getTags()) {
query = query.must(QueryBuilders.termQuery("tags", tag));
}
builder.setQuery(query);
if (customizer != null) {
customizer.customize(builder);
}
return builder.get();
}
@Override
public boolean hasData(MetricConfig conf) {
SearchResponse response = executeQuery(conf, getType(), Arrays.asList("timestamp"), AbstractMetric.ONE);
return response.getHits().getHits().length > 0;
}
public abstract String getType();
}