In a hierarchy of `Facility`

, lowest-level ones are where meters are attached that have (incrementing) registers with possibly different wraparound index values (`indexMax`

). For unrelated reasons, we cannot use `RegisterMeasurement`

and we use `registerReadInterpolator`

instead to compute the difference between indices at t+1 and t (consumption), like this:

`registerReadInterpolator(sum(sum(normalized.data.quantity)), 'GT', 0, indexMax)`

However, this EEF does not use modulo operation and can overflow, so our current solution is to bracket it using:

`E >= indexMax ? E - indexMax : (E < 0 ? E + indexMax : E)`

with `E`

being the previous expression (even though just `E >= indexMax ? E - indexMax : E`

might suffice).

The problem is when we want to aggregate (sum) at a higher-level `Facility`

, which in a test contains 3 lowest-level facilities, the test fails when an overflow occurs. We use `sum(sum(...`

in the argument to `registerReadInterpolator`

because there are other problems when there is only one `sum`

, and it seems that `registerReadInterpolator`

gets the sum of the 3 metersâ€™ measurements as input. But `indexMax`

is not the sum of these metersâ€™ `indexMax`

s: it is only one of them (in one case investigated, itâ€™s the first one).

Next I wrote a custom EEF:

```
type ProjectMFL mixes MetricFunctionLibrary {
diffR: function(incr: !Timeseries, rollover: Timeseries): !Timeseries js server
//diffR: function(incr: ![Timeseries], rollover: [Timeseries]): ![Timeseries] js server
}
```

and

```
c3Import('metadata', 'timeseries')
var log = C3.logger('Customer/ProjectMFL')
function diffR_1(ts, rollover) {
var data = ts.data()
var results = []
var next = data.slice(1).concat(data.slice(-1)[0])
try {
if (rollover) {
results = _.map(
_.zip.apply(_, [data, next, rollover]),
function(t) { return t[2] ? (t[1] - t[0]) % t[2] : t[1] - t[0] })
} else {
results = _.map(
_.zip.apply(_, [data, next]),
function(t) { return t[1] - t[0] })
}
} catch (e) {
log.error(e)
}
return Timeseries.makeNorm(NormTimeseriesDoubleSpec.make({
start: ts.start(),
end: ts.end(),
interval: ts.interval(),
data: Double.array(results),
unit: ts.unit()}))
}
function diffR(ts, rollover) {
if (ts.constructor === Array) {
if (rollover) {
return _.map(_.zip.apply(_, [ts, rollover]),
function(p) { return diffR(p[0], p[1]) })
} else {
return _.map(ts, function(ts) { return diffR(ts) })
}
} else {
return diffR_1(ts, rollover)
}
}
```

hoping to deal with both lowest- and higher-level `Facility`

(aggregation).

Now, for the simple metric/case, the following expression works:

`diffR(sum(normalized.data.quantity), identity(indexMax))`

and for a compound metric `IndividualElectricityConsumption`

, the following also works:

`diffR(IndividualElectricityMeasurements, IndexMax)`

where for `IndividualElectricityMeasurements`

the expression is `sum(sum(normalized.data.quantity))`

and for `IndexMax`

it is just `indexMax`

, but for the aggregate case, if we try a simple metric with:

`sum(diffR(sum(normalized.data.quantity), identity(indexMax)))`

we get a run-time error saying that `diffR`

got 3 arguments corresponding to the first parameter `incr`

. The commented-out API does not work/help either. So, again, the following does not throw engine errors:

`sum(diffR(sum(sum(normalized.data.quantity)), identity(indexMax)))`

but the test fails for the same reason (summing before computing the difference with a wraparound).

Similarly, a compound metric version with the expression:

`sum(diffR(IndividualElectricityMeasurements, IndexMax))`

gives an engine error `java.lang.NoSuchMethodError: c3.love.expr.bytecode.TsFuncLib.sum(Lc3/love/timeseries/LazyTimeseries;)`

. The same error with the following expression:

`sum(IndividualElectriityConsumption)`

(It is not impossible that something god messed up in all these attemptsâ€¦)

How should we do what we need?

Could someone please explain the polymorphic meaning of `sum`

in the context of non-/normalized, simple/compound metrics, `sum(...)`

vs. `sum(sum...)`

? We are using v7.6.1.

Is it possible to overload `diffR`

with multiple c3typ signatures (and the same name of a single Javascript function)?

Finally, it might help if there were built-in EEFs `tail`

(dropping the first value and, if necessary, pulling the rest forward in time) and `mod`

(modulo) to do this more efficiently.

Thanks,

Alex