Evaluate metric in a variable interval

#1

Hi, have we a way to evaluate a timeSeries taking the interval directly from the UI? I mean, I want to calculate the sum of consumption stored in the db, but if I select interval MONTH , start = 2017-01-14 , end = 2017-02-10 I want that the system calculate the consumption for January from the 14th to 31th, for February from the 1st to the 10th. Can I use start() and end() inside the expression of the metric?

0 Likes

#2

Yes, in metrics expression you can use start() and end() or any other function defined in ExpressionEngineFunction.

The interval is part of the EvalMetricSpec you can set it from the UI, I don’t see a problem.
If you are looking to use a variable inside you metric expression then check this Binding variables

0 Likes

#3

Hi, I’m trying to use the eval engine function to evaluate the metric only during the interval setted at runtime

If I try to pass to the eval a fixed start / end date it seems to work fine, for example
sum( eval( 'DAY' , $myMetric , dateTime('2017-01-28') , dateTime('2017-02-04') ) )

If I want to substitute dateTime(‘2017-01-28’) with start() and dateTime(‘2017-02-04’) with end() how can I do? Using :
sum( eval( 'DAY' , $myMetric , start() , end() ) )
It doesn’t work because start() and end() are --> a timeseries where value of every datapoint is the number of milliseconds from UNIX epoch, based on that datapoint’s START/END date .

How can I convert those to the value requested by eval function?

Thanks,
Laura

0 Likes

#4

Hi all,
no suggestion to solve my problem?

Thanks,
Laura

0 Likes

#5

Use the following in expression

evalStart()
evalEnd()

Check this post also How to get 'evalStart', 'evalEnd', 'evalInterval' in expression?

0 Likes

#6

Hi, I’m trying to use

sum( eval( ‘DAY’ , $myMetric , evalStart() , evalEnd() ) )

But it doesn’t work…I tryied even to use a variable, like

newSimple = SimpleMetric.make({
“id”: “MySimpleMetric”,
“name”: “MySimpleMetric”,
“srcType”: “Facility”,
“expression”: “sum(eval(‘DAY’,$MyMetric,varStart,varEnd))”,
“variables”: [{
“name”: “varStart”,
“value” : “evalStart()”
},{
“name”: “varEnd”,
“value” : “evalEnd()”
}]
})

But without results…any suggestion? Maybe only changing a little thing it will works, but for now I’m blocked…

Thanks
Laura

0 Likes

#7

In order to evaluate a metric you must CREATE (put it in the db) not MAKE (put it in local, e.g. browser, memory).
You would want something like:

SimpleMetric.create({
“id”: “MySimpleMetric”,
“name”: “MySimpleMetric”,
“srcType”: “Facility”,
“expression”: “sum(eval(‘DAY’,$MyMetric,varStart,varEnd))”,
“variables”: [{
“name”: “varStart”,
“value” : “evalStart()”
},{
“name”: “varEnd”,
“value” : “evalEnd()”
}]
})

var result = Facility.evalMetric({expression: 'MySimpleMetric', id: 'FACILITY_ID', ...etc.})
0 Likes

#8

Hi, thanks but I’m “making” (not “creating”) the metric because I can evaluate it (using “evalMetricsWithMetadata”) directly in the browser at runtime, without store it: is not the same for “test purpose”?
Anyway, even using “create” I have the same problem, the system seems ignore the variables evalStart and evalEnd. For now I have “bypassed” this problem using javascript code, even if I would like to find a way to define this logic inside the metric.

Thanks,
Laura

0 Likes

#9

try to use evalXYZ() directly on your expression, no need to bind them into variables:

"expression": "sum(eval(‘DAY’, MetricName, evalStart(), evalEnd()))”
0 Likes