How create a custom metric for a specific timerange

#1

Hi,
I have:

startA = "2016-01-01T02:00:00"
startB = "2016-01-02T12:00:00"

I’m trying to create a metric which returns 1 in the period [startA - startB] and 0
elsewhere.

How I can do it?

Thanks

0 Likes

#2

Are these hard-coded dates, or where are they coming from?

you can try an expression like this if they are hard-coded:

"expression": "dateTime('2016-01-01T02:00:00') <= now() && now( < dateTime('2016-01-02T12:00:00') ? 1 : 0" 

edit: as Bachir mentioned, start() is the correct function to use rather than now() :slight_smile:

0 Likes

#3

I would rather use start/end as now will gives current server time

now(): datetime static
Returns the current date time on the server.

Here is an example of defining a metric for type Facility:

var sm = SimpleMetric.make({
        "id": "IsInTimeRange",
        "name": "IsInTimeRange",
        "description": "Check if evaluation time is between two dates.",
        "srcType": "Facility",
        "expression": "dateTime('2016-01-01T02:00:00') <= start() && end() < dateTime('2016-01-02T12:00:00') ? 1 : 0"
    })

res = Facility.evalMetricsWithMetadata({
  "expressions":[ "IsInTimeRange"],
  "interval":"HOUR",
  "ids":["0001"],
  "start":"2015-12-31T05:00:00.000Z",
  "end":"2016-01-03T05:00:00.000Z"
  }, [sm]);


c3Viz(res)

You get this visualization:

1 Like