Skip to Content
Octopus Energy Packages
API ReferenceTransforms

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";
resolveSummaryTimeframe

Prop

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,
});
ResolveSummaryTimeframeOptions

Prop

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";
createMeasurementsSummary

Prop

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:

FieldMeaning
status"ready" when at least one entry has a comparable metric delta
entriesone entry per input MeasurementGroup, including empty entries
periods.weekBefore / periods.lastWeeksafe total period values and completeness, where totals can combine
deltas.energy / deltas.costsafe 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.

MeasurementsSummaryConfig

Prop

Type

MeasurementsSummaryDependencies

Prop

Type

MeasurementsSummary<TFilters>

Prop

Type

SummaryEntry<TFilters>

Prop

Type

SummaryPeriodTotals

Prop

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.

getSummaryComparison

Prop

Type

type SummaryComparison =
  SummaryComparisonTotalCost | SummaryComparisonTotalEnergy | SummaryComparisonBreakdown;
SummaryComparisonTotalCost

Prop

Type

SummaryComparisonTotalEnergy

Prop

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.

getSummaryIndicators

Prop

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.

getSummaryChartBars

Prop

Type

SummaryChartBars

Prop

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.

getSummaryChartLabels

Prop

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:

RoleDefault preference
comparison["energy", "cost"]
indicators["energy", "cost"]
chart bars["energy", "cost"]
chart labels["cost", "energy"]
export type SummaryMetricPreference = readonly ["cost", "energy"] | readonly ["energy", "cost"];
DefaultSummaryMetricPreferences

Prop

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".

SummaryDeltas

Prop

Type

SummaryDelta<TDifference>

Prop

Type

SummaryCostDifference

Prop

Type

SummaryEnergyDifference

Prop

Type

SummaryPercentage

Prop

Type


On this page