Implement a metric as rollup metric


#1

Can I implement a metric as a rollup metric in a simplemetric spec?

i.e. I want to implement weathernormalization for organization (not supported) as a rollup of the metrics results of the org’s underlying facilities


#2

One possibility is to define an actionDecl metric that will call a function defined on the orgs on which you sum the result of the rollups on the children facilities.
Here is an example (assuming you have a metric called FacilitiyMetric and defined at the facility level):

{
  "id": "FacilitiyMetricRollup_Organization",
  "name": "FacilitiyMetricRollup",
  "srcType": "Organization",
  "variables": [{
    "name": "targetMetric",
    "value": "FacilitiyMetric"
  }, {
    "name": "aggregationFunc",
    "value": "SUM"
  }],
  "actionDecl": {
    "action": "rollupFacilityMetric",
    "include": "id"
  }
}

In Organization.c3typ

remix type Organization {
  rollupFacilityMetric: function(obj: Obj, spec: TSEvalSpec, metric: Metric): Timeseries js server
}

In Organization.js

function rollupFacilityMetric(obj, spec, metric) {
  var targetMetric = metric.variables.find(function(variable) {
    return variable.name === "targetMetric";
  });
  var aggregationFunc = metric.variables.find(function(variable) {
    return variable.name === "aggregationFunc";
  });

  if (_.isUndefined(targetMetric) || _.isUndefined(aggregationFunc)) {
    // return an all missing timeseries 
    var length = DateTime.dateDiff(spec.start, spec.end, spec.interval);
    var missing = _.times(length, _.constant(100));
    return NormTimeseriesDouble.make({
      m_start: spec.start,
      m_end: spec.end,
      m_interval: spec.interval,
      m_missing: missing
    });
  }
  // return the rollup 
  return Facility.rollupMetric({
    rollupFunc: aggregationFunc.at("value"),
    expressions: [targetMetric.at("value")],
    filter: Filter.intersects("parentOrgs", [obj.id]),
    start: spec.start,
    end: spec.end,
    interval: spec.interval,
    continueOnError: true,
    limit: -1,
    cache: true
  });
}

#3

Is it possible to replace an actionDecl like the above (rollup) by something declarative/optimizable, i.e., expression or tsDecl?