MetricQueryBuilder
Builds and executes a query over a Metric’s projected contributions.
Remarks
Section titled “Remarks”Each method returns a new builder and leaves the original unchanged. A query needs a focal
Dimension, an interval, and a period before execute can run it.
Example
Section titled “Example”Counting distinct employees per moment across a year
const report = await instance.execute('read', async (_current, ctx) => { const metric = ctx.apps.pyn.metrics.inMoment; return await metric .query('count') .focalDimension(metric.getDimensionByPrn('prn::pyn:dimension/employee')) .dimensions([metric.getDimensionByPrn('prn::pyn:dimension/moment')]) .interval(new Date('2024-01-01'), new Date('2024-12-31')) .period('day') .execute();});const byMoment = report.entries['day'].entries; // => { [momentId]: { key, count, value, entries: {} } }Methods
Section titled “Methods”dimensions()
Section titled “dimensions()”dimensions(
dimensions):this
Groups the result by the given Dimensions, nesting one level of entries per Dimension in the order supplied.
Parameters
Section titled “Parameters”dimensions
Section titled “dimensions”Dimensions of this Metric to group by; a Metric declares at most four.
Returns
Section titled “Returns”this
execute()
Section titled “execute()”execute():
Promise<MetricReportEntry>
Runs the query and returns the aggregated report.
Returns
Section titled “Returns”Promise<MetricReportEntry>
Remarks
Section titled “Remarks”The root entry carries the totals for the whole interval. Beneath it, one entry keyed by
the period holds an entry per value of the first Dimension, and so on for each further
Dimension. In every entry, value aggregates the projected contributions under the query’s
function and count is the number of distinct focal Dimension values.
Throws
Section titled “Throws”when the focal Dimension, interval, or period has not been set.
focalDimension()
Section titled “focalDimension()”focalDimension(
focalDimension):this
Sets the Dimension whose distinct values are counted into each entry’s count.
Parameters
Section titled “Parameters”focalDimension
Section titled “focalDimension”A Dimension of this Metric.
Returns
Section titled “Returns”this
interval()
Section titled “interval()”interval(
from,to):this
Restricts the query to contributions whose Event occurred between from and to,
inclusive.
Parameters
Section titled “Parameters”Date
Start of the interval.
Date
End of the interval.
Returns
Section titled “Returns”this
period()
Section titled “period()”period(
period):this
Sets the granularity each entry is resolved to, so contributions are grouped per day, month, or year within the interval.
Parameters
Section titled “Parameters”period
Section titled “period”Reporting granularity.
Returns
Section titled “Returns”this

