Files
website/scripts/stats/statcan.ts
Stan Runge 45841d6f6c
All checks were successful
Deploy production / deploy (push) Successful in 1m13s
stats: core-7 economy page with CPI index levels, rebase and categories
- /stats/economy defaults multi-country charts to CAN KOR CHN JPN USA NLD CUW,
  with add-a-country chips and an EU 27 comparison line
- CPI index levels rebased to a chosen base year, index/YoY toggle,
  categories (food, energy, housing, core) where the source has them
- new no-key collectors: oecd (CPI categories, 10y yields), statcan (CPI);
  bis WS_LONG_CPI, FRED US CPI categories, Eurostat prc_hicp_minr, WB FP.CPI.TOTL
- Canada added as a region; CBS Curacao fallbacks for growth/unemployment
- timers stats-oecd and stats-statcan
2026-10-26 00:34:27 +00:00

58 lines
2.2 KiB
TypeScript

// Statistics Canada Web Data Service (no key): CPI table 18-10-0004-01 (2002 = 100, monthly,
// not seasonally adjusted) by vector. Normalised only: `statcan:cpi:<category>`.
import { getJson, log, run, writeSeries, type ObsRow, type SeriesRow } from './common';
export const STATCAN_CPI: { vector: number; name: string; title: string }[] = [
{ vector: 41690973, name: 'total', title: 'Canada consumer price index, all items' },
{ vector: 41690974, name: 'food', title: 'Canada consumer price index, food' },
{ vector: 41691050, name: 'housing', title: 'Canada consumer price index, shelter' },
{ vector: 41691239, name: 'energy', title: 'Canada consumer price index, energy' },
{
vector: 41691233,
name: 'core',
title: 'Canada consumer price index, all items excluding food and energy'
}
];
type Wds = {
status: string;
object?: { vectorId: number; vectorDataPoint: { refPer: string; value: number | null }[] };
}[];
export async function statcan(opts: { job: string }) {
await run('statcan', opts.job, async (note) => {
const ids = STATCAN_CPI.map((s) => `%22${s.vector}%22`).join(',');
const body = await getJson<Wds>(
`https://www150.statcan.gc.ca/t1/wds/rest/getDataFromVectorByReferencePeriodRange?vectorIds=${ids}&startRefPeriod=1900-01-01&endReferencePeriod=2100-12-01`
);
const series: SeriesRow[] = [];
const obs: ObsRow[] = [];
for (const s of STATCAN_CPI) {
const points = body.find((b) => b.object?.vectorId === s.vector)?.object?.vectorDataPoint;
if (!points?.length) {
note(`v${s.vector}: no data`);
continue;
}
const id = `statcan:cpi:${s.name}`;
series.push({
series_id: id,
source_id: 'statcan',
source_code: `18-10-0004-01/v${s.vector}`,
title: s.title,
unit: 'Index 2002=100',
freq: 'M',
region_code: 'CAN',
theme: 'economy',
meta: { url: 'https://www150.statcan.gc.ca/t1/tbl1/en/tv.action?pid=1810000401' }
});
for (const p of points)
if (p.value != null && /^\d{4}-\d{2}-01$/.test(p.refPer))
obs.push({ series_id: id, period: p.refPer, value: p.value, status: null });
}
if (!obs.length) throw new Error('no StatCan observations');
await writeSeries(series, obs);
log(`statcan: ${obs.length} obs in ${series.length} series`);
return obs.length;
});
}