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

brooklyn.enricher.RollingMeanEnricher Maven / Gradle / Ivy

package brooklyn.enricher;

import java.util.LinkedList;

import brooklyn.enricher.basic.AbstractTypeTransformingEnricher;
import brooklyn.entity.Entity;
import brooklyn.event.AttributeSensor;
import brooklyn.event.SensorEvent;


/**
* Transforms a sensor into a rolling average based on a fixed window size. This is useful for smoothing sample type metrics, 
* such as latency or CPU time
*/
public class RollingMeanEnricher extends AbstractTypeTransformingEnricher {
    private LinkedList values = new LinkedList();
    
    int windowSize;
    
    public RollingMeanEnricher(Entity producer, AttributeSensor source, AttributeSensor target,
            int windowSize) {
        super(producer, source, target);
        this.windowSize = windowSize;
    }
    
    /** @returns null when no data has been received or windowSize is 0 */
    public Double getAverage() {
        pruneValues();
        return values.size() == 0 ? null : sum(values) / values.size();
    }
    
    @Override
    public void onEvent(SensorEvent event) {
        values.addLast(event.getValue());
        pruneValues();
        entity.setAttribute((AttributeSensor)target, getAverage());
    }
    
    private void pruneValues() {
        while(windowSize > -1 && values.size() > windowSize) {
            values.removeFirst();
        }
    }
    
    private double sum(Iterable vals) {
        double result = 0;
        for (Number val : vals) {
            result += val.doubleValue();
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy