Get roll up in scripted data source
-
Hi Phil,
I tried looking around but I'm still scratching my head,,,
Is it possible to query a context point in a scripted data source/meta datasource and get the values associated with it have a roll up applied to it? Ie get the last value every 15 mins etc... over the last day or whatever period required.
It's just not clicking for me atm...Thanks
Fox
-
Hi Fox,
Is it possible to query a context point in a scripted data source/meta datasource and get the values associated with it have a roll up applied to it?
Of course! You're probably looking for the PointValueQuery object. The help will tell you to
print(PointValueQuery);
to see its method signatures. Which should be,{ query([dataPointIds], long from, long to, boolean bookend, ScriptPointValueTimeCallback callback): void, rollupQuery([dataPointIds], long from, long to, ScriptPointValueTimeCallback callback, int rollupType, int rollupPeriods, int rollupPeriodType): void }
So we're looking for the rollup query. The queries will call the callback function with each value in time order for all series in the query. If one wants it sorted by series then time, one should loop over their points and do something like
var values = []; var valueCallback = function( value, index ) { //function definition in com.infiniteautomation.mango.db.query.PVTQueryCallback values.push(value); }; for( var point in CONTEXT_POINTS ) { PointValueQuery.query( [ this[point].getDataPointWrapper().getId() ], 0, CONTEXT.getRuntime(), true, valueCallback); } print(values);
he start timestamp is inclusive, the end timestamp is exclusive. The fourth argument, "bookend," refers to including the value of the point at the start time of the period (whether at or before that time) and the value held at the end of the period. Both of these values are presented with the timestamps of the period start and end, and may not actually exist in the DB. Bookends can be handy for charting.
But you're looking for rollups, which means we need the RollupType IDs and the PeriodType IDs to call the function. Let's just reference them where they're defined...
var values = []; PointValueQuery.rollupQuery( [1], 0, CONTEXT.getRuntime(), function( value, index ) { values.push(value); }, com.serotonin.m2m2.Common.Rollups.LAST, 15, com.serotonin.m2m2.Common.TimePeriods.MINUTES); print(values);
-
Excellent Phil! Much appreciated thanks!