Skip to content

Metrics and projections

A Metric is a time-series aggregation declared on an App with addMetric. It selects one or more Events, and every committed Event it selects contributes one point at the Event’s createdAt. Dimensions assign each contribution to an Item, so a query breaks the series down by team, by person or by any Template the App names. The projector turns committed Events into Metric rows after the write that produced them has finished.

examples/concepts/metrics-and-projections.ts
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
export const supportApp: AppSpec = (app) => {
app.configure('support');
const analyst = app.addRole('analyst', { label: 'Analyst' });
const team = app.addTemplate('team');
team.addProperty('name', 'string');
const ticket = app.addTemplate('ticket');
ticket.addRelation('team', 'many-to-one', team, { toKey: 'tickets' });
const resolved = ticket.addEvent('resolved');
resolved.addProperty('minutes', 'integer').mandatory();
const byTeam = app.addMetricDimension('team', [team]);
const resolutionMinutes = app.addMetric('resolutionMinutes');
resolutionMinutes.addEvent(resolved, (event) => {
const minutes = event.payload.minutes;
return typeof minutes === 'number' ? minutes : null; // null excludes the Event from the Metric
});
resolutionMinutes.addDimension(byTeam, 1, async (event, _dimension, ctx) => {
const item = await ctx.getItem(event.itemId);
const relation = item?.relations.team;
if (relation?.type !== 'one') return undefined;
return await relation.get();
});
resolutionMinutes.addGrant(analyst);
};

metric.addEvent(event, handler?) selects an Event. Without a handler each Event contributes 1, which makes the Metric a count. With a handler, (event) => number | null reads the Event’s payload and returns the value to contribute; null excludes that Event. In the example, resolutionMinutes contributes the minutes each ticket took, read from the resolved payload rather than from the ticket Item.

A contribution comes from the Event, so put the facts a Metric needs into the Event’s payload when you declare it. The payload is fixed at publish time; the Item can change afterward. A Metric that reads its value from the payload gives the same answer whenever it is projected. A Metric selects Events from any App in the composition, so a root App measures Events declared by the Apps it imports.

A Dimension is declared once on the App with addMetricDimension(key, subjects). The subjects are the Templates or Unions whose Items serve as categories. addDimension(dimension, position, handler) attaches it to a Metric. The position, 1 to 4, is the slot the Dimension occupies in the projection; a Metric carries at most four Dimensions, one per position.

The handler (event, dimension, ctx) maps one Event to one category. It receives a read context, so it loads the Event’s Item and follows a Relation, as the example does to reach the ticket’s team. It returns the category Item, the Item’s Identifier as a string, or undefined when the Event has no category on that Dimension. A raw string must be a UUID, because Dimension columns are UUID-typed. A handler that throws or returns an invalid value places the contribution in the null category rather than failing the projection.

Reading current Items inside a Dimension handler is a deliberate choice. It gives the category as it is at projection time, which is what most reports want for a team assignment. When the category at the time of the Event is what counts, put it in the payload and read it from event.payload instead.

The projector runs as system, separately from the write and from the reactor. It selects committed Events that have not been projected and evaluates the calculation and Dimension handlers for each Metric that selects the Event. It then inserts one row per Metric with the value, the Dimension columns, the Event’s createdAt and the Workspace. Each row is keyed by Event and Metric, so projecting an Event twice writes it once. The projector stores a failure against the Event so the projection can be repaired and rerun.

The Metric therefore lags the write. A Mutation returns when its transaction commits; the Event it published becomes a Metric row when the projector next runs. In production the projector is a background loop. In tests, harness.tickAll() runs the reactor and the projector to completion, so a test writes, ticks, and then asserts on the Metric.

Deriving Metrics from Events rather than from current Items makes them cheap and stable. A Metric never scans Items, its history does not change when an Item is edited or archived, and clearing the projection rebuilds it from the Event history.

At run time a handler reaches a Metric through current.metrics.<key>, a MetricInstance. query(func) takes an aggregate function such as 'count' or 'sum' and returns a builder that sets the period, the interval and the Dimensions to bucket by. execute on that builder returns the projected series. current.report(request) runs several such queries in one call. metric.addGrant(role) decides which App Roles may query the Metric; a principal without a Grant receives nothing.

  • A Metric is not a Query over Items. It reads only the projection built from Events.
  • A Mutation returning does not mean its Metrics have updated. Projection follows commit.
  • A Dimension is not a free-text tag. It is an Item of a declared Template or Union, or a UUID.
  • A Metric has at most four Dimensions, one per position.