All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nitorcreations.willow.metrics.FullMessageSimpleMetric Maven / Gradle / Ivy

The newest version!
package com.nitorcreations.willow.metrics;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import com.nitorcreations.willow.messages.AbstractMessage;
import com.nitorcreations.willow.messages.metrics.MetricConfig;
import com.nitorcreations.willow.messages.metrics.TimePoint;

public abstract class FullMessageSimpleMetric extends FullMessageMetric>> {

  @Override
  public Collection> processData(long start, long stop, int step, MetricConfig conf) {
    List> ret = new ArrayList>();
    if (rawData.isEmpty()) {
      return ret;
    }
    List retTimes = new LinkedList();
    long curr = stop;
    while (curr > start) {
      retTimes.add(0, Long.valueOf(curr));
      curr -= step;
    }
    rawData = rawData.tailMap(curr - step + 1);
    for (Long nextTime : retTimes) {
      long afterNextTime = nextTime + 1;
      Collection preceeding = rawData.headMap(afterNextTime).values();
      rawData = rawData.tailMap(afterNextTime);
      List tmplist = new ArrayList(preceeding);
      if (tmplist.isEmpty()) {
        ret.add(new TimePoint(nextTime.longValue(), fillMissingValue()));
        continue;
      }
      ret.add(new TimePoint(nextTime.longValue(), estimateValue(tmplist, nextTime, step)));
    }
    return ret;
  }
  protected abstract Y fillMissingValue();

  protected Y estimateValue(List preceeding, long stepTime, long stepLen) {
    return calculateValue(filterGroupedData(groupData(preceeding)), stepTime, stepLen);
  }
  protected HashMap> groupData(List preceeding) {
    HashMap> groupedData = new HashMap<>();
    for (T next : preceeding) {
      String host = "" + getGroupBy(next);
      List groupMessages = groupedData.get(host);
      if (groupMessages == null) {
        groupMessages = new ArrayList<>();
        groupedData.put(host, groupMessages);
      }
      groupMessages.add(next);
    }
    return groupedData;
  }

  protected abstract String getGroupBy(T message);
  protected abstract List filterGroupedData(HashMap> groupedData);
  protected abstract Y calculateValue(List values, long stepTime, long stepLen);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy