Charts over millions of events
Every meter of an energy company sends a reading a minute, so metering.readings grows by millions of rows a month. A chart of 90 days over those rows reads them all each time someone opens the dashboard. This guide gives the readings an hourly time rollup and charts that instead: a few thousand rows, the same answers.
1. The events and their rollup
Section titled “1. The events and their rollup”import { f, resource } from '@protobase/schema'
// Meter readings, millions of rows, and their hourly rollup per organization and meter.
/** metering.readings: one row per reading. */export const readings = resource('readings') .table('metering.readings') .fields({ id: f.bigint().readOnly().dbDefault(), organization_id: f.integer().filterable(), meter_id: f.relation('meters').filterable(), taken_at: f.timestamp().filterable().sortable(), watts: f.integer(), cost: f.decimal({ precision: 10, scale: 4 }), }) .primaryKey((r) => r.id) .tenant((r) => r.organization_id)
/** metering.readings_hourly: one row per hour, organization and meter, which the migration creates. */export const readingsHourly = resource('readings_hourly') .table('metering.readings_hourly') .bucketsOf(readings, { time: (r) => r.taken_at, every: 'hour', by: (r) => [r.organization_id, r.meter_id], measures: { watts: ['sum', 'max'], cost: ['sum'] }, late: '2 hours', }) // Its access is its own: every member of an organization reads its totals. .access({ read: () => true })readings is the table the meters write to. readings_hourly is a resource over it with one row per hour, organization and meter: how many readings fell in it (events), their total and highest watts (watts_sum, watts_max) and their total cost (cost_sum). Run protobase migration and apply the file it writes: it creates the rollup’s table and indexes and fills it.
2. A dashboard on the rollup
Section titled “2. A dashboard on the rollup”/** @jsxImportSource @protobase/layout */import { Chart, Grid, Page, page } from '@protobase/layout'
// A dashboard over the hourly rollup: each chart reads a few thousand buckets instead of millions of readings.export const energy = page( 'energy', <Page title="Energy" description="The last 90 days of every meter."> <Grid columns={2}> <Chart title="Readings per day" resource="readings_hourly" groupBy="bucket" range="90d" /> <Chart title="Average watts per day" resource="readings_hourly" groupBy="bucket" average="watts_sum" range="90d" type="line" /> <Chart title="Cost per week by meter" resource="readings_hourly" groupBy="bucket" splitBy="meter_id" sum="cost_sum" granularity="week" range="90d" /> <Chart title="Cost per meter" resource="readings_hourly" groupBy="meter_id" sum="cost_sum" filter="bucket >= now() - 90d" limit={5} /> </Grid> </Page>,)Each Chart names the rollup as its resource and groups by bucket, or by meter_id. A chart counts the readings the buckets hold, and its measure names the rollup’s field: average="watts_sum" divides the watts by the readings, as an average over the readings themselves would. The days, weeks and months are the viewer’s, for any time zone a whole number of hours off UTC.
3. What a rollup cannot answer
Section titled “3. What a rollup cannot answer”A rollup knows nothing finer than its hour, so a chart of 15 minutes, or of days that start at half past the hour, such as India’s, is refused rather than answered wrongly. Point such a chart at readings instead. Charts over a rollup lists every measure and what is refused.