Energy Summary
Step-by-step guide to migrate the week-over-week comparison widget to the new measurements API
Which tab should I pick?
Blueprint if you use SingleEnergyStatisticsCard,
MultiEnergyStatisticsCard, or the deprecated
ConsumptionOverviewStatisticsCard from @krakentech/blueprint-utils or
@octopus-energy/measurements before v0.5.
Coral if you already render with Coral's EnergySummary compound components
and useEnergySummary.
Fetch summary data
The summary needs the two most recent complete ISO weeks before the anchor's
current week. Use resolveSummaryTimeframe to compute the right date range,
then fetch measurements with React Query.
Replace useMeasurementFilters + useMeasurements with
resolveSummaryTimeframe and React Query based fetching.
import {
DateRangeToggleValue,
FuelType,
useMeasurementFilters,
useMeasurements,
} from "@octopus-energy/measurements";
const measurementFilters = useMeasurementFilters({
initialDateRange: DateRangeToggleValue.WEEK,
});
const measurements = useMeasurements({
variables: {
accountNumber,
marketSupplyPointId,
measurementFilters,
propertyId,
utilityType: FuelType.Electricity,
},
});import { type UtilityMarkets } from "@octopus-energy/measurements/market-supply-points";
import {
marketSupplyPointsQueryOptions,
measurementsQueryOptions,
} from "@octopus-energy/measurements/react-query";
import { resolveSummaryTimeframe } from "@octopus-energy/measurements/summary";
import { useQuery } from "@tanstack/react-query";
const utilityMarkets: UtilityMarkets = {
electricity: ["GBR_ELECTRICITY"],
gas: ["GBR_GAS"],
};
const timeframe = resolveSummaryTimeframe({
timezone: "Europe/London",
});
const marketSupplyPointsQuery = useQuery(
marketSupplyPointsQueryOptions(client, {
accountNumber,
propertyId,
utilityMarkets,
}),
);
const measurementsQuery = useQuery(
measurementsQueryOptions(client, {
marketSupplyPoints: marketSupplyPointsQuery.data,
propertyId,
timeframe: timeframe,
}),
);No user-controlled state needed
Unlike EnergyOverview, EnergySummary doesn't need useTimeframe or usePreferences.
resolveSummaryTimeframe computes a fixed 2-week window automatically.
Transform into summary data
The old statistics cards (SingleEnergyStatisticsCard,
MultiEnergyStatisticsCard) computed totals and percentage change internally.
This is now an explicit step using createMeasurementsSummary.
import { groupMeasurements } from "@octopus-energy/measurements";
import {
createMeasurementsSummary,
getSummaryChartBars,
getSummaryChartLabels,
getSummaryComparison,
getSummaryIndicators,
type SummaryComparison,
} from "@octopus-energy/measurements/summary";
const groups = groupMeasurements(measurementsQuery.data, {
matching: marketSupplyPoints,
});
const summary = createMeasurementsSummary(groups, {
timeframe,
locale: "en-GB",
});
const comparison = getSummaryComparison(summary);
const indicators = getSummaryIndicators(summary);
const chartBars = getSummaryChartBars(summary);
const chartLabels = getSummaryChartLabels(summary);Format the comparison
The summary produces structured delta objects. You need a small formatting function to turn them into user-facing content. This replaces the hardcoded percentage rendering from the old statistics cards.
import type { SummaryIndicatorProps } from "@octopus-energy/measurements/compat";
import type {
SummaryComparison,
SummaryComparisonEntry,
} from "@octopus-energy/measurements/summary";
const listFormat = new Intl.ListFormat("en-GB", { type: "conjunction" });
function formatGroupComparison(entry: SummaryComparisonEntry): string {
const energy = entry.utility ?? "energy";
if (entry.delta.trend === "stable") {
return `${energy} stayed about the same`;
}
const direction = entry.delta.trend === "up" ? "more" : "less";
return `${energy} was ${entry.delta.difference.label} ${direction}`;
}
function formatComparison(
comparison: SummaryComparison | null,
energyType: "electricity" | "gas" | "energy",
): string {
if (!comparison) return "Not enough data for week-over-week comparison.";
if (comparison.kind === "breakdown") {
const parts = comparison.entries.map(formatGroupComparison);
return `${listFormat.format(parts)} last week compared to the week before.`;
}
const verb = comparison.metric === "cost" ? "spent" : "used";
if (comparison.delta.percentage.value === Infinity) {
return `You ${verb} ${energyType} last week, but had none the week before.`;
}
if (Math.abs(comparison.delta.percentage.value) <= 1) {
return `You ${verb} about the same on ${energyType} last week compared to the week before.`;
}
const direction = comparison.delta.percentage.value < 0 ? "less" : "more";
return `You ${verb} ${comparison.delta.percentage.label} ${direction} on ${energyType} last week compared to the week before.`;
}
function formatIndicator(indicator: SummaryIndicatorProps): string {
const energy = indicator.utility ?? "energy";
if (indicator.delta.trend === "stable") {
return `About the same on ${energy}`;
}
const direction = indicator.delta.trend === "up" ? "more" : "less";
return `${indicator.delta.difference.label} ${direction} on ${energy}`;
}Customize to your needs
This is just a minimal example. You can adjust the threshold, add i18n (the sandbox uses
next-intl with ICU message format), or tailor the text to your product.
Wire to Coral components
The compat layer maps the summary output into props for Coral's EnergySummary
compound components. The helpers produce the exact same shapes that
useEnergySummary() returned.
Replace SingleEnergyStatisticsCard or MultiEnergyStatisticsCard with Coral's
EnergySummary compound components. The compat layer produces all the props you
need.
import { SingleEnergyStatisticsCard } from "@krakentech/blueprint-utils/client/components";
<SingleEnergyStatisticsCard
measurements={measurements}
utilityType={FuelType.Electricity}
translations={translations}
currency="GBP"
locale="en-GB"
/>;import {
getSummaryChartBarProps,
getSummaryChartLabelProps,
getSummaryIndicatorsProps,
} from "@octopus-energy/measurements/compat";
import {
EnergySummary,
EnergySummaryChart,
EnergySummaryChartContainer,
EnergySummaryChartGroup,
EnergySummaryChartLabel,
EnergySummaryCharts,
EnergySummaryIndicator,
EnergySummaryIndicators,
EnergySummarySummary,
} from "@octopus-energy/coral/molecules";
import { Typography } from "@octopus-energy/coral/atoms";
return (
<EnergySummary
error={false}
errorMessage="Something went wrong"
loading={marketSupplyPointsQuery.isPending || measurementsQuery.isPending}
>
<EnergySummarySummary>
<h4>At a glance</h4>
<Typography textStyle="body2">{formatComparison(comparison, energyType)}</Typography>
{getSummaryIndicatorsProps(indicators).map((indicator) => (
<EnergySummaryIndicators key={indicator.key}>
<EnergySummaryIndicator direction={indicator.direction} />
<Typography textStyle="body2">{formatIndicator(indicator)}</Typography>
</EnergySummaryIndicators>
))}
</EnergySummarySummary>
<EnergySummaryCharts>
<EnergySummaryChartContainer>
<EnergySummaryChartLabel {...getSummaryChartLabelProps(chartLabels.weekBefore)} />
<EnergySummaryChartGroup>
{getSummaryChartBarProps(chartBars.weekBefore).map(({ key, ...chart }) => (
<EnergySummaryChart key={key} {...chart} />
))}
</EnergySummaryChartGroup>
<EnergySummaryChartLabel value="Week before" />
</EnergySummaryChartContainer>
<EnergySummaryChartContainer>
<EnergySummaryChartLabel {...getSummaryChartLabelProps(chartLabels.lastWeek)} />
<EnergySummaryChartGroup>
{getSummaryChartBarProps(chartBars.lastWeek).map(({ key, ...chart }) => (
<EnergySummaryChart key={key} {...chart} />
))}
</EnergySummaryChartGroup>
<EnergySummaryChartLabel value="Last week" />
</EnergySummaryChartContainer>
</EnergySummaryCharts>
</EnergySummary>
);