Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package brooklyn.enricher;
 
 
 
Converts an absolute sensor into a delta sensor (i.e. the diff between the current and previous value), presented as a units/timeUnit based on the event timing
    private static final Logger LOG = LoggerFactory.getLogger(TimeWeightedDeltaEnricher.class);
    
    long lastTime = -1;
    int unitMillis;
    
    // default 1 second
    public static <T extends NumberTimeWeightedDeltaEnricher<T> getPerSecondDeltaEnricher(Entity producerSensor<T> sourceSensor<Doubletarget) {
        return new TimeWeightedDeltaEnricher<T>(producersourcetarget, 1000);
    }
    public TimeWeightedDeltaEnricher(Entity producerSensor<T> sourceSensor<Doubletargetint unitMillis) {
        this(producersourcetargetunitMillis, Functions.<Double>identity());
    }
    public TimeWeightedDeltaEnricher(Entity producerSensor<T> sourceSensor<Doubletargetint unitMillisClosure<DoublepostProcessor) {
        this(producersourcetargetunitMillis, GroovyJavaMethods.<Double,Double>functionFromClosure(postProcessor));
    }
    
    public TimeWeightedDeltaEnricher(Entity producerSensor<T> sourceSensor<Doubletargetint unitMillisFunction<Double,DoublepostProcessor) {
        super(producersourcetarget);
        this. = unitMillis;
        this. = postProcessor;
    }
    
    @Override
    public void onEvent(SensorEvent<T> event) {
        onEvent(eventevent.getTimestamp());
    }
    
    public void onEvent(SensorEvent<T> eventlong eventTime) {
        Number current = event.getValue();
        
        if (current == null) {
            // Can't compute a delta; 
            // don't assume current=zero because then things like requestCount->requestsPerSecond is negative!
            // instead assume same as last time, so delta == 0
            double deltaPostProcessed = .apply(0d);
            .setAttribute((AttributeSensor<Double>)deltaPostProcessed);
            if (.isTraceEnabled()) .trace("set {} to {}, {} -> {} at {}"new Object[] {thisdeltaPostProcessedcurrenteventTime});
            return;
        }
        
        if (eventTime > ) {
            if ( == null) {
                // cannot calculate time-based delta with a single value
                if (.isTraceEnabled()) .trace("{} received event but no last value so will not emit, null -> {} at {}"new Object[] {thiscurrenteventTime}); 
            } else {
                double duration = ( < 0) ?  : eventTime - ;
                double delta = (current.doubleValue() - .doubleValue()) / (duration / );
                double deltaPostProcessed = .apply(delta);
                .setAttribute((AttributeSensor<Double>)deltaPostProcessed);
                if (.isTraceEnabled()) .trace("set {} to {}, {} -> {} at {}"new Object[] {thisdeltaPostProcessedcurrenteventTime}); 
            }
             = current;
             = eventTime;
        }
    }
New to GrepCode? Check out our FAQ X