Skip to content

MetricQueryBuilder

Builds and executes a query over a Metric’s projected contributions.

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.

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: {} } }

Measure with metrics

dimensions(dimensions): this

Groups the result by the given Dimensions, nesting one level of entries per Dimension in the order supplied.

DimensionInstance[]

Dimensions of this Metric to group by; a Metric declares at most four.

this


execute(): Promise<MetricReportEntry>

Runs the query and returns the aggregated report.

Promise<MetricReportEntry>

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.

when the focal Dimension, interval, or period has not been set.


focalDimension(focalDimension): this

Sets the Dimension whose distinct values are counted into each entry’s count.

DimensionInstance

A Dimension of this Metric.

this


interval(from, to): this

Restricts the query to contributions whose Event occurred between from and to, inclusive.

Date

Start of the interval.

Date

End of the interval.

this


period(period): this

Sets the granularity each entry is resolved to, so contributions are grouped per day, month, or year within the interval.

MetricQueryPeriod

Reporting granularity.

this