Window Method in Metrics
The ExpressionEngineFunction#window can be used with metrics. The method takes in data as a time series and returns a time series where every value is the output of an aggregate function applied to a range or window of values about the current value.
Example
Consider a metric that calculates the number of times a light bulb is switched on or off during the previous 60 minutes.
The following data corresponding to a single light bulb is provided for the metric SwitchCount, at a QUARTER_HOUR granularity "2018-01-01T00:00:00" until "2018-01-01T02:30:00":
[1, 2, 3, 5, 2, 1, 0, 2, 4, 1],
To calculate the number of times a light bulb was switched on or off in the previous 60 minutes, define a metric SwitchCountPreviousHour with the following expression:
window('SUM', SwitchCount, -4, 4).
Assume this metric has no data beyond the data provided above.
Since the inputOffset is equal to the inputSpan, when the window is calculating the ith value, only the i-4 until i-1 data points are considered, excluding the ith input data point.
SwitchCountPreviousHour must be evaluated at a quarter hour interval to produce the desired output.
Evaluating SwitchCountPreviousHour at the QUARTER_HOUR interval from "2018-01-01T00:00:00" until "2018-01-01T03:30:00", results in the following output:
[0 + 0 + 0 + 0, 0 + 0 + 0 + 1, 0 + 0 + 1 + 2, 0 + 1 + 2 + 3, 1 + 2 + 3 + 5, 2 + 3 + 5 + 2, 3 + 5 + 2 + 1, 5 + 2 + 1 + 0, 2 + 1 + 0 + 2, 1 + 0 + 2 + 4, 0 + 2 + 4 + 1, 2 + 4 + 1 + 0, 4 + 1 + 0 + 0, 1 + 0 + 0 + 0, 0 + 0 + 0 + 0]
-> [0, 1, 3, 6, 11, 12, 11, 8, 5, 7, 7, 7, 5, 1, 0]
Consider a metric that is an indicator (1 or 0) if the light bulb fails in the next three days or not.
Given the following data for a metric BurntOut for a light bulb at a DAY granularity "2018-01-01" until "2018-01-14":
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
it is clear that the bulb burns out on the tenth day.
In the next example, evaluate if light bulb fails in the next 3 days.
To calculate the is a light bulb fails in the next three days, define a metric WillFailNextThreeDays with this expression:
window('MAX', BurntOut, 0, 3).
Assume this metric has no data beyond the data provided above. Now evaluate WillFailNextThreeDays at the DAY interval from "2018-01-01" to "2018-01-12", it evaluates this way:
[max(0, 0, 0), max(0, 0, 0), max(0, 0, 0), max(0, 0, 0), max(0, 0, 0), max(0, 0, 0), max(0, 0, 0), max(0, 0, 1), max(0, 1, 1), max(1, 1, 1), max(1, 1, 1), max(1, 1, 1)]
-> [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
That is, not until the 8th day does WillFailNextThreeDays indicate the light bulb can fail in the next 3 days.