Summary
Functions and types for week-over-week energy summaries
resolveSummaryTimeframe
Computes the timeframe needed for week-over-week summaries. Returns a timeframe
compatible with fetchMeasurements that covers the last complete ISO week and
the week before it.
import { resolveSummaryTimeframe } from "@octopus-energy/measurements/summary";resolveSummaryTimeframeProp
Type
import { fetchMeasurements } from "@octopus-energy/measurements";
import { resolveSummaryTimeframe } from "@octopus-energy/measurements/summary";
const timeframe = resolveSummaryTimeframe({ anchorDate, timezone });
const records = await fetchMeasurements(client, {
propertyId,
marketSupplyPoints,
timeframe,
});ResolveSummaryTimeframeOptionsProp
Type
createMeasurementsSummary
Creates a primitive week-over-week MeasurementsSummary model from an ordered
array of measurement groups. Role selection does not happen in this transform;
use the role helpers below when rendering headline copy, indicators, chart bars,
or chart labels.
import { groupMeasurements } from "@octopus-energy/measurements";
import { createMeasurementsSummary } from "@octopus-energy/measurements/summary";createMeasurementsSummaryProp
Type
import { fetchMeasurements, groupMeasurements } from "@octopus-energy/measurements";
import { createMeasurementsSummary } from "@octopus-energy/measurements/summary";
const records = await fetchMeasurements(client, {
propertyId,
marketSupplyPoints,
timeframe,
});
const summary = createMeasurementsSummary(
groupMeasurements(records, { matching: marketSupplyPoints }),
{
currencyFallback: "GBP",
locale: "en-GB",
timeframe,
},
);The returned object is always a summary state:
| Field | Meaning |
|---|---|
status | "ready" when at least one entry has a comparable metric delta |
entries | one entry per input MeasurementGroup, including empty entries |
periods.weekBefore / periods.lastWeek | safe total period values and completeness, where totals can combine |
deltas.energy / deltas.cost | safe total deltas, where totals can be compared |
direction on entries means measurement flow ("consumption" | "generation").
Week-over-week change direction is represented by delta.trend
("up" | "down" | "stable").
Cost normalization
The default normalizer converts costs from minor to major currency units using Intl.NumberFormat currency precision. For GBP, minor units are pence and major units are pounds. formatOptions.cost changes display formatting, not the conversion between minor and major units.
The transform reads the currency from cost metadata and uses currencyFallback only when metadata is missing. The transform calculates a group’s weekly cost total only when all contributing costs share the same resolved currency. Otherwise, the transform skips that cost total and does not call normalizeCost.
Pass normalizeCost in the optional third argument to override conversion. normalizeCost receives { amount, currency } and returns a numeric amount in major units without display rounding. amount is numeric, and currency always contains the resolved currency code.
For each group’s weekly period, the transform sums raw costs at their original precision before calling normalizeCost. Combined totals and deltas use those normalized amounts without another normalization step.
For APIs that already return major units, return amount unchanged:
const summary = createMeasurementsSummary(
groupMeasurements(records, { matching: marketSupplyPoints }),
{ timeframe, locale: "en-GB", currencyFallback: "GBP" },
{ normalizeCost: ({ amount }) => amount },
);Configuration and output types
The transform accepts configuration and dependencies, and returns a MeasurementsSummary.
MeasurementsSummaryConfigProp
Type
MeasurementsSummaryDependenciesProp
Type
MeasurementsSummary<TFilters>Prop
Type
SummaryEntry<TFilters>Prop
Type
SummaryPeriodTotalsProp
Type
Role helpers
Role helpers select data for each render role from the primitive summary model. They are safe to call for both ready and empty summaries.
import {
getSummaryChartBars,
getSummaryChartLabels,
getSummaryComparison,
getSummaryIndicators,
} from "@octopus-energy/measurements/summary";
const comparison = getSummaryComparison(summary);
const indicators = getSummaryIndicators(summary);
const chartBars = getSummaryChartBars(summary);
const chartLabels = getSummaryChartLabels(summary);getSummaryComparison
Returns SummaryComparison | null. It tries total metrics in preference order
first, falls back to kind: "breakdown" using ready entries when totals are
unsafe, and returns null when no usable comparison exists.
getSummaryComparisonProp
Type
type SummaryComparison =
SummaryComparisonTotalCost | SummaryComparisonTotalEnergy | SummaryComparisonBreakdown;SummaryComparisonTotalCostProp
Type
SummaryComparisonTotalEnergyProp
Type
SummaryComparisonBreakdown<TFilters>Prop
Type
getSummaryIndicators
Returns ready SummaryIndicator[] entries for indicator rows. Empty summary
entries are preserved in the primitive model but filtered out of this render
role.
getSummaryIndicatorsProp
Type
type SummaryIndicator = SummaryIndicatorCost | SummaryIndicatorEnergy;SummaryIndicatorCost<TFilters>Prop
Type
SummaryIndicatorEnergy<TFilters>Prop
Type
getSummaryChartBars
Returns { weekBefore, lastWeek } arrays of future-facing chart bars. Chart
bars include only ready entries with a concrete utility because Coral chart
styling needs a utility-specific type.
getSummaryChartBarsProp
Type
SummaryChartBarsProp
Type
getSummaryChartLabels
Returns stable labels for the week-before and last-week chart columns. Empty
summaries are represented as { kind: "empty", label: "" } for each period.
getSummaryChartLabelsProp
Type
SummaryChartLabels<TFilters>Prop
Type
Metric preferences
Each role helper accepts an optional metric preference. The preference is a
two-metric tuple so helpers can fall back when the preferred metric is not safe
for that role.
getSummaryComparison(summary, { metric: ["energy", "cost"] });
getSummaryChartLabels(summary, { metric: ["cost", "energy"] });Current defaults live in DEFAULT_SUMMARY_METRICS:
| Role | Default preference |
|---|---|
| comparison | ["energy", "cost"] |
| indicators | ["energy", "cost"] |
| chart bars | ["energy", "cost"] |
| chart labels | ["cost", "energy"] |
export type SummaryMetricPreference = readonly ["cost", "energy"] | readonly ["energy", "cost"];DefaultSummaryMetricPreferencesProp
Type
Percentage changes
Every delta includes a relative percentage change:
const comparison = getSummaryComparison(summary);
if (comparison?.kind === "total") {
comparison.delta.percentage;
// { value: 14.285714285714285, label: "14%" }
}percentage.value is the signed raw percentage value:
((lastWeek - weekBefore) / weekBefore) * 100;percentage.label is an absolute, localized percentage string for display copy.
It uses Intl.NumberFormat and defaults to integer display, matching the
current Coral-style summary presentation:
const summary = createMeasurementsSummary(grouped, {
locale: "en-GB",
timeframe,
formatOptions: {
percentage: {
maximumFractionDigits: 1,
},
},
});When the week-before value is zero and last week is non-zero, percentage.value
is Infinity or -Infinity, and percentage.label is formatted by
Intl.NumberFormat, for example "∞%". When both values are zero,
percentage.value is 0 and delta.trend is "stable".
SummaryDeltasProp
Type
SummaryDelta<TDifference>Prop
Type
SummaryCostDifferenceProp
Type
SummaryEnergyDifferenceProp
Type
SummaryPercentageProp
Type
Related
- EnergySummary Quick Start: full walkthrough
- Measurements API reference:
fetchMeasurementsused with the resolved timeframe - Coral Compat API reference: adapting role-helper outputs to current Coral props