Files
at-mintel/packages/journaling/src/clients/data-commons.ts
2026-02-21 19:08:06 +01:00

53 lines
1.7 KiB
TypeScript

import axios from "axios";
export interface DataPoint {
date: string;
value: number;
}
export class DataCommonsClient {
private baseUrl = "https://api.datacommons.org";
/**
* Fetches statistical series for a specific variable and place.
* @param placeId DCID of the place (e.g., 'country/DEU' for Germany)
* @param variable DCID of the statistical variable (e.g., 'Count_Person')
*/
async getStatSeries(placeId: string, variable: string): Promise<DataPoint[]> {
try {
// https://docs.datacommons.org/api/rest/v2/stat_series
const response = await axios.get(`${this.baseUrl}/v2/stat/series`, {
params: {
place: placeId,
stat_var: variable,
},
});
// Response format: { "series": { "country/DEU": { "Count_Person": { "val": { "2020": 83166711, ... } } } } }
const seriesData = response.data?.series?.[placeId]?.[variable]?.val;
if (!seriesData) {
return [];
}
return Object.entries(seriesData)
.map(([date, value]) => ({ date, value: Number(value) }))
.sort((a, b) => a.date.localeCompare(b.date));
} catch (error) {
console.error(`DataCommons Error (${placeId}, ${variable}):`, error);
return [];
}
}
/**
* Search for entities (places, etc.)
*/
async resolveEntity(name: string): Promise<string | null> {
// Search API or simple mapping for now.
// DC doesn't have a simple "search" endpoint in v2 public API easily accessible without key sometimes?
// Let's rely on LLM to provide DCIDs for now, or implement a naive search if needed.
// For now, return null to force LLM to guess/know DCIDs.
return null;
}
}