compare_municipalities
Compare specific performance indicators across multiple Swedish municipalities for selected years, enabling benchmarking analysis with gender filtering options.
Instructions
Jämför ett specifikt nyckeltal över flera kommuner för angivna år. Utmärkt för benchmarking. Stöder könsfiltrering (T/M/K).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| kpi_id | Yes | KPI-ID att jämföra | |
| municipality_ids | Yes | Lista med kommun-ID:n att jämföra (2-10 kommuner) | |
| years | No | Specifika år att inkludera i jämförelsen | |
| gender | No | Könsfilter: T=Totalt, M=Män, K=Kvinnor, all=visa alla | all |
Implementation Reference
- src/tools/data-tools.ts:278-340 (handler)Handler function that fetches KPI data from Kolada API for multiple municipalities in parallel, applies gender filtering, and returns structured comparison data.handler: async (args: z.infer<typeof compareMunicipalitiesSchema>): Promise<ToolResult> => { const startTime = Date.now(); const { kpi_id, municipality_ids, years, gender } = args; logger.toolCall('compare_municipalities', { kpi_id, municipality_ids, years, gender }); try { interface ComparisonEntry { municipality_id: string; data: KPIData[]; } const comparison: { kpi_id: string; municipalities: ComparisonEntry[]; } = { kpi_id, municipalities: [], }; // Fetch data for all municipalities in parallel for better performance const promises = municipality_ids.map(async (municipality_id) => { // Kolada API v3 uses path-based URLs let endpoint = `/data/kpi/${kpi_id}/municipality/${municipality_id}`; if (years && years.length > 0) { endpoint += `/year/${years.join(',')}`; } let data = await koladaClient.fetchAllData<KPIData>(endpoint); // Apply gender filter data = filterByGender(data, gender); return { municipality_id, data, }; }); comparison.municipalities = await Promise.all(promises); logger.toolResult('compare_municipalities', true, Date.now() - startTime); return { content: [ { type: 'text', text: JSON.stringify( { ...comparison, gender_filter: gender, source: 'Kolada - Källa: Kolada', }, null, 2 ), }, ], }; } catch (error) { logger.toolResult('compare_municipalities', false, Date.now() - startTime); throw error; } },
- src/tools/data-tools.ts:36-45 (schema)Zod schema defining input parameters: kpi_id, municipality_ids (2-10), optional years, and gender filter.const compareMunicipalitiesSchema = z.object({ kpi_id: z.string().describe('KPI-ID att jämföra'), municipality_ids: z .array(z.string()) .min(2) .max(10) .describe('Lista med kommun-ID:n att jämföra (2-10 kommuner)'), years: z.array(z.number()).optional().describe('Specifika år att inkludera i jämförelsen'), gender: z.enum(['T', 'M', 'K', 'all']).default('all').describe('Könsfilter: T=Totalt, M=Män, K=Kvinnor, all=visa alla'), });
- src/tools/data-tools.ts:274-341 (registration)Tool registration as 'compare_municipalities' within the dataTools export object, including description, schema, annotations, and handler reference.compare_municipalities: { description: 'Jämför ett specifikt nyckeltal över flera kommuner för angivna år. Utmärkt för benchmarking. Stöder könsfiltrering (T/M/K).', inputSchema: compareMunicipalitiesSchema, annotations: READ_ONLY_ANNOTATIONS, handler: async (args: z.infer<typeof compareMunicipalitiesSchema>): Promise<ToolResult> => { const startTime = Date.now(); const { kpi_id, municipality_ids, years, gender } = args; logger.toolCall('compare_municipalities', { kpi_id, municipality_ids, years, gender }); try { interface ComparisonEntry { municipality_id: string; data: KPIData[]; } const comparison: { kpi_id: string; municipalities: ComparisonEntry[]; } = { kpi_id, municipalities: [], }; // Fetch data for all municipalities in parallel for better performance const promises = municipality_ids.map(async (municipality_id) => { // Kolada API v3 uses path-based URLs let endpoint = `/data/kpi/${kpi_id}/municipality/${municipality_id}`; if (years && years.length > 0) { endpoint += `/year/${years.join(',')}`; } let data = await koladaClient.fetchAllData<KPIData>(endpoint); // Apply gender filter data = filterByGender(data, gender); return { municipality_id, data, }; }); comparison.municipalities = await Promise.all(promises); logger.toolResult('compare_municipalities', true, Date.now() - startTime); return { content: [ { type: 'text', text: JSON.stringify( { ...comparison, gender_filter: gender, source: 'Kolada - Källa: Kolada', }, null, 2 ), }, ], }; } catch (error) { logger.toolResult('compare_municipalities', false, Date.now() - startTime); throw error; } }, },
- src/tools/data-tools.ts:58-65 (helper)Helper function to filter KPI data points by gender (T=Totalt, M=Män, K=Kvinnor, all=unfiltered), used by the handler.function filterByGender(data: KPIData[], gender: 'T' | 'M' | 'K' | 'all'): KPIData[] { if (gender === 'all') return data; return data.map(d => ({ ...d, values: d.values.filter(v => v.gender === gender || (!v.gender && gender === 'T')) })).filter(d => d.values.length > 0); }