Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/prosperity-dashboard/CombinedSectionChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "@buildcanada/charts";
import {
humanizeSourceName,
humanizeSourceUrl,
type EconomySeriesResponse,
} from "@/lib/api/economy";
import {
Expand Down Expand Up @@ -95,7 +96,7 @@ function buildGrapherState(
{
id: 1,
title: humanizeSourceName(source.name),
urlMain: source.url ?? undefined,
urlMain: humanizeSourceUrl(source.name, source.url) ?? undefined,
datePublished: source.last_fetched_at ?? undefined,
},
]
Expand Down
3 changes: 2 additions & 1 deletion src/app/prosperity-dashboard/IndicatorChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "@buildcanada/charts";
import {
humanizeSourceName,
humanizeSourceUrl,
type EconomySeriesResponse,
} from "@/lib/api/economy";
import {
Expand Down Expand Up @@ -99,7 +100,7 @@ function buildGrapherState(
{
id: 1,
title: humanizeSourceName(source.name),
urlMain: source.url ?? undefined,
urlMain: humanizeSourceUrl(source.name, source.url) ?? undefined,
datePublished: source.last_fetched_at ?? undefined,
},
]
Expand Down
6 changes: 4 additions & 2 deletions src/app/prosperity-dashboard/[section]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getSiteConfig } from "@/lib/api";
import {
getEconomicSeries,
humanizeSourceName,
humanizeSourceUrl,
type EconomySeriesResponse,
} from "@/lib/api/economy";
import { PageHeader } from "@/components/ui/page-header";
Expand Down Expand Up @@ -63,12 +64,13 @@ function SourceLine({ response }: { response: EconomySeriesResponse }) {
? formatFetchedDate(source.last_fetched_at)
: "";
const sourceName = humanizeSourceName(source.name);
const sourceUrl = humanizeSourceUrl(source.name, source.url);
return (
<p className="mt-3 type-label-sm text-dark/60">
Source:{" "}
{source.url ? (
{sourceUrl ? (
<a
href={source.url}
href={sourceUrl}
target="_blank"
rel="noopener noreferrer"
className="underline hover:text-dark"
Expand Down
33 changes: 33 additions & 0 deletions src/lib/api/economy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,39 @@ export function humanizeSourceName(name: string): string {
return name;
}

// For StatsCan sources the API's source URL is its ingestion endpoint
// (StatsCan's POST-only Web Data Service, so browsers get a 405); link to
// the public table page instead. Table numbers match SOURCE_NAMES above.
const STATCAN_TABLES: Record<string, string> = {
econ_statcan_gini_after_tax: "11-10-0134",
econ_statcan_low_income_dynamics: "11-10-0024",
econ_statcan_housing_starts: "34-10-0126",
econ_statcan_rental_vacancy: "34-10-0127",
econ_statcan_crime_severity: "35-10-0026",
econ_statcan_crime_rate: "35-10-0177",
econ_statcan_cpi_essentials: "18-10-0004",
econ_statcan_gdp_monthly: "36-10-0434",
};

export function humanizeSourceUrl(
name: string,
url: string | null,
): string | null {
const table = STATCAN_TABLES[name];
if (table)
return `https://www150.statcan.gc.ca/t1/tbl1/en/tv.action?pid=${table.replaceAll("-", "")}01`;
// World Bank source URLs are raw API queries (JSON dumps); link to the
// public indicator page instead. Worldwide Governance Indicators carry a
// GOV_WGI_ prefix in the API's source=3 dataset that the public catalog
// doesn't use (GOV_WGI_GE.EST -> GE.EST).
const worldBank = url?.match(
/^https:\/\/api\.worldbank\.org\/v2\/country\/[^/]+\/indicator\/([A-Za-z0-9._]+)/,
);
if (worldBank)
return `https://data.worldbank.org/indicator/${worldBank[1].replace(/^GOV_WGI_/, "")}`;
return url;
}

// Annual data refreshed at most weekly; matches the API's Cache-Control max-age.
const REVALIDATE = 3600;

Expand Down
Loading