riksdagsmonitor 0.8.60 → 0.8.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/lib/cia/csv-utils.d.ts +75 -0
  2. package/dist/lib/cia/csv-utils.d.ts.map +1 -0
  3. package/dist/lib/cia/csv-utils.js +141 -0
  4. package/dist/lib/cia/csv-utils.js.map +1 -0
  5. package/dist/lib/cia/data-loader.d.ts +38 -348
  6. package/dist/lib/cia/data-loader.d.ts.map +1 -1
  7. package/dist/lib/cia/data-loader.js +54 -766
  8. package/dist/lib/cia/data-loader.js.map +1 -1
  9. package/dist/lib/cia/election-predictions.d.ts +1 -1
  10. package/dist/lib/cia/election-predictions.d.ts.map +1 -1
  11. package/dist/lib/cia/loaders/committees.d.ts +25 -0
  12. package/dist/lib/cia/loaders/committees.d.ts.map +1 -0
  13. package/dist/lib/cia/loaders/committees.js +110 -0
  14. package/dist/lib/cia/loaders/committees.js.map +1 -0
  15. package/dist/lib/cia/loaders/demographics.d.ts +22 -0
  16. package/dist/lib/cia/loaders/demographics.d.ts.map +1 -0
  17. package/dist/lib/cia/loaders/demographics.js +48 -0
  18. package/dist/lib/cia/loaders/demographics.js.map +1 -0
  19. package/dist/lib/cia/loaders/documents.d.ts +22 -0
  20. package/dist/lib/cia/loaders/documents.d.ts.map +1 -0
  21. package/dist/lib/cia/loaders/documents.js +51 -0
  22. package/dist/lib/cia/loaders/documents.js.map +1 -0
  23. package/dist/lib/cia/loaders/election.d.ts +24 -0
  24. package/dist/lib/cia/loaders/election.d.ts.map +1 -0
  25. package/dist/lib/cia/loaders/election.js +111 -0
  26. package/dist/lib/cia/loaders/election.js.map +1 -0
  27. package/dist/lib/cia/loaders/index.d.ts +26 -0
  28. package/dist/lib/cia/loaders/index.d.ts.map +1 -0
  29. package/dist/lib/cia/loaders/index.js +26 -0
  30. package/dist/lib/cia/loaders/index.js.map +1 -0
  31. package/dist/lib/cia/loaders/ministries.d.ts +22 -0
  32. package/dist/lib/cia/loaders/ministries.d.ts.map +1 -0
  33. package/dist/lib/cia/loaders/ministries.js +41 -0
  34. package/dist/lib/cia/loaders/ministries.js.map +1 -0
  35. package/dist/lib/cia/loaders/overview.d.ts +24 -0
  36. package/dist/lib/cia/loaders/overview.d.ts.map +1 -0
  37. package/dist/lib/cia/loaders/overview.js +96 -0
  38. package/dist/lib/cia/loaders/overview.js.map +1 -0
  39. package/dist/lib/cia/loaders/parties.d.ts +24 -0
  40. package/dist/lib/cia/loaders/parties.d.ts.map +1 -0
  41. package/dist/lib/cia/loaders/parties.js +92 -0
  42. package/dist/lib/cia/loaders/parties.js.map +1 -0
  43. package/dist/lib/cia/loaders/risk.d.ts +22 -0
  44. package/dist/lib/cia/loaders/risk.d.ts.map +1 -0
  45. package/dist/lib/cia/loaders/risk.js +38 -0
  46. package/dist/lib/cia/loaders/risk.js.map +1 -0
  47. package/dist/lib/cia/loaders/top10.d.ts +24 -0
  48. package/dist/lib/cia/loaders/top10.d.ts.map +1 -0
  49. package/dist/lib/cia/loaders/top10.js +68 -0
  50. package/dist/lib/cia/loaders/top10.js.map +1 -0
  51. package/dist/lib/cia/loaders/voting.d.ts +27 -0
  52. package/dist/lib/cia/loaders/voting.d.ts.map +1 -0
  53. package/dist/lib/cia/loaders/voting.js +108 -0
  54. package/dist/lib/cia/loaders/voting.js.map +1 -0
  55. package/dist/lib/cia/sources.d.ts +29 -0
  56. package/dist/lib/cia/sources.d.ts.map +1 -0
  57. package/dist/lib/cia/sources.js +162 -0
  58. package/dist/lib/cia/sources.js.map +1 -0
  59. package/dist/lib/cia/types.d.ts +324 -0
  60. package/dist/lib/cia/types.d.ts.map +1 -0
  61. package/dist/lib/cia/types.js +24 -0
  62. package/dist/lib/cia/types.js.map +1 -0
  63. package/dist/lib/cia/visualizations.d.ts +1 -1
  64. package/dist/lib/cia/visualizations.d.ts.map +1 -1
  65. package/package.json +1 -1
@@ -4,211 +4,69 @@
4
4
  *
5
5
  * @description
6
6
  * CIA Intelligence Data Loader & Pipeline Orchestrator.
7
- * Core data acquisition module implementing multi-source intelligence data loading
8
- * from the Citizen Intelligence Agency (CIA) Platform. Manages CSV export ingestion
9
- * for 19+ intelligence product categories and JSON fallback for model-generated
10
- * electoral forecasts. Provides resilient data pipeline with local-first strategy
11
- * and remote fallback capabilities.
7
+ *
8
+ * Thin orchestrator that wires together the per-domain loaders in `loaders/`
9
+ * with a single `LoadCSV` closure built from `csvBaseURL` + `fallbackURL`.
10
+ * Public API (constructor, static `CSV_SOURCES`, `parseCSV`, `loadCSV`,
11
+ * `load*()` methods, `loadAll`) is preserved for existing consumers
12
+ * (`dashboard-init.ts`, `election-predictions.ts`, `visualizations.ts`).
13
+ *
14
+ * Was originally a 1 300-line monolith; decomposed into focused modules:
15
+ * - `types.ts` — DTO interfaces
16
+ * - `sources.ts` — CSV source URL inventory + Riksdag constants
17
+ * - `csv-utils.ts` — `parseCSV` / `loadCSV` helpers
18
+ * - `loaders/*.ts` — one file per domain (10 loaders)
12
19
  *
13
20
  * @author Hack23 AB - Data Pipeline Engineering
14
21
  * @license Apache-2.0
15
- * @version 2.0.0
22
+ * @version 3.0.0
16
23
  * @since 2024
17
-
18
24
  *
19
25
  * @intelligence CIA Platform Data Pipeline Orchestrator — core data acquisition module implementing multi-source intelligence data loading from 19+ CIA product categories. Manages CSV export ingestion and JSON fallback for electoral forecasts. Provides resilient pipeline with local-first strategy and remote fallback.
20
26
  *
21
27
  * @business Data infrastructure investment — the CIA data pipeline is the foundation for all analytical products. Pipeline reliability directly impacts user experience and platform credibility. Modular architecture enables future data source expansion (European Parliament, Nordic councils).
22
28
  *
23
29
  * @marketing Data transparency asset — transparent data sourcing (CIA Platform, open government data) builds trust with all audience segments. Data pipeline documentation demonstrates commitment to accuracy and verifiability, key messaging for press and academic audiences.
24
- * */
30
+ */
31
+ import { COMMITTEE_DOCS_PER_MEETING_ESTIMATE, COMMITTEE_ORG_CODES, CSV_SOURCES, RIKSDAG_PARTIES } from './sources.js';
32
+ import { createLoadCSV, loadCSV as loadCSVFn, parseCSV as parseCSVFn } from './csv-utils.js';
33
+ import { loadCommitteeNetwork, loadDemographics, loadDocumentActivity, loadElectionAnalysis, loadMinistryDashboard, loadOverviewDashboard, loadPartyPerformance, loadRiskEvolution, loadTop10Influential, loadVotingPatterns } from './loaders/index.js';
25
34
  /* ------------------------------------------------------------------ */
26
- /* CIADataLoader class */
35
+ /* CIADataLoader class — thin orchestrator */
27
36
  /* ------------------------------------------------------------------ */
37
+ /**
38
+ * Aggregator that delegates per-domain loading to the dedicated modules in
39
+ * `loaders/`. Maintains the original public API so existing callers compile
40
+ * unchanged after the decomposition refactor.
41
+ */
28
42
  export class CIADataLoader {
29
43
  csvBaseURL;
30
44
  fallbackURL;
31
45
  /** The 8 parties represented in the Swedish Riksdag. */
32
- static RIKSDAG_PARTIES = ['S', 'M', 'SD', 'C', 'V', 'KD', 'L', 'MP'];
46
+ static RIKSDAG_PARTIES = RIKSDAG_PARTIES;
33
47
  /** Mapping of full Swedish committee names to their Riksdag org codes. */
34
- static COMMITTEE_ORG_CODES = {
35
- 'Konstitutionsutskottet': 'KU',
36
- 'Civilutskottet': 'CU',
37
- 'Trafikutskottet': 'TU',
38
- 'Näringsutskottet': 'NU',
39
- 'Miljö- och jordbruksutskottet': 'MJU',
40
- 'Utrikesutskottet': 'UU',
41
- 'Arbetsmarknadsutskottet': 'AU',
42
- 'Socialförsäkringsutskottet': 'SfU',
43
- 'Socialutskottet': 'SoU',
44
- 'Justitieutskottet': 'JuU',
45
- 'Skatteutskottet': 'SkU',
46
- 'EU-nämnden': 'EUN',
47
- 'Kulturutskottet': 'KrU',
48
- 'Utbildningsutskottet': 'UbU',
49
- 'Finansutskottet': 'FiU',
50
- 'Försvarsutskottet': 'FöU',
51
- 'Lagutskottet': 'LU',
52
- 'Bostadsutskottet': 'BoU'
53
- };
48
+ static COMMITTEE_ORG_CODES = COMMITTEE_ORG_CODES;
54
49
  /**
55
50
  * Heuristic divisor to estimate meetings/year from committee document counts.
56
51
  * Assumption: ~25 published documents per active committee meeting.
57
52
  */
58
- static COMMITTEE_DOCS_PER_MEETING_ESTIMATE = 25;
53
+ static COMMITTEE_DOCS_PER_MEETING_ESTIMATE = COMMITTEE_DOCS_PER_MEETING_ESTIMATE;
54
+ /** CSV data source definitions – maps to real PostgreSQL view exports. */
55
+ static CSV_SOURCES = CSV_SOURCES;
56
+ /** Loader closure bound to this instance's URL configuration. */
57
+ loadCSVFn;
59
58
  constructor() {
60
59
  this.csvBaseURL = '../cia-data/';
61
60
  this.fallbackURL = 'https://raw.githubusercontent.com/Hack23/cia/master/service.data.impl/sample-data/';
61
+ this.loadCSVFn = createLoadCSV(this.csvBaseURL, this.fallbackURL);
62
62
  }
63
- /** CSV data source definitions – maps to real PostgreSQL view exports. */
64
- static CSV_SOURCES = {
65
- personStatus: {
66
- local: 'distribution_person_status.csv',
67
- description: 'Active MP counts by status'
68
- },
69
- riskByParty: {
70
- local: 'distribution_risk_by_party.csv',
71
- description: 'Risk levels per party'
72
- },
73
- riskLevels: {
74
- local: 'distribution_politician_risk_levels.csv',
75
- description: 'Aggregate risk level distribution'
76
- },
77
- annualBallots: {
78
- local: 'voting/distribution_annual_ballots.csv',
79
- description: 'Annual ballot/vote counts'
80
- },
81
- crisisResilience: {
82
- local: 'risk/distribution_crisis_resilience.csv',
83
- description: 'Coalition stability/resilience scores'
84
- },
85
- partyPerformance: {
86
- local: 'party/distribution_party_performance.csv',
87
- description: 'Party metrics (docs, motions, performance level)'
88
- },
89
- partyMetrics: {
90
- local: 'party/view_party_performance_metrics_sample.csv',
91
- description: 'Full party metrics with win rate, rebel rate, absence rate'
92
- },
93
- partyMomentum: {
94
- local: 'party/distribution_party_momentum.csv',
95
- description: 'Party trend direction and stability'
96
- },
97
- partyMembers: {
98
- local: 'party/distribution_annual_party_members.csv',
99
- description: 'Annual party membership counts'
100
- },
101
- influenceMetrics: {
102
- local: 'politician/view_riksdagen_politician_influence_metrics_sample.csv',
103
- description: 'MP influence scores and network connections'
104
- },
105
- riskSummary: {
106
- local: 'politician/view_politician_risk_summary_sample.csv',
107
- description: 'MP risk scores and assessments'
108
- },
109
- committeeProductivity: {
110
- local: 'committee/distribution_committee_productivity.csv',
111
- description: 'Committee productivity and member counts'
112
- },
113
- committeeActivity: {
114
- local: 'committee/distribution_committee_activity.csv',
115
- description: 'Committee document counts'
116
- },
117
- partyEffectiveness: {
118
- local: 'party/distribution_party_effectiveness_trends.csv',
119
- description: 'Party effectiveness trends with win rate'
120
- },
121
- electionForecast: {
122
- local: 'election/election_forecast.csv',
123
- description: 'Election 2026 seat predictions per party'
124
- },
125
- coalitionScenarios: {
126
- local: 'election/coalition_scenarios.csv',
127
- description: 'Coalition scenario probability modeling'
128
- },
129
- coalitionAlignment: {
130
- local: 'party/distribution_coalition_alignment.csv',
131
- description: 'Real party-pair voting alignment rates'
132
- },
133
- genderByParty: {
134
- local: 'party/distribution_gender_by_party.csv',
135
- description: 'Gender distribution per party'
136
- },
137
- experienceByParty: {
138
- local: 'party/distribution_experience_by_party.csv',
139
- description: 'Experience levels per party'
140
- },
141
- ministryEffectiveness: {
142
- local: 'ministry/distribution_ministry_effectiveness.csv',
143
- description: 'Ministry effectiveness assessments'
144
- },
145
- annualDocTypes: {
146
- local: 'voting/distribution_annual_document_types.csv',
147
- description: 'Annual document type counts'
148
- },
149
- decisionTrends: {
150
- local: 'voting/distribution_decision_trends.csv',
151
- description: 'Decision approval trends over time'
152
- },
153
- electionRegions: {
154
- local: 'election/distribution_election_regions.csv',
155
- description: 'MPs per election region'
156
- },
157
- governmentRoles: {
158
- local: 'view_riksdagen_goverment_role_member_sample.csv',
159
- description: 'Government minister role assignments'
160
- },
161
- riskEvolution: {
162
- local: 'distribution_risk_evolution_temporal.csv',
163
- description: 'Risk score changes over time'
164
- },
165
- behavioralPatterns: {
166
- local: 'party/distribution_behavioral_patterns_by_party.csv',
167
- description: 'Behavioral risk patterns per party'
168
- }
169
- };
170
63
  /**
171
64
  * Parse CSV text into array of objects using header row as keys.
172
65
  * @param csvText - Raw CSV text
173
66
  * @returns Parsed rows
174
67
  */
175
68
  parseCSV(csvText) {
176
- const lines = csvText.trim().split('\n');
177
- if (lines.length < 2)
178
- return [];
179
- const headers = lines[0].split(',').map(h => h.trim().replace(/^"|"$/g, ''));
180
- const rows = [];
181
- for (let i = 1; i < lines.length; i++) {
182
- const line = lines[i].trim();
183
- if (!line)
184
- continue;
185
- // Simple CSV parsing (handles basic quoting)
186
- const values = [];
187
- let current = '';
188
- let inQuotes = false;
189
- for (let j = 0; j < line.length; j++) {
190
- const ch = line[j];
191
- if (ch === '"') {
192
- inQuotes = !inQuotes;
193
- }
194
- else if (ch === ',' && !inQuotes) {
195
- values.push(current.trim());
196
- current = '';
197
- }
198
- else {
199
- current += ch;
200
- }
201
- }
202
- values.push(current.trim());
203
- const row = {};
204
- headers.forEach((h, idx) => {
205
- const val = values[idx] || '';
206
- const num = Number(val);
207
- row[h] = val !== '' && !isNaN(num) ? num : val;
208
- });
209
- rows.push(row);
210
- }
211
- return rows;
69
+ return parseCSVFn(csvText);
212
70
  }
213
71
  /**
214
72
  * Load CSV with local-first fallback.
@@ -217,621 +75,51 @@ export class CIADataLoader {
217
75
  * @returns Parsed CSV rows
218
76
  */
219
77
  async loadCSV(localPath, fallbackPath) {
220
- const urls = [
221
- `${this.csvBaseURL}${localPath}`
222
- ];
223
- if (fallbackPath) {
224
- urls.push(`${this.fallbackURL}${fallbackPath}`);
225
- }
226
- for (const url of urls) {
227
- try {
228
- const response = await fetch(url);
229
- if (!response.ok)
230
- continue;
231
- const text = await response.text();
232
- const rows = this.parseCSV(text);
233
- if (rows.length > 0)
234
- return rows;
235
- }
236
- catch (e) {
237
- const message = e instanceof Error ? e.message : String(e);
238
- console.warn(`Failed to load CSV from ${url}:`, message);
239
- }
240
- }
241
- console.warn(`No data loaded for ${localPath}`);
242
- return [];
78
+ return loadCSVFn(this.csvBaseURL, this.fallbackURL, localPath, fallbackPath);
243
79
  }
244
- /**
245
- * Build overview dashboard from CSV sources.
246
- * Replaces overview-dashboard.json.
247
- */
80
+ /** Build overview dashboard from CSV sources. Replaces overview-dashboard.json. */
248
81
  async loadOverviewDashboard() {
249
- const [personStatus, riskByParty, riskLevels, annualBallots, resilience] = await Promise.all([
250
- this.loadCSV(CIADataLoader.CSV_SOURCES.personStatus.local),
251
- this.loadCSV(CIADataLoader.CSV_SOURCES.riskByParty.local),
252
- this.loadCSV(CIADataLoader.CSV_SOURCES.riskLevels.local),
253
- this.loadCSV(CIADataLoader.CSV_SOURCES.annualBallots.local),
254
- this.loadCSV(CIADataLoader.CSV_SOURCES.crisisResilience.local)
255
- ]);
256
- // Count active MPs
257
- const activeRow = personStatus.find(r => r.status === 'Tjänstgörande riksdagsledamot');
258
- const totalMPs = activeRow ? activeRow.person_count : 349;
259
- // Count unique parties from risk data (only real riksdag parties)
260
- const riksdagParties = CIADataLoader.RIKSDAG_PARTIES;
261
- const partiesInData = new Set(riskByParty.map(r => r.party).filter(p => riksdagParties.includes(p)));
262
- const totalParties = partiesInData.size || 8;
263
- // Risk alerts from risk_by_party
264
- const highRisk = riskByParty.filter(r => r.risk_level === 'HIGH');
265
- const medRisk = riskByParty.filter(r => r.risk_level === 'MEDIUM');
266
- const lowRisk = riskByParty.filter(r => r.risk_level === 'LOW');
267
- const critical = highRisk.reduce((sum, r) => sum + (r.politician_count || 0), 0);
268
- const major = medRisk.reduce((sum, r) => sum + (r.politician_count || 0), 0);
269
- const minor = lowRisk.reduce((sum, r) => sum + (r.politician_count || 0), 0);
270
- // Total risk rules from risk levels
271
- const totalRiskRules = riskLevels.length > 0
272
- ? riskLevels.reduce((sum, r) => sum + (r.politician_count || 0), 0)
273
- : 45;
274
- // Latest year ballot activity
275
- const latestBallot = annualBallots.length > 0
276
- ? annualBallots[annualBallots.length - 1]
277
- : {};
278
- // Coalition stability from resilience scores (Tidö = M, KD, L, SD)
279
- const tidoParties = ['M', 'KD', 'L', 'SD'];
280
- const tidoResilience = resilience.filter(r => tidoParties.includes(r.party));
281
- const avgResilience = tidoResilience.length > 0
282
- ? Math.round(tidoResilience.reduce((s, r) => s + (r.avg_resilience_score || 0), 0) / tidoResilience.length)
283
- : 72;
284
- return {
285
- title: 'Swedish Riksdag Overview Dashboard',
286
- description: 'Live intelligence from CIA PostgreSQL database exports',
287
- lastUpdated: new Date().toISOString(),
288
- keyMetrics: {
289
- totalMPs,
290
- totalParties,
291
- totalRiskRules,
292
- governmentCoalition: 'Tidö Agreement',
293
- coalitionSeats: 176,
294
- oppositionSeats: 173,
295
- majorityMargin: 1
296
- },
297
- riskAlerts: {
298
- critical,
299
- major,
300
- minor,
301
- last90Days: { critical, major, minor }
302
- },
303
- parliamentActivity: {
304
- votesLastMonth: latestBallot.total_votes || 0,
305
- documentsProcessed: latestBallot.unique_ballots || 0,
306
- motionsSubmitted: 0,
307
- committeeMeetings: 0
308
- },
309
- coalitionStability: {
310
- stabilityScore: avgResilience,
311
- riskLevel: avgResilience >= 70 ? 'moderate' : 'high',
312
- defectionProbability: 100 - avgResilience,
313
- ideologicalTension: avgResilience < 60 ? 'high' : 'moderate'
314
- },
315
- dataQuality: {
316
- completeness: 98.5,
317
- lastDataSync: new Date().toISOString(),
318
- coverage: '50+ years (1971-2026)'
319
- },
320
- _source: 'csv'
321
- };
82
+ return loadOverviewDashboard(this.loadCSVFn);
322
83
  }
323
- /**
324
- * Build election analysis from CSV sources.
325
- * Replaces election-analysis.json.
326
- */
84
+ /** Build election analysis from CSV sources. Replaces election-analysis.json. */
327
85
  async loadElectionAnalysis() {
328
- const [forecastRows, scenarioRows] = await Promise.all([
329
- this.loadCSV(CIADataLoader.CSV_SOURCES.electionForecast.local),
330
- this.loadCSV(CIADataLoader.CSV_SOURCES.coalitionScenarios.local)
331
- ]);
332
- const toFiniteNumber = (value) => {
333
- if (typeof value === 'number' && Number.isFinite(value))
334
- return value;
335
- if (typeof value === 'string' && value.trim() !== '') {
336
- const num = Number(value);
337
- if (Number.isFinite(num))
338
- return num;
339
- }
340
- return undefined;
341
- };
342
- const toBoolean = (value) => {
343
- if (typeof value === 'boolean')
344
- return value;
345
- if (typeof value === 'string') {
346
- const normalized = value.trim().toLowerCase();
347
- if (normalized === 'true')
348
- return true;
349
- if (normalized === 'false')
350
- return false;
351
- }
352
- return undefined;
353
- };
354
- const parties = forecastRows.flatMap(r => {
355
- const name = String(r.name ?? '').trim();
356
- const currentSeats = toFiniteNumber(r.currentSeats);
357
- const predictedSeats = toFiniteNumber(r.predictedSeats);
358
- const change = toFiniteNumber(r.change);
359
- const voteShare = toFiniteNumber(r.voteShare);
360
- if (!name || currentSeats === undefined || predictedSeats === undefined || change === undefined || voteShare === undefined) {
361
- return [];
362
- }
363
- const confidenceMin = toFiniteNumber(r.confidenceMin);
364
- const confidenceMax = toFiniteNumber(r.confidenceMax);
365
- return [{
366
- name,
367
- currentSeats,
368
- predictedSeats,
369
- change,
370
- voteShare,
371
- confidenceInterval: confidenceMin !== undefined && confidenceMax !== undefined
372
- ? { min: confidenceMin, max: confidenceMax }
373
- : undefined
374
- }];
375
- });
376
- const coalitionScenarios = scenarioRows.flatMap(r => {
377
- const name = String(r.name ?? '').trim();
378
- const probability = toFiniteNumber(r.probability);
379
- const totalSeats = toFiniteNumber(r.totalSeats);
380
- const majority = toBoolean(r.majority);
381
- const riskLevel = String(r.riskLevel ?? '').trim();
382
- const composition = String(r.composition ?? '')
383
- .split(',')
384
- .map(s => s.trim())
385
- .filter(Boolean);
386
- if (!name ||
387
- probability === undefined ||
388
- totalSeats === undefined ||
389
- majority === undefined ||
390
- !riskLevel ||
391
- composition.length === 0) {
392
- return [];
393
- }
394
- return [{
395
- name,
396
- probability,
397
- composition,
398
- totalSeats,
399
- majority,
400
- riskLevel
401
- }];
402
- });
403
- return {
404
- forecast: { parties },
405
- coalitionScenarios,
406
- keyFactors: [
407
- 'Economic conditions',
408
- 'Immigration policy',
409
- 'Climate change priorities',
410
- 'Healthcare reform',
411
- 'NATO membership impact'
412
- ],
413
- electionDate: '2026-09-13'
414
- };
86
+ return loadElectionAnalysis(this.loadCSVFn);
415
87
  }
416
- /**
417
- * Build party performance from CSV sources.
418
- * Replaces party-performance.json.
419
- */
88
+ /** Build party performance from CSV sources. Replaces party-performance.json. */
420
89
  async loadPartyPerformance() {
421
- const [performance, metrics, momentum] = await Promise.all([
422
- this.loadCSV(CIADataLoader.CSV_SOURCES.partyPerformance.local),
423
- this.loadCSV(CIADataLoader.CSV_SOURCES.partyMetrics.local),
424
- this.loadCSV(CIADataLoader.CSV_SOURCES.partyMomentum.local)
425
- ]);
426
- // Only include real riksdag parties
427
- const riksdagParties = CIADataLoader.RIKSDAG_PARTIES;
428
- const activePerformance = performance.filter(p => riksdagParties.includes(p.party));
429
- // Build a lookup from the detailed metrics
430
- const metricsMap = {};
431
- metrics.forEach(m => {
432
- if (riksdagParties.includes(m.party)) {
433
- metricsMap[m.party] = m;
434
- }
435
- });
436
- // Get latest momentum per party
437
- const latestMomentum = {};
438
- momentum
439
- .filter(m => riksdagParties.includes(m.party))
440
- .forEach(m => {
441
- const party = m.party;
442
- if (!latestMomentum[party] ||
443
- m.year > latestMomentum[party].year ||
444
- (m.year === latestMomentum[party].year &&
445
- m.quarter > latestMomentum[party].quarter)) {
446
- latestMomentum[party] = m;
447
- }
448
- });
449
- // Known seat counts (from 2022 election results)
450
- const seatMap = {
451
- S: 107, SD: 73, M: 68, C: 24, V: 24, KD: 19, L: 16, MP: 18
452
- };
453
- const parties = activePerformance.map(p => {
454
- const party = p.party;
455
- const m = metricsMap[party] || {};
456
- const mom = latestMomentum[party] || {};
457
- return {
458
- id: party,
459
- partyName: p.party_name || party,
460
- shortName: party,
461
- metrics: {
462
- seats: seatMap[party] || 0,
463
- voteShare: 0,
464
- memberCount: p.active_members || 0,
465
- documentsAuthored: p.documents_last_year || 0,
466
- motionsSubmitted: p.motions_last_year || 0,
467
- successRate: m.avg_win_rate || 0
468
- },
469
- voting: {
470
- totalVotes: m.total_votes_last_year || 0,
471
- cohesionScore: m.avg_participation_rate || 0,
472
- rebellionRate: m.avg_rebel_rate || 0
473
- },
474
- trends: {
475
- supportTrend: (mom.trend_direction || 'stable').toLowerCase(),
476
- activityTrend: (mom.stability_classification || 'stable').toLowerCase(),
477
- performanceLevel: m.performance_level || p.performance_level || ''
478
- },
479
- _source: 'csv'
480
- };
481
- });
482
- // Sort by seats descending
483
- parties.sort((a, b) => (b.metrics.seats || 0) - (a.metrics.seats || 0));
484
- return {
485
- title: 'Party Performance Dashboard',
486
- description: 'Live party data from CIA PostgreSQL database exports',
487
- lastUpdated: new Date().toISOString(),
488
- parties,
489
- _source: 'csv'
490
- };
90
+ return loadPartyPerformance(this.loadCSVFn);
491
91
  }
492
- /**
493
- * Build top 10 influential MPs from CSV sources.
494
- * Replaces top10-influential-mps.json.
495
- */
92
+ /** Build top 10 influential MPs from CSV sources. Replaces top10-influential-mps.json. */
496
93
  async loadTop10Influential() {
497
- const [influence, riskSummary] = await Promise.all([
498
- this.loadCSV(CIADataLoader.CSV_SOURCES.influenceMetrics.local),
499
- this.loadCSV(CIADataLoader.CSV_SOURCES.riskSummary.local)
500
- ]);
501
- // Build risk lookup by person_id
502
- const riskMap = {};
503
- riskSummary.forEach(r => {
504
- riskMap[r.person_id] = r;
505
- });
506
- // Sort by network_connections descending, take top 10
507
- const sorted = [...influence]
508
- .filter(mp => mp.network_connections > 0)
509
- .sort((a, b) => (b.network_connections || 0) - (a.network_connections || 0))
510
- .slice(0, 10);
511
- const rankings = sorted.map((mp, idx) => {
512
- const risk = riskMap[mp.person_id] || {};
513
- return {
514
- rank: idx + 1,
515
- id: String(mp.person_id),
516
- firstName: mp.first_name || '',
517
- lastName: mp.last_name || '',
518
- party: mp.party || '',
519
- role: mp.influence_classification
520
- ? mp.influence_classification
521
- .replace(/_/g, ' ')
522
- .toLowerCase()
523
- .replace(/\b\w/g, (c) => c.toUpperCase())
524
- : '',
525
- influenceScore: mp.network_connections || 0,
526
- networkConnections: mp.network_connections || 0,
527
- brokerClassification: mp.broker_classification || '',
528
- riskLevel: risk.risk_level || '',
529
- riskScore: risk.risk_score || 0,
530
- _source: 'csv'
531
- };
532
- });
533
- return {
534
- title: 'Top 10 Most Influential MPs',
535
- description: 'Network analysis from CIA politician influence metrics view',
536
- lastUpdated: new Date().toISOString(),
537
- methodology: 'Ranked by network_connections from view_riksdagen_politician_influence_metrics',
538
- rankings,
539
- _source: 'csv'
540
- };
94
+ return loadTop10Influential(this.loadCSVFn);
541
95
  }
542
- /**
543
- * Build committee network from CSV sources.
544
- * Replaces committee-network.json.
545
- * Filters out INACTIVE committees and deduplicates by name.
546
- */
96
+ /** Build committee network from CSV sources. Replaces committee-network.json. */
547
97
  async loadCommitteeNetwork() {
548
- const [productivity, activity] = await Promise.all([
549
- this.loadCSV(CIADataLoader.CSV_SOURCES.committeeProductivity.local),
550
- this.loadCSV(CIADataLoader.CSV_SOURCES.committeeActivity.local)
551
- ]);
552
- // Build activity lookup by org code
553
- const activityMap = {};
554
- activity.forEach(a => {
555
- activityMap[a.org] = a.document_count || 0;
556
- });
557
- // Use class-level committee name-to-org-code mapping
558
- const nameToOrgCode = CIADataLoader.COMMITTEE_ORG_CODES;
559
- // Deduplicate committees by name, keeping the entry with the most data
560
- const bestByName = {};
561
- productivity.forEach(c => {
562
- const name = c.committee_name;
563
- if (!name)
564
- return;
565
- const existing = bestByName[name];
566
- if (!existing || c.total_documents > existing.total_documents ||
567
- (c.total_documents === existing.total_documents &&
568
- c.total_members > existing.total_members)) {
569
- bestByName[name] = c;
570
- }
571
- });
572
- // Normalize committee metrics first so filtering and rendering use one consistent source.
573
- const committees = Object.values(bestByName)
574
- .map(c => {
575
- const name = c.committee_name;
576
- const code = nameToOrgCode[name] || name.substring(0, 3).toUpperCase();
577
- const totalDocuments = c.total_documents || 0;
578
- const activityDocs = activityMap[code] || 0;
579
- const documentsProcessed = Math.max(totalDocuments, activityDocs);
580
- const productivityLevel = c.productivity_level || '';
581
- return {
582
- id: code,
583
- name,
584
- memberCount: c.total_members || 0,
585
- influenceScore: c.docs_per_member
586
- ? Math.round(c.docs_per_member * 100)
587
- : 0,
588
- documentsProcessed,
589
- productivityLevel,
590
- meetingsPerYear: documentsProcessed > 0
591
- ? Math.round(documentsProcessed / CIADataLoader.COMMITTEE_DOCS_PER_MEETING_ESTIMATE)
592
- : 0,
593
- keyIssues: [productivityLevel || 'N/A'],
594
- _source: 'csv'
595
- };
596
- })
597
- .filter(c => {
598
- // Keep real committees only; skip generic node and inactive rows with no measured output.
599
- return (c.name !== 'Riksdagen' &&
600
- c.memberCount > 0 &&
601
- (c.productivityLevel !== 'INACTIVE' || c.documentsProcessed > 0));
602
- })
603
- .sort((a, b) => b.documentsProcessed - a.documentsProcessed);
604
- // Build simple network graph from committees
605
- const nodes = committees.map(c => ({
606
- id: c.id,
607
- name: c.name,
608
- size: c.influenceScore
609
- }));
610
- // Create edges between committees that share similar productivity levels
611
- const edges = [];
612
- for (let i = 0; i < committees.length; i++) {
613
- for (let j = i + 1; j < committees.length && edges.length < 10; j++) {
614
- if (committees[i].productivityLevel === committees[j].productivityLevel &&
615
- committees[i].productivityLevel !== 'INACTIVE') {
616
- edges.push({
617
- source: committees[i].id,
618
- target: committees[j].id,
619
- weight: Math.min(committees[i].documentsProcessed, committees[j].documentsProcessed),
620
- type: 'productivity_similarity'
621
- });
622
- }
623
- }
624
- }
625
- return {
626
- title: 'Committee Network Analysis',
627
- description: 'Committee data from CIA committee productivity view',
628
- lastUpdated: new Date().toISOString(),
629
- committees,
630
- networkGraph: { nodes, edges },
631
- crossCommitteeMPs: [],
632
- _source: 'csv'
633
- };
98
+ return loadCommitteeNetwork(this.loadCSVFn);
634
99
  }
635
100
  /**
636
101
  * Build voting patterns from CSV sources.
637
- * Uses real coalition alignment data for the agreement matrix
638
- * and party effectiveness trends for win rates.
102
+ * Uses real coalition alignment data for the agreement matrix when present;
103
+ * otherwise falls back to win-rate similarity.
639
104
  */
640
105
  async loadVotingPatterns() {
641
- const [coalitionAlignment, effectiveness, riskByParty] = await Promise.all([
642
- this.loadCSV(CIADataLoader.CSV_SOURCES.coalitionAlignment.local),
643
- this.loadCSV(CIADataLoader.CSV_SOURCES.partyEffectiveness.local),
644
- this.loadCSV(CIADataLoader.CSV_SOURCES.riskByParty.local)
645
- ]);
646
- const riksdagParties = CIADataLoader.RIKSDAG_PARTIES;
647
- const labels = riksdagParties;
648
- const partyNames = [
649
- 'Social Democrats', 'Moderates', 'Sweden Democrats', 'Centre',
650
- 'Left', 'Christian Democrats', 'Liberals', 'Green'
651
- ];
652
- // Build agreement matrix from real coalition alignment data
653
- const alignmentLookup = {};
654
- coalitionAlignment
655
- .filter(r => riksdagParties.includes(r.party1) && riksdagParties.includes(r.party2))
656
- .forEach(r => {
657
- const key1 = `${r.party1}:${r.party2}`;
658
- const key2 = `${r.party2}:${r.party1}`;
659
- const rate = Math.round((r.alignment_rate || 0) * 100);
660
- alignmentLookup[key1] = rate;
661
- alignmentLookup[key2] = rate;
662
- });
663
- // If real alignment data is available, use it; otherwise fall back to effectiveness-based
664
- const hasAlignmentData = Object.keys(alignmentLookup).length > 0;
665
- let agreementMatrix;
666
- if (hasAlignmentData) {
667
- agreementMatrix = labels.map(p1 => labels.map(p2 => {
668
- if (p1 === p2)
669
- return 100;
670
- return alignmentLookup[`${p1}:${p2}`] ?? 50;
671
- }));
672
- }
673
- else {
674
- // Fallback: build from win rate similarity
675
- const latestWinRate = {};
676
- effectiveness
677
- .filter(e => riksdagParties.includes(e.party))
678
- .forEach(e => {
679
- const party = e.party;
680
- if (!latestWinRate[party] ||
681
- e.year > latestWinRate[party].year ||
682
- (e.year === latestWinRate[party].year &&
683
- e.quarter > latestWinRate[party].quarter)) {
684
- latestWinRate[party] = e;
685
- }
686
- });
687
- agreementMatrix = labels.map(p1 => {
688
- const wr1 = latestWinRate[p1] ? latestWinRate[p1].avg_win_rate : 50;
689
- return labels.map(p2 => {
690
- if (p1 === p2)
691
- return 100;
692
- const wr2 = latestWinRate[p2] ? latestWinRate[p2].avg_win_rate : 50;
693
- return Math.max(0, Math.round(100 - Math.abs(wr1 - wr2)));
694
- });
695
- });
696
- }
697
- // Rebellion tracking from risk data (HIGH risk ~ rebellious)
698
- const rebellionTracking = riksdagParties
699
- .map(party => {
700
- const partyRisks = riskByParty.filter(r => r.party === party);
701
- const highRisk = partyRisks.find(r => r.risk_level === 'HIGH');
702
- const total = partyRisks.reduce((s, r) => s + (r.politician_count || 0), 0);
703
- const highCount = highRisk ? highRisk.politician_count : 0;
704
- const rebellionRate = total > 0 ? Math.round((highCount / total) * 100 * 10) / 10 : 0;
705
- return {
706
- party,
707
- rebellionRate,
708
- trend: rebellionRate > 25 ? 'increasing' : rebellionRate > 15 ? 'stable' : 'decreasing'
709
- };
710
- })
711
- .filter(r => r.rebellionRate > 0);
712
- return {
713
- title: 'Voting Patterns Analysis',
714
- description: hasAlignmentData
715
- ? 'Real coalition alignment data from CIA voting analysis'
716
- : 'Derived from CIA party effectiveness trends and risk data',
717
- lastUpdated: new Date().toISOString(),
718
- analysisPeriod: '2022-2026',
719
- votingMatrix: { labels, partyNames, agreementMatrix },
720
- keyIssues: [],
721
- rebellionTracking,
722
- _source: 'csv'
723
- };
106
+ return loadVotingPatterns(this.loadCSVFn);
724
107
  }
725
- /**
726
- * Build ministry dashboard from CSV sources.
727
- */
108
+ /** Build ministry dashboard from CSV sources. */
728
109
  async loadMinistryDashboard() {
729
- const rows = await this.loadCSV(CIADataLoader.CSV_SOURCES.ministryEffectiveness.local);
730
- const ministries = rows
731
- .filter(r => r.ministry_name && r.documents_produced > 0)
732
- .map(r => ({
733
- name: r.ministry_name,
734
- effectiveness: r.effectiveness_assessment || '',
735
- documentsProduced: r.documents_produced || 0,
736
- governmentBills: r.government_bills || 0,
737
- year: r.year || 0,
738
- quarter: r.quarter || 0
739
- }))
740
- .sort((a, b) => b.documentsProduced - a.documentsProduced);
741
- return {
742
- title: 'Ministry Performance',
743
- description: 'Ministry effectiveness from CIA database exports',
744
- lastUpdated: new Date().toISOString(),
745
- ministries,
746
- _source: 'csv'
747
- };
110
+ return loadMinistryDashboard(this.loadCSVFn);
748
111
  }
749
- /**
750
- * Build demographics dashboard from CSV sources.
751
- */
112
+ /** Build demographics dashboard from CSV sources. */
752
113
  async loadDemographics() {
753
- const riksdagParties = CIADataLoader.RIKSDAG_PARTIES;
754
- const [genderRows, experienceRows] = await Promise.all([
755
- this.loadCSV(CIADataLoader.CSV_SOURCES.genderByParty.local),
756
- this.loadCSV(CIADataLoader.CSV_SOURCES.experienceByParty.local)
757
- ]);
758
- const genderByParty = genderRows
759
- .filter(r => riksdagParties.includes(r.party))
760
- .map(r => ({
761
- party: r.party,
762
- gender: r.gender,
763
- count: r.count || 0
764
- }));
765
- const experienceByParty = experienceRows
766
- .filter(r => riksdagParties.includes(r.party))
767
- .map(r => ({
768
- party: r.party,
769
- experienceLevel: r.experience_level || '',
770
- politicianCount: r.politician_count || 0
771
- }));
772
- return {
773
- title: 'Parliamentary Demographics',
774
- description: 'Gender and experience distribution from CIA database exports',
775
- lastUpdated: new Date().toISOString(),
776
- genderByParty,
777
- experienceByParty,
778
- _source: 'csv'
779
- };
114
+ return loadDemographics(this.loadCSVFn);
780
115
  }
781
- /**
782
- * Build document activity dashboard from CSV sources.
783
- */
116
+ /** Build document activity dashboard from CSV sources. */
784
117
  async loadDocumentActivity() {
785
- const [docTypeRows, decisionRows] = await Promise.all([
786
- this.loadCSV(CIADataLoader.CSV_SOURCES.annualDocTypes.local),
787
- this.loadCSV(CIADataLoader.CSV_SOURCES.decisionTrends.local)
788
- ]);
789
- const documentTypes = docTypeRows
790
- .filter(r => r.doc_count > 0)
791
- .map(r => ({
792
- year: r.year || 0,
793
- documentType: r.document_type || '',
794
- docCount: r.doc_count || 0
795
- }));
796
- const decisionTrends = decisionRows
797
- .filter(r => r.decision_count > 0)
798
- .map(r => ({
799
- year: r.year || 0,
800
- month: r.month || 0,
801
- decisionCount: r.decision_count || 0,
802
- approvedDecisions: r.approved_decisions || 0,
803
- rejectedDecisions: r.rejected_decisions || 0,
804
- approvalRate: r.approval_rate || 0
805
- }));
806
- return {
807
- title: 'Parliamentary Document Activity',
808
- description: 'Document production and decision trends from CIA database exports',
809
- lastUpdated: new Date().toISOString(),
810
- documentTypes,
811
- decisionTrends,
812
- _source: 'csv'
813
- };
118
+ return loadDocumentActivity(this.loadCSVFn);
814
119
  }
815
- /**
816
- * Build risk evolution dashboard from CSV sources.
817
- */
120
+ /** Build risk evolution dashboard from CSV sources. */
818
121
  async loadRiskEvolution() {
819
- const rows = await this.loadCSV(CIADataLoader.CSV_SOURCES.riskEvolution.local);
820
- const entries = rows
821
- .filter(r => r.politician_count > 0)
822
- .map(r => ({
823
- period: r.assessment_period || '',
824
- severity: r.risk_severity || '',
825
- politicianCount: r.politician_count || 0,
826
- avgRiskScore: r.avg_risk_score || 0
827
- }));
828
- return {
829
- title: 'Risk Score Evolution',
830
- description: 'Temporal risk score changes from CIA database exports',
831
- lastUpdated: new Date().toISOString(),
832
- entries,
833
- _source: 'csv'
834
- };
122
+ return loadRiskEvolution(this.loadCSVFn);
835
123
  }
836
124
  /**
837
125
  * Load all data in parallel.