Write a metric that outputs net values from incremental / lifetime values


Say you have a MeasurementSeries of incremental / lifetime measurements (e.g an electric meter). You are asked to write a metric that returns the hourly electric consumption given the meter’s data.

One approach would be to use the rollingDiff function. It works fine as long as you don’t need the consumption for the first interval. To compute the first interval you need to get the data for start() - 1 interval. This could be achieved by adding an eval() call in the expression but this is not efficient and should be avoided when possible.

A cleaner approach is to use the window function as follow :

"name": "NetElectricConsumption",
"id": "NetElectricConsumption",
"srcType": "ServicePoint",
"path": "pointMeasurements",
"expression": "sum(max(normalized.data.quantity) - window('MAX', max(normalized.data.quantity), -1, 1))"

The max(normalized.data.quantity) could be factored as a SimpleMetric and the above metric would become a CompoundMetric.

One could also add some interpolate() calls to prevent missing data. A missing data interpreted as a zero could lead to undesirable spikes.

Last value of TS returned by `registerReadInterpolator` always 0

If you do need the rolling diff of the first interval, this worked for me:

"expression" : "start() == evalStart() ? NetElectricConsumption - window('MAX', NetElectricConsumption, -1, 1) : rollingDiff(NetElectricConsumption)"