Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | 7x 7x 7x 7x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 1x 1x 1x 1x 15x 1x 1x 1x 11x 18x 18x 18x 1x 1x 1x 1x | #!/usr/bin/env tsx
/**
* @module populate-analysis-data
* @description Standalone script that fetches recent data from **all** MCP sources
* (riksdag-regering, SCB, World Bank) and persists it to `analysis/data/`.
*
* This populates the analysis folder with **all** supported data types:
* - **Documents**: propositions, motions, committeeReports, votes, speeches,
* questions, interpellations
* - **Calendar events**: upcoming parliamentary events
* - **MPs**: current member profiles
* - **Government documents**: recent government publications (regeringen.se)
* - **Voting groups**: party-level voting patterns
* - **World Bank indicators**: Swedish economic context (GDP, unemployment, etc.)
* - **SCB statistics**: Swedish official statistics for key policy domains
*
* The script reuses the existing MCP client and data-persistence modules,
* ensuring the collision-free sidecar design (data + .meta.json) is
* consistently applied.
*
* Usage:
* npx tsx scripts/populate-analysis-data.ts [--limit N] [--date YYYY-MM-DD]
*
* Options:
* --limit N Max documents per type (default: 20)
* --date YYYY-MM-DD Target date for analysis (default: today UTC)
*
* @author Hack23 AB
* @license Apache-2.0
*/
import { MCPClient } from './mcp-client/client.js';
import {
downloadAllDocuments,
flattenDocuments,
} from './pre-article-analysis/data-downloader.js';
import {
persistDownloadedData,
persistEvents,
persistMPs,
persistMCPResponse,
persistWorldBankData,
persistSCBData,
getDataRoot,
resolveDocId,
} from './pre-article-analysis/data-persistence.js';
import type { RawDocument } from './data-transformers/types.js';
import {
WorldBankClient,
INDICATOR_IDS,
COUNTRY_CODES,
} from './world-bank-client.js';
import {
SCBClient,
SCB_DOMAINS,
} from './scb-client.js';
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/**
* Compute riksmöte (parliamentary session year) from an ISO date string.
* Swedish parliamentary year runs Oct → Sep:
* - 2025-10-01 to 2026-09-30 → "2025/26"
* - 2026-10-01 to 2027-09-30 → "2026/27"
*
* @param dateStr - ISO date string (YYYY-MM-DD)
* @returns Riksmöte identifier (e.g. "2025/26")
*/
export function riksMoteFromDate(dateStr: string): string {
const d = new Date(dateStr + 'T00:00:00Z');
const year = d.getUTCFullYear();
const month = d.getUTCMonth() + 1;
if (month >= 10) return `${year}/${String(year + 1).slice(-2)}`;
return `${year - 1}/${String(year).slice(-2)}`;
}
/** Format a Date to YYYY-MM-DD. */
function formatDate(d: Date): string {
return d.toISOString().slice(0, 10);
}
/** Safely extract error message from unknown thrown value. */
function errorMessage(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
function parseArgs(): { limit: number; date: string } {
const args = process.argv.slice(2);
let limit = 20;
let date = formatDate(new Date());
for (let i = 0; i < args.length; i++) {
if (args[i] === '--limit' && args[i + 1]) {
limit = Math.max(1, parseInt(args[i + 1], 10) || 20);
i++;
} else if (args[i] === '--date' && args[i + 1]) {
const candidate = args[i + 1];
const dm = /^(\d{4})-(\d{2})-(\d{2})$/.exec(candidate);
if (!dm) {
console.error(`❌ Invalid date: "${candidate}". Expected YYYY-MM-DD format.`);
process.exit(1);
}
const [, yStr, mStr, dStr] = dm;
const y = Number(yStr), mo = Number(mStr), da = Number(dStr);
const parsed = new Date(Date.UTC(y, mo - 1, da));
if (Number.isNaN(parsed.getTime()) || parsed.getUTCFullYear() !== y || parsed.getUTCMonth() + 1 !== mo || parsed.getUTCDate() !== da) {
console.error(`❌ Invalid date: "${candidate}". Expected a valid YYYY-MM-DD date.`);
process.exit(1);
}
date = candidate;
i++;
}
}
return { limit, date };
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main(): Promise<void> {
const { limit, date } = parseArgs();
const rm = riksMoteFromDate(date);
console.log('╔══════════════════════════════════════════════════════════════╗');
console.log('║ 📥 Populate Analysis Data — MCP Data Fetcher ║');
console.log('╚══════════════════════════════════════════════════════════════╝');
console.log(` 📅 Date: ${date}`);
console.log(` 🏛️ Riksmöte: ${rm}`);
console.log(` 📊 Limit: ${limit} per type`);
console.log(` 📂 Data root: ${getDataRoot()}`);
console.log('');
const client = new MCPClient();
// ── Step 1: Download all document types ─────────────────────────────────
console.log('📄 Step 1/7: Downloading parliamentary documents...');
try {
const { data, manifest } = await downloadAllDocuments(client, { limit, rm });
const allDocs = flattenDocuments(data);
console.log(` ✅ Downloaded ${allDocs.length} documents from ${manifest.dataSources.length} MCP tools (${manifest.durationMs}ms)`);
const persistResult = persistDownloadedData(data, rm);
console.log(` 🗄️ Persisted ${persistResult.written} document files (${persistResult.skipped} skipped)`);
} catch (err) {
console.error(' ❌ Document download failed:', errorMessage(err));
}
// ── Step 2: Download calendar events ────────────────────────────────────
console.log('\n📅 Step 2/7: Downloading calendar events...');
try {
const today = new Date(date + 'T00:00:00Z');
const twoWeeksAhead = new Date(today);
twoWeeksAhead.setUTCDate(twoWeeksAhead.getUTCDate() + 14);
const rawEvents = await client.fetchCalendarEvents(
formatDate(today),
formatDate(twoWeeksAhead),
);
const events: RawDocument[] = (Array.isArray(rawEvents) ? rawEvents : []) as RawDocument[];
console.log(` ✅ Downloaded ${events.length} calendar events`);
Iif (events.length > 0) {
const persistResult = persistEvents(events, rm);
console.log(` 🗄️ Persisted ${persistResult.written} event files (${persistResult.skipped} skipped)`);
}
} catch (err) {
console.error(' ❌ Calendar event download failed:', errorMessage(err));
}
// ── Step 3: Download MP profiles ────────────────────────────────────────
console.log('\n👤 Step 3/7: Downloading MP profiles...');
try {
const rawMPs = await client.fetchMPs({ limit });
const mps: RawDocument[] = (Array.isArray(rawMPs) ? rawMPs : []) as RawDocument[];
console.log(` ✅ Downloaded ${mps.length} MP profiles`);
Iif (mps.length > 0) {
const persistResult = persistMPs(mps, rm);
console.log(` 🗄️ Persisted ${persistResult.written} MP files (${persistResult.skipped} skipped)`);
}
} catch (err) {
console.error(' ❌ MP download failed:', errorMessage(err));
}
// ── Step 4: Download government documents ───────────────────────────────
console.log('\n🏛️ Step 4/7: Downloading government documents...');
try {
const thirtyDaysAgo = new Date(date + 'T00:00:00Z');
thirtyDaysAgo.setUTCDate(thirtyDaysAgo.getUTCDate() - 30);
const rawGovDocs = await client.fetchGovernmentDocuments({
dateFrom: formatDate(thirtyDaysAgo),
dateTo: date,
limit,
});
const govDocs: RawDocument[] = (Array.isArray(rawGovDocs) ? rawGovDocs : []) as RawDocument[];
console.log(` ✅ Downloaded ${govDocs.length} government documents`);
let govWritten = 0;
for (let i = 0; i < govDocs.length; i++) {
const doc = govDocs[i];
if (!doc) continue;
persistMCPResponse(
{ tool: 'search_regering', params: { dateFrom: formatDate(thirtyDaysAgo), dateTo: date, limit }, server: 'riksdag-regering' },
doc,
resolveDocId(doc, i),
);
govWritten++;
}
console.log(` 🗄️ Persisted ${govWritten} government document files`);
} catch (err) {
console.error(' ❌ Government document download failed:', errorMessage(err));
}
// ── Step 5: Download voting groups by party ─────────────────────────────
console.log('\n🗳️ Step 5/7: Downloading voting groups by party...');
try {
const rawGroups = await client.fetchVotingGroup({ rm, groupBy: 'parti', limit });
const groups: RawDocument[] = (Array.isArray(rawGroups) ? rawGroups : []) as RawDocument[];
console.log(` ✅ Downloaded ${groups.length} voting group records`);
let voteGroupWritten = 0;
for (let i = 0; i < groups.length; i++) {
const group = groups[i];
if (!group) continue;
const record = group as Record<string, unknown>;
const id = (typeof record['parti'] === 'string' && record['parti'])
|| `group-${i + 1}`;
persistMCPResponse(
{ tool: 'get_voting_group', params: { rm, groupBy: 'parti', limit }, server: 'riksdag-regering' },
group,
`${rm.replace('/', '-')}-${id}`,
);
voteGroupWritten++;
}
console.log(` 🗄️ Persisted ${voteGroupWritten} voting group files`);
} catch (err) {
console.error(' ❌ Voting group download failed:', errorMessage(err));
}
// ── Step 6: Download World Bank indicators for Sweden ───────────────────
console.log('\n🌍 Step 6/7: Downloading World Bank indicators for Sweden...');
try {
const wb = new WorldBankClient();
const indicatorEntries = Object.entries(INDICATOR_IDS);
let wbWritten = 0;
let wbFailed = 0;
for (const [_name, indicatorId] of indicatorEntries) {
try {
const dataPoints = await wb.getIndicator(COUNTRY_CODES.sweden, indicatorId, 10);
Iif (dataPoints.length > 0) {
persistWorldBankData(indicatorId, COUNTRY_CODES.sweden, dataPoints);
wbWritten++;
}
} catch {
wbFailed++;
}
}
console.log(` ✅ Persisted ${wbWritten}/${indicatorEntries.length} World Bank indicators (${wbFailed} failed)`);
} catch (err) {
console.error(' ❌ World Bank data download failed:', errorMessage(err));
}
// ── Step 7: Download SCB statistics for key domains ─────────────────────
console.log('\n📊 Step 7/7: Downloading SCB statistics...');
try {
const scb = new SCBClient();
const domainsWithTables = SCB_DOMAINS.filter(d => d.tables.length > 0);
let scbWritten = 0;
let scbFailed = 0;
for (const domain of domainsWithTables) {
for (const tableId of domain.tables) {
try {
const data = await scb.getTableData(tableId);
Iif (data && (Array.isArray(data) ? data.length > 0 : true)) {
persistSCBData(tableId, data, { domain: domain.domain, query: domain.query });
scbWritten++;
}
} catch {
scbFailed++;
}
}
}
console.log(` ✅ Persisted ${scbWritten} SCB tables (${scbFailed} failed)`);
} catch (err) {
console.error(' ❌ SCB data download failed:', errorMessage(err));
}
console.log('\n✅ Analysis data population complete.');
console.log(` 📂 Data written to: ${getDataRoot()}`);
}
main().catch((err) => {
console.error('Fatal error:', err);
process.exit(1);
});
|