com.nitorcreations.willow.metrics.FullMessageMetric 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.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import com.google.gson.Gson;
import com.nitorcreations.willow.messages.AbstractMessage;
import com.nitorcreations.willow.messages.MessageMapping;
import com.nitorcreations.willow.messages.metrics.MetricConfig;
public abstract class FullMessageMetric extends AbstractMetric {
protected SortedMap rawData;
private final Class type;
@SuppressWarnings("unchecked")
public FullMessageMetric() {
Class extends AbstractMetric> cls = (Class extends AbstractMetric>) getClass();
Type genSup = cls.getGenericSuperclass();
while (!(genSup instanceof ParameterizedType)) {
cls = (Class extends AbstractMetric>) cls.getSuperclass();
genSup = cls.getGenericSuperclass();
}
this.type = (Class) ((ParameterizedType)genSup).getActualTypeArguments()[0];
}
protected void readResponse(SearchResponse response) {
rawData = new TreeMap();
Gson gson = new Gson();
for (SearchHit next : response.getHits().getHits()) {
T nextMsg = gson.fromJson(next.getSourceAsString(), type);
nextMsg.setId(next.getId());
rawData.put(nextMsg.getTimestamp(), nextMsg);
}
}
@Override
public R calculateMetric(MetricConfig conf) {
SearchResponse response = executeQuery(conf, MessageMapping.map(type).lcName(), Collections.emptyList(), getCustomizer());
readResponse(response);
return processData(conf.getStart(), conf.getStop(), conf.getStep(), conf);
}
@Override
public String getType() {
return MessageMapping.map(this.type).lcName();
}
protected BuilderCustomizer getCustomizer() {
return null;
}
protected abstract R processData(long start, long stop, int step, MetricConfig conf);
}