Skip to content

Metrics and Dimensions

A Metric counts the Events that an App publishes, and a Dimension says which Item each Event counts for. A handler queries a Metric for the count of an interval of time. An Event is counted after the call that published it has returned, when the queued work runs. A script that reads a Metric registers a projector for that work, as Tutorial: compose and measure does.

app.addMetric(key: string): MetricBuilder
metric.addEvent(event: EventBuilder): MetricBuilder

Declares a Metric: a count of the Events it names. Call addEvent once for each Event the Metric counts, and addDimension for what it counts by.

Argument Type Description
key string A lowercase letter, then letters and digits, 64 characters at most. An App uses a Metric key once.

Returns the Metric.

Modifier What it declares
addEvent An Event the Metric counts, as addEvent returns it. Returns this Metric.

In the sample wins counts the Event won of a deal.

pipeline.ts
const wonEvent = deal.addEvent('won');
const wins = app.addMetric('wins');
wins.addEvent(wonEvent);

Behavior

  • Each Event that a Metric names adds 1 to its count.
  • An Event is not counted when publish returns, nor when the Mutation that called it does. It is counted when the queued work runs.
  • An Event is counted once, however often the queued work runs.
  • An Event stays counted when its Item changes afterwards: a Metric counts Events, and not the state of an Item.
  • A Metric that names several Events counts each of them, an Event of an App that its App imports among them.

See: Metrics and projections, Measure with metrics, Tutorial: compose and measure, Sales pipeline with stages, AppBuilder.addMetric.

app.addMetricDimension(
key: string,
subject: (TemplateBuilder | UnionBuilder)[],
): MetricDimensionBuilder

Declares a Dimension: what a Metric counts by, as the rep of a deal or the garage of a van. A Dimension belongs to the App. Each Metric that counts by it attaches it with addDimension, and gives it the function that finds the Item an Event counts for.

Argument Type Description
key string A key as for a Metric. An App uses a Dimension key once.
subject (TemplateBuilder | UnionBuilder)[] The Templates and the Unions of the Items the Dimension counts by, as [garage] in the sample.

Returns the Dimension. The sample declares two: the garage a van belongs to, and the van itself.

metrics-app.ts
// What a Metric counts by: the garage a van belongs to, and the van itself.
const byGarage = app.addMetricDimension('garage', [garage]);
const byVan = app.addMetricDimension('van', [van]);

Behavior

  • Two Metrics that attach one Dimension each keep their own counts by it.
  • A Metric that attaches two Dimensions counts each Event by both.

See: Dimensions and positions, Measure with metrics, Tutorial: compose and measure, AppBuilder.addMetricDimension.

metric.addDimension(
dimension: MetricDimensionBuilder,
position: 1 | 2 | 3 | 4,
handler: MetricDimensionHandler,
): MetricBuilder
handler(
event: AlpyneEvent,
dimension: DimensionInstance,
ctx: ReadRuntimeInstance,
): Promise<ReadItem | string | undefined>
event.itemId: Identifier

Attaches a Dimension to the Metric, with the function that finds the Item an Event counts for: the second signature. event.itemId is the Identifier of the Item the Event was published against. The function returns an Item, the Identifier of one, or undefined.

Argument Type Description
dimension MetricDimensionBuilder A Dimension, as app.addMetricDimension returns it.
position 1 | 2 | 3 | 4 The place of the Dimension among the four of a Metric. Each Dimension of a Metric takes a place of its own.
handler MetricDimensionHandler The function to run: the second signature above.

Returns the Metric. In the sample an approval counts for the person its request points at.

leave.ts
const byRequester = app.addMetricDimension('requester', [person]);
approvals.addDimension(byRequester, 1, async (event, _dimension, ctx) => {
const item = await ctx.getItem(event.itemId);
const relation = item?.relations.requester;
return relation?.type === 'one' ? await relation.get() : undefined;
});

Behavior

  • An Event counts for the Item the handler returns, or for the Item whose Identifier it returns.
  • Where the handler returns undefined, the Event adds to the total and to the count of no Item: it is counted under the Identifier 00000000-0000-0000-0000-000000000000.
  • The handler runs when the Event is counted, and reads each Item as it is then.
  • It runs as the system: it reads an Item that no Grant gives the person who published the Event.

See: Dimensions and positions, Measure with metrics, Tutorial: compose and measure, MetricBuilder.addDimension.

current.metrics: Record<string, MetricInstance>
metric.dimensions: Record<string, DimensionInstance>
metric.getDimensionByPrn(prn: string): DimensionInstance
metric.prn: string
dimension.prn: string

The Metrics of an App by key, as current.metrics.work. A Metric holds the Dimensions it attaches in dimensions, each under its key, and getDimensionByPrn finds one by its PRN. A handler names a Dimension in a query. metric.prn and dimension.prn are the PRNs of a Metric and of a Dimension, as prn::fleet:metric/work and prn::fleet:dimension/garage.

In the sample a Query reads the Metric work and its Dimension garage, and returns the count of one month.

metrics-app.ts
const worked = app.addQuery('worked', async (input: MonthInput, current) => {
const metric = current.metrics.work;
const { from, to } = monthOf(input);
const query = metric.query('count').focalDimension(metric.dimensions.garage);
const report = await query.interval(from, to).period('month').execute();
return report.value ?? 0;
});

Behavior

  • A handler reads the Metrics of its App in a read execution and in a write execution, and the same count in both.
  • getDimensionByPrn gives the Dimension that dimensions holds under its key.
  • Two Metrics that attach one Dimension each hold it under its key, with its PRN.

See: Querying, Measure with metrics, ReadAppInstance.metrics.

metric.query(func: MetricQueryFunction): MetricQueryBuilder
query.focalDimension(
focalDimension: DimensionInstance,
): MetricQueryBuilder
query.dimensions(dimensions: DimensionInstance[]): MetricQueryBuilder

Begins a query: a count of the Events of the Metric in an interval of time. Each call on a query returns a new query, and execute runs it. Before it runs, a query names a focal Dimension, an interval and a period.

Argument Type Description
func MetricQueryFunction The function of the query, from the table below.
Function What the query gives
count The number of Events, as the value of each entry of the report.

Returns the query.

Modifier What it sets
focalDimension The focal Dimension: the one whose different values the report counts, as the count of each entry.
dimensions The Dimensions the report breaks the count down by, in order.

The sample counts the services of a month by garage, with the van as the focal Dimension.

metrics-app.ts
const metric = current.metrics.services;
const dimensions = metric.dimensions;
const { from, to } = monthOf(input);
const report = await metric
.query('count')
.focalDimension(dimensions.van)
.dimensions([dimensions.garage])
.interval(from, to)
.period('month')
.execute();

Behavior

  • A query that names two Dimensions gives an entry for each Item of the first, and in it an entry for each Item of the second.
  • A query that names no Dimensions gives the total alone.
  • A call on a query leaves that query as it was.

See: Querying, Measure with metrics, MetricInstance.query.

query.interval(from: Date, to: Date): MetricQueryBuilder
query.period(period: MetricQueryPeriod): MetricQueryBuilder

Sets the interval of time a query counts in, and the period of its report. Every query sets both. To count month by month, run one query for each month: in the sample from and to are the first and the last millisecond of one month.

Argument Type Description
from Date The first moment of the interval.
to Date The last moment of the interval.
period MetricQueryPeriod 'day', 'month' or 'year': the key of the entry of the report that holds the counts, as report.entries.month.

Each returns a new query.

run.ts
const from = new Date(Date.UTC(2020, month - 1, 1));
const to = new Date(Date.UTC(2020, month, 1) - 1);
const report = await metric
.query('count')
.focalDimension(byRep)
.dimensions([byRep])
.interval(from, to)
.period('month')
.execute();

Behavior

  • A query counts an Event by the time it was published, whenever the queued work counted it.
  • The interval includes both of its ends: an Event published at from or at to is counted.
  • execute throws for a query that sets no focal Dimension, no interval or no period.

See: Querying, Count things month by month, Sales pipeline with stages, MetricQueryBuilder.interval, MetricQueryBuilder.period.

query.execute(): Promise<MetricReportEntry>

Runs the query and returns its report. The report is one entry, a MetricReportEntry, for the whole interval. It holds the entry of the period, and that entry holds an entry for each Item of the first Dimension of the query, under the Identifier of the Item. Each entry has the members of the table.

Member What it holds
key The key of the entry in the entries it is one of: the period, or an Identifier. The key of the report is empty.
value The number of Events counted.
count The number of different values of the focal Dimension among those Events.
entries The entries one level down, each under its key.

In the sample entry is the entry of one garage: its value is the number of services, and its count the number of vans that had them.

metrics-app.ts
const entries = report.entries.month.entries;
const entry = entries[input.garage];
return { services: entry?.value ?? 0, vans: entry?.count ?? 0 };

Behavior

  • The value of an entry is the number of Events counted for it, and its count the number of different values of the focal Dimension among them.
  • An Item that no Event counts for has no entry.
  • The value of a report that counts no Event is null.

See: Querying, Measure with metrics, MetricQueryBuilder.execute.