# Problem defining metric with fileds from different sources

#1

Hello!
I have a ServicePoint that as two fields:

``````measurements: MeasurementSeries
motorData: [MotorData]
``````

into the measurements i have PointMeasurement for status (bool) and for position [0.0, 1.0]
into the motorData i have the field consumption (int)

i need to create a metric for the type Facility that evaluate the sum of the expressions [status == 1 ? consumption * position : 0] for every ServicePoints.

The problem is that if i define the different simple metrics for consumption, status and position for type Facility i donât obtain the correct value, ie:

``````                         consumption       status       position   _total_
ServicePoint 1          10              1             0.5        5
ServicePoint 2          10              1             0.5        5
ServicePoint 3          10              0              1         0
``````

if i call the metric on a Facility i want to obtain the value 10 (5+5+0)

so basically the idea is to evaluate the metric âby rowsâ instead âby columnsâ

my simple idea is to create SimpleMetrics for consumption, status and position and a CompoundMetric to evaluate the expression consumption * status * position but the problem is that the CompoundMetric only works for type ServicePoint and not for Facility (or better, it works but it multiplicate the aggregation of consumption, status and position. Instead i want to aggregate the multiplication between consumption, status and position over the several ServicePoints).

Any suggestions on how to do that??
Thank you

#2

try using `Facility.rollupMetrics` like:

``````Facility.rollupMetrics({
filter: facilityId,
expressions: ["consumption * status * position"],
start: "2017-01-01",
end: "2017-02-01",
interval: "DAY",
rollupFunc: "SUM"
});
``````

#3

Thanks Bachir,

it seems that the behavior of rollupMetrics keeps evaluate the multiplication AFTER the aggregation over the servicePoints and not before.

#4

Try the following, define an aggregation function at Facility

``````remix type Facility {
/**
* rollupMetrics call for facility-level metrics
*/
aggregateMetric: function(obj: !Obj, spec: TSEvalSpec, metric: Metric, name: !string): Timeseries js server
}
``````

In `Facility.js`

``````function aggregateMetric(obj, spec, metric, name) {
return ServicePoint.rollupMetric({
filter: Filter.eq('facility.id', obj.id),
expressions: [name],
start: spec.start,
end: spec.end,
interval: spec.interval,
rollupFunc: 'SUM'
});
}
``````

Then define the following `SimpleMetric`:

``````{
"id": "AggregateConsumptionByStatusByPosition_Facility",
"name": "AggregateConsumptionByStatusByPosition",
"srcType": "Facility",
"actionDecl": {
"action": "aggregateMetric('ConsumptionByStatusByPosition')"
}
}
``````

Now you can get the sum of all timesire of the metric `ConsumptionByStatusByPosition` (which you defined at ServicePoint level) with:

``````Facility.evalMetric({
expression: 'AggregateConsumptionByStatusByPosition',
id: 'facility01',
interval: ...,
start: ...,
end: ...
})
``````

#5

hei bachir, i tested your solution and works!!

One last question, now that i have:

• SimpleMetric: AggregateConsumptionByStatusByPosition for Facility,
• CompoundMetric ConsumptionByStatusByPosition for ServicePoint

Is the a way to unificate them?
Basically i tried to rename AggregateConsumptionByStatusByPosition into ConsumptionByStatusByPosition but when i call the metric on a Facility type it calls the CompoundMetric (the one for ServicePoint) and returns an error.

The error returned is:

âMetricEngine error :
Metric with name âStatusâ for type âFacilityâ not found! Check seed data!â

where âStatusâ is a metric called into the CompoundMetric ConsumptionByStatusByPosition defined on ServicePoint type

#6

Itâs seems that when renaming `AggregateConsumptionByStatusByPosition` to `ConsumptionByStatusByPosition` then calling eval metric at Facility level, it actually starts evaluating the `CompoundMetric` which depends on the simple metrics we defined for `ServicePoint` (i.e. `Status`, `Position`, `Consumption`) and this is why itâs not able to find `Status` for `Facility` as we didnât define one!