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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 | /**
* @module CIA/DataLoader
* @category Intelligence Platform - Data Acquisition & Pipeline Management
*
* @description
* CIA Intelligence Data Loader & Pipeline Orchestrator.
* Core data acquisition module implementing multi-source intelligence data loading
* from the Citizen Intelligence Agency (CIA) Platform. Manages CSV export ingestion
* for 19+ intelligence product categories and JSON fallback for model-generated
* electoral forecasts. Provides resilient data pipeline with local-first strategy
* and remote fallback capabilities.
*
* @author Hack23 AB - Data Pipeline Engineering
* @license Apache-2.0
* @version 2.0.0
* @since 2024
*
* @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.
*
* @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).
*
* @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.
* */
/* ------------------------------------------------------------------ */
/* Interfaces */
/* ------------------------------------------------------------------ */
/** Definition for a single CSV data source mapping. */
export interface CSVSourceDefinition {
/** Relative path within the csvBaseURL directory. */
local: string;
/** Human-readable description of the data product. */
description: string;
}
/** Map of all known CSV source categories. */
export interface CSVSourceMap {
personStatus: CSVSourceDefinition;
riskByParty: CSVSourceDefinition;
riskLevels: CSVSourceDefinition;
annualBallots: CSVSourceDefinition;
crisisResilience: CSVSourceDefinition;
partyPerformance: CSVSourceDefinition;
partyMetrics: CSVSourceDefinition;
partyMomentum: CSVSourceDefinition;
partyMembers: CSVSourceDefinition;
influenceMetrics: CSVSourceDefinition;
riskSummary: CSVSourceDefinition;
committeeProductivity: CSVSourceDefinition;
committeeActivity: CSVSourceDefinition;
partyEffectiveness: CSVSourceDefinition;
}
/** A single parsed CSV row (header-keyed, auto-typed values). */
export interface CSVRow {
[key: string]: string | number;
}
/* ── Overview dashboard shapes ── */
export interface KeyMetrics {
totalMPs: number;
totalParties: number;
totalRiskRules: number;
governmentCoalition: string;
coalitionSeats: number;
oppositionSeats: number;
majorityMargin: number;
}
export interface RiskAlerts {
critical: number;
major: number;
minor: number;
last90Days: { critical: number; major: number; minor: number };
}
export interface ParliamentActivity {
votesLastMonth: number;
documentsProcessed: number;
motionsSubmitted: number;
committeeMeetings: number;
}
export interface CoalitionStability {
stabilityScore: number;
riskLevel: string;
defectionProbability: number;
ideologicalTension: string;
}
export interface DataQuality {
completeness: number;
lastDataSync: string;
coverage: string;
}
export interface OverviewDashboard {
title: string;
description: string;
lastUpdated: string;
keyMetrics: KeyMetrics;
riskAlerts: RiskAlerts;
parliamentActivity: ParliamentActivity;
coalitionStability: CoalitionStability;
dataQuality: DataQuality;
_source: string;
}
/* ── Party performance shapes ── */
export interface PartyMetricsData {
seats: number;
voteShare: number;
memberCount: number;
documentsAuthored: number;
motionsSubmitted: number;
successRate: number;
}
export interface PartyVoting {
totalVotes: number;
cohesionScore: number;
rebellionRate: number;
}
export interface PartyTrends {
supportTrend: string;
activityTrend: string;
performanceLevel: string;
}
export interface PartyEntry {
id: string;
partyName: string;
shortName: string;
metrics: PartyMetricsData;
voting: PartyVoting;
trends: PartyTrends;
_source: string;
}
export interface PartyPerformance {
title: string;
description: string;
lastUpdated: string;
parties: PartyEntry[];
_source: string;
}
/* ── Top-10 shapes ── */
export interface MPRanking {
rank: number;
id: string;
firstName: string;
lastName: string;
party: string;
role: string;
influenceScore: number;
networkConnections: number;
brokerClassification: string;
riskLevel: string;
riskScore: number;
_source: string;
}
export interface Top10Influential {
title: string;
description: string;
lastUpdated: string;
methodology: string;
rankings: MPRanking[];
_source: string;
}
/* ── Committee network shapes ── */
export interface CommitteeEntry {
id: string;
name: string;
memberCount: number;
influenceScore: number;
documentsProcessed: number;
productivityLevel: string;
meetingsPerYear: number;
keyIssues: string[];
_source: string;
}
export interface NetworkNode {
id: string;
name: string;
size: number;
}
export interface NetworkEdge {
source: string;
target: string;
weight: number;
type: string;
}
export interface CommitteeNetwork {
title: string;
description: string;
lastUpdated: string;
committees: CommitteeEntry[];
networkGraph: { nodes: NetworkNode[]; edges: NetworkEdge[] };
crossCommitteeMPs: unknown[];
_source: string;
}
/* ── Voting patterns shapes ── */
export interface VotingMatrix {
labels: string[];
partyNames: string[];
agreementMatrix: number[][];
}
export interface RebellionEntry {
party: string;
rebellionRate: number;
trend: string;
}
export interface VotingPatterns {
title: string;
description: string;
lastUpdated: string;
analysisPeriod: string;
votingMatrix: VotingMatrix;
keyIssues: unknown[];
rebellionTracking: RebellionEntry[];
_source: string;
}
/* ── Election analysis (JSON model) ── */
export interface ElectionAnalysis {
forecast: {
parties: Array<{
name: string;
currentSeats: number;
predictedSeats: number;
change: number;
voteShare: number;
confidenceInterval?: { min: number; max: number };
}>;
};
coalitionScenarios: Array<{
name: string;
composition: string[];
totalSeats: number;
probability: number;
majority: boolean;
riskLevel: string;
}>;
keyFactors: string[];
electionDate?: string;
}
/* ── Aggregate payload ── */
export interface CIADataPayload {
overview: OverviewDashboard;
election: ElectionAnalysis;
partyPerf: PartyPerformance;
top10: Top10Influential;
committees: CommitteeNetwork;
votingPatterns: VotingPatterns;
}
/* ------------------------------------------------------------------ */
/* CIADataLoader class */
/* ------------------------------------------------------------------ */
export class CIADataLoader {
readonly csvBaseURL: string;
readonly jsonBaseURL: string;
readonly fallbackURL: string;
constructor() {
this.csvBaseURL = '../cia-data/';
this.jsonBaseURL = '../data/cia-exports/current/';
this.fallbackURL = 'https://raw.githubusercontent.com/Hack23/cia/master/service.data.impl/sample-data/';
}
/** CSV data source definitions – maps to real PostgreSQL view exports. */
static CSV_SOURCES: CSVSourceMap = {
personStatus: {
local: 'distribution_person_status.csv',
description: 'Active MP counts by status'
},
riskByParty: {
local: 'distribution_risk_by_party.csv',
description: 'Risk levels per party'
},
riskLevels: {
local: 'distribution_politician_risk_levels.csv',
description: 'Aggregate risk level distribution'
},
annualBallots: {
local: 'voting/distribution_annual_ballots.csv',
description: 'Annual ballot/vote counts'
},
crisisResilience: {
local: 'risk/distribution_crisis_resilience.csv',
description: 'Coalition stability/resilience scores'
},
partyPerformance: {
local: 'party/distribution_party_performance.csv',
description: 'Party metrics (docs, motions, performance level)'
},
partyMetrics: {
local: 'party/view_party_performance_metrics_sample.csv',
description: 'Full party metrics with win rate, rebel rate, absence rate'
},
partyMomentum: {
local: 'party/distribution_party_momentum.csv',
description: 'Party trend direction and stability'
},
partyMembers: {
local: 'party/distribution_annual_party_members.csv',
description: 'Annual party membership counts'
},
influenceMetrics: {
local: 'politician/view_riksdagen_politician_influence_metrics_sample.csv',
description: 'MP influence scores and network connections'
},
riskSummary: {
local: 'politician/view_politician_risk_summary_sample.csv',
description: 'MP risk scores and assessments'
},
committeeProductivity: {
local: 'committee/distribution_committee_productivity.csv',
description: 'Committee productivity and member counts'
},
committeeActivity: {
local: 'committee/distribution_committee_activity.csv',
description: 'Committee document counts'
},
partyEffectiveness: {
local: 'party/distribution_party_effectiveness_trends.csv',
description: 'Party effectiveness trends with win rate'
}
};
/**
* Parse CSV text into array of objects using header row as keys.
* @param csvText - Raw CSV text
* @returns Parsed rows
*/
parseCSV(csvText: string): CSVRow[] {
const lines = csvText.trim().split('\n');
if (lines.length < 2) return [];
const headers = lines[0].split(',').map(h => h.trim().replace(/^"|"$/g, ''));
const rows: CSVRow[] = [];
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue;
// Simple CSV parsing (handles basic quoting)
const values: string[] = [];
let current = '';
let inQuotes = false;
for (let j = 0; j < line.length; j++) {
const ch = line[j];
if (ch === '"') {
inQuotes = !inQuotes;
} else if (ch === ',' && !inQuotes) {
values.push(current.trim());
current = '';
} else {
current += ch;
}
}
values.push(current.trim());
const row: CSVRow = {};
headers.forEach((h, idx) => {
const val = values[idx] || '';
const num = Number(val);
row[h] = val !== '' && !isNaN(num) ? num : val;
});
rows.push(row);
}
return rows;
}
/**
* Load CSV with local-first fallback.
* @param localPath - Path relative to csvBaseURL
* @param fallbackPath - Optional fallback path
* @returns Parsed CSV rows
*/
async loadCSV(localPath: string, fallbackPath?: string): Promise<CSVRow[]> {
const urls: string[] = [
`${this.csvBaseURL}${localPath}`
];
if (fallbackPath) {
urls.push(`${this.fallbackURL}${fallbackPath}`);
}
for (const url of urls) {
try {
const response = await fetch(url);
if (!response.ok) continue;
const text = await response.text();
const rows = this.parseCSV(text);
if (rows.length > 0) return rows;
} catch (e: unknown) {
const message = e instanceof Error ? e.message : String(e);
console.warn(`Failed to load CSV from ${url}:`, message);
}
}
console.warn(`No data loaded for ${localPath}`);
return [];
}
/**
* Load JSON with fallback (for election predictions only).
* @param filename - JSON filename
* @returns Parsed JSON
*/
async loadJSON<T = unknown>(filename: string): Promise<T> {
const urls: string[] = [
`${this.jsonBaseURL}${filename}`
];
for (const url of urls) {
try {
const response = await fetch(url);
if (!response.ok) continue;
return (await response.json()) as T;
} catch (e: unknown) {
const message = e instanceof Error ? e.message : String(e);
console.warn(`Failed to load JSON from ${url}:`, message);
}
}
throw new Error(`Failed to load ${filename}`);
}
/**
* Build overview dashboard from CSV sources.
* Replaces overview-dashboard.json.
*/
async loadOverviewDashboard(): Promise<OverviewDashboard> {
const [personStatus, riskByParty, riskLevels, annualBallots, resilience] = await Promise.all([
this.loadCSV(CIADataLoader.CSV_SOURCES.personStatus.local),
this.loadCSV(CIADataLoader.CSV_SOURCES.riskByParty.local),
this.loadCSV(CIADataLoader.CSV_SOURCES.riskLevels.local),
this.loadCSV(CIADataLoader.CSV_SOURCES.annualBallots.local),
this.loadCSV(CIADataLoader.CSV_SOURCES.crisisResilience.local)
]);
// Count active MPs
const activeRow = personStatus.find(r => r.status === 'Tjänstgörande riksdagsledamot');
const totalMPs = activeRow ? (activeRow.person_count as number) : 349;
// Count unique parties from risk data (only real riksdag parties)
const riksdagParties = ['S', 'M', 'SD', 'C', 'V', 'KD', 'L', 'MP'];
const partiesInData = new Set(
riskByParty.map(r => r.party as string).filter(p => riksdagParties.includes(p))
);
const totalParties = partiesInData.size || 8;
// Risk alerts from risk_by_party
const highRisk = riskByParty.filter(r => r.risk_level === 'HIGH');
const medRisk = riskByParty.filter(r => r.risk_level === 'MEDIUM');
const lowRisk = riskByParty.filter(r => r.risk_level === 'LOW');
const critical = highRisk.reduce((sum, r) => sum + ((r.politician_count as number) || 0), 0);
const major = medRisk.reduce((sum, r) => sum + ((r.politician_count as number) || 0), 0);
const minor = lowRisk.reduce((sum, r) => sum + ((r.politician_count as number) || 0), 0);
// Total risk rules from risk levels
const totalRiskRules = riskLevels.length > 0
? riskLevels.reduce((sum, r) => sum + ((r.politician_count as number) || 0), 0)
: 45;
// Latest year ballot activity
const latestBallot: CSVRow = annualBallots.length > 0
? annualBallots[annualBallots.length - 1]
: {};
// Coalition stability from resilience scores (Tidö = M, KD, L, SD)
const tidoParties = ['M', 'KD', 'L', 'SD'];
const tidoResilience = resilience.filter(r => tidoParties.includes(r.party as string));
const avgResilience = tidoResilience.length > 0
? Math.round(tidoResilience.reduce((s, r) => s + ((r.avg_resilience_score as number) || 0), 0) / tidoResilience.length)
: 72;
return {
title: 'Swedish Riksdag Overview Dashboard',
description: 'Live intelligence from CIA PostgreSQL database exports',
lastUpdated: new Date().toISOString(),
keyMetrics: {
totalMPs,
totalParties,
totalRiskRules,
governmentCoalition: 'Tidö Agreement',
coalitionSeats: 176,
oppositionSeats: 173,
majorityMargin: 1
},
riskAlerts: {
critical,
major,
minor,
last90Days: { critical, major, minor }
},
parliamentActivity: {
votesLastMonth: (latestBallot.total_votes as number) || 0,
documentsProcessed: (latestBallot.unique_ballots as number) || 0,
motionsSubmitted: 0,
committeeMeetings: 0
},
coalitionStability: {
stabilityScore: avgResilience,
riskLevel: avgResilience >= 70 ? 'moderate' : 'high',
defectionProbability: 100 - avgResilience,
ideologicalTension: avgResilience < 60 ? 'high' : 'moderate'
},
dataQuality: {
completeness: 98.5,
lastDataSync: new Date().toISOString(),
coverage: '50+ years (1971-2026)'
},
_source: 'csv'
};
}
/** Load election analysis – kept as JSON (model-generated predictions). */
async loadElectionAnalysis(): Promise<ElectionAnalysis> {
return this.loadJSON<ElectionAnalysis>('election-analysis.json');
}
/**
* Build party performance from CSV sources.
* Replaces party-performance.json.
*/
async loadPartyPerformance(): Promise<PartyPerformance> {
const [performance, metrics, momentum] = await Promise.all([
this.loadCSV(CIADataLoader.CSV_SOURCES.partyPerformance.local),
this.loadCSV(CIADataLoader.CSV_SOURCES.partyMetrics.local),
this.loadCSV(CIADataLoader.CSV_SOURCES.partyMomentum.local)
]);
// Only include real riksdag parties
const riksdagParties = ['S', 'M', 'SD', 'C', 'V', 'KD', 'L', 'MP'];
const activePerformance = performance.filter(p => riksdagParties.includes(p.party as string));
// Build a lookup from the detailed metrics
const metricsMap: Record<string, CSVRow> = {};
metrics.forEach(m => {
if (riksdagParties.includes(m.party as string)) {
metricsMap[m.party as string] = m;
}
});
// Get latest momentum per party
const latestMomentum: Record<string, CSVRow> = {};
momentum
.filter(m => riksdagParties.includes(m.party as string))
.forEach(m => {
const party = m.party as string;
if (
!latestMomentum[party] ||
(m.year as number) > (latestMomentum[party].year as number) ||
((m.year as number) === (latestMomentum[party].year as number) &&
(m.quarter as number) > (latestMomentum[party].quarter as number))
) {
latestMomentum[party] = m;
}
});
// Known seat counts (from 2022 election results)
const seatMap: Record<string, number> = {
S: 107, SD: 73, M: 68, C: 24, V: 24, KD: 19, L: 16, MP: 18
};
const parties: PartyEntry[] = activePerformance.map(p => {
const party = p.party as string;
const m = metricsMap[party] || {};
const mom = latestMomentum[party] || {};
return {
id: party,
partyName: (p.party_name as string) || party,
shortName: party,
metrics: {
seats: seatMap[party] || 0,
voteShare: 0,
memberCount: (p.active_members as number) || 0,
documentsAuthored: (p.documents_last_year as number) || 0,
motionsSubmitted: (p.motions_last_year as number) || 0,
successRate: (m.avg_win_rate as number) || 0
},
voting: {
totalVotes: (m.total_votes_last_year as number) || 0,
cohesionScore: (m.avg_participation_rate as number) || 0,
rebellionRate: (m.avg_rebel_rate as number) || 0
},
trends: {
supportTrend: ((mom.trend_direction as string) || 'stable').toLowerCase(),
activityTrend: ((mom.stability_classification as string) || 'stable').toLowerCase(),
performanceLevel: (m.performance_level as string) || (p.performance_level as string) || ''
},
_source: 'csv'
};
});
// Sort by seats descending
parties.sort((a, b) => (b.metrics.seats || 0) - (a.metrics.seats || 0));
return {
title: 'Party Performance Dashboard',
description: 'Live party data from CIA PostgreSQL database exports',
lastUpdated: new Date().toISOString(),
parties,
_source: 'csv'
};
}
/**
* Build top 10 influential MPs from CSV sources.
* Replaces top10-influential-mps.json.
*/
async loadTop10Influential(): Promise<Top10Influential> {
const [influence, riskSummary] = await Promise.all([
this.loadCSV(CIADataLoader.CSV_SOURCES.influenceMetrics.local),
this.loadCSV(CIADataLoader.CSV_SOURCES.riskSummary.local)
]);
// Build risk lookup by person_id
const riskMap: Record<string, CSVRow> = {};
riskSummary.forEach(r => {
riskMap[r.person_id as string] = r;
});
// Sort by network_connections descending, take top 10
const sorted = [...influence]
.filter(mp => (mp.network_connections as number) > 0)
.sort((a, b) => ((b.network_connections as number) || 0) - ((a.network_connections as number) || 0))
.slice(0, 10);
const rankings: MPRanking[] = sorted.map((mp, idx) => {
const risk = riskMap[mp.person_id as string] || {};
return {
rank: idx + 1,
id: String(mp.person_id),
firstName: (mp.first_name as string) || '',
lastName: (mp.last_name as string) || '',
party: (mp.party as string) || '',
role: (mp.influence_classification as string)
? (mp.influence_classification as string)
.replace(/_/g, ' ')
.toLowerCase()
.replace(/\b\w/g, (c: string) => c.toUpperCase())
: '',
influenceScore: (mp.network_connections as number) || 0,
networkConnections: (mp.network_connections as number) || 0,
brokerClassification: (mp.broker_classification as string) || '',
riskLevel: (risk.risk_level as string) || '',
riskScore: (risk.risk_score as number) || 0,
_source: 'csv'
};
});
return {
title: 'Top 10 Most Influential MPs',
description: 'Network analysis from CIA politician influence metrics view',
lastUpdated: new Date().toISOString(),
methodology: 'Ranked by network_connections from view_riksdagen_politician_influence_metrics',
rankings,
_source: 'csv'
};
}
/**
* Build committee network from CSV sources.
* Replaces committee-network.json.
*/
async loadCommitteeNetwork(): Promise<CommitteeNetwork> {
const [productivity, activity] = await Promise.all([
this.loadCSV(CIADataLoader.CSV_SOURCES.committeeProductivity.local),
this.loadCSV(CIADataLoader.CSV_SOURCES.committeeActivity.local)
]);
// Build activity lookup by org code
const activityMap: Record<string, number> = {};
activity.forEach(a => {
activityMap[a.org as string] = (a.document_count as number) || 0;
});
// Map committee names to codes
const committees: CommitteeEntry[] = productivity
.filter(c => c.committee_name && (c.total_members as number) > 0)
.map(c => {
const code = (c.committee_name as string).substring(0, 3).toUpperCase();
return {
id: code,
name: c.committee_name as string,
memberCount: (c.total_members as number) || 0,
influenceScore: c.docs_per_member
? Math.round((c.docs_per_member as number) * 100)
: 0,
documentsProcessed: (c.total_documents as number) || 0,
productivityLevel: (c.productivity_level as string) || '',
meetingsPerYear: 0,
keyIssues: [(c.productivity_level as string) || 'N/A'],
_source: 'csv'
};
});
// Build simple network graph from committees
const nodes: NetworkNode[] = committees.map(c => ({
id: c.id,
name: c.name,
size: c.influenceScore
}));
// Create edges between committees that share similar productivity levels
const edges: NetworkEdge[] = [];
for (let i = 0; i < committees.length; i++) {
for (let j = i + 1; j < committees.length && edges.length < 10; j++) {
if (
committees[i].productivityLevel === committees[j].productivityLevel &&
committees[i].productivityLevel !== 'INACTIVE'
) {
edges.push({
source: committees[i].id,
target: committees[j].id,
weight: Math.min(committees[i].documentsProcessed, committees[j].documentsProcessed),
type: 'productivity_similarity'
});
}
}
}
return {
title: 'Committee Network Analysis',
description: 'Committee data from CIA committee productivity view',
lastUpdated: new Date().toISOString(),
committees,
networkGraph: { nodes, edges },
crossCommitteeMPs: [],
_source: 'csv'
};
}
/**
* Build voting patterns from CSV sources.
* Replaces voting-patterns.json.
* Uses party effectiveness trends since coalition_alignment CSV is empty.
*/
async loadVotingPatterns(): Promise<VotingPatterns> {
const [effectiveness, riskByParty] = await Promise.all([
this.loadCSV(CIADataLoader.CSV_SOURCES.partyEffectiveness.local),
this.loadCSV(CIADataLoader.CSV_SOURCES.riskByParty.local)
]);
const riksdagParties = ['S', 'M', 'SD', 'C', 'V', 'KD', 'L', 'MP'];
const labels = riksdagParties;
const partyNames = [
'Social Democrats', 'Moderates', 'Sweden Democrats', 'Centre',
'Left', 'Christian Democrats', 'Liberals', 'Green'
];
// Build latest win rate per party from effectiveness trends
const latestWinRate: Record<string, CSVRow> = {};
effectiveness
.filter(e => riksdagParties.includes(e.party as string))
.forEach(e => {
const party = e.party as string;
if (
!latestWinRate[party] ||
(e.year as number) > (latestWinRate[party].year as number) ||
((e.year as number) === (latestWinRate[party].year as number) &&
(e.quarter as number) > (latestWinRate[party].quarter as number))
) {
latestWinRate[party] = e;
}
});
// Build agreement matrix: parties with similar win rates are more aligned
const agreementMatrix: number[][] = labels.map(p1 => {
const wr1 = latestWinRate[p1] ? (latestWinRate[p1].avg_win_rate as number) : 50;
return labels.map(p2 => {
if (p1 === p2) return 100;
const wr2 = latestWinRate[p2] ? (latestWinRate[p2].avg_win_rate as number) : 50;
return Math.max(0, Math.round(100 - Math.abs(wr1 - wr2)));
});
});
// Rebellion tracking from risk data (HIGH risk ~ rebellious)
const rebellionTracking: RebellionEntry[] = riksdagParties
.map(party => {
const partyRisks = riskByParty.filter(r => r.party === party);
const highRisk = partyRisks.find(r => r.risk_level === 'HIGH');
const total = partyRisks.reduce((s, r) => s + ((r.politician_count as number) || 0), 0);
const highCount = highRisk ? (highRisk.politician_count as number) : 0;
const rebellionRate = total > 0 ? Math.round((highCount / total) * 100 * 10) / 10 : 0;
return {
party,
rebellionRate,
trend: rebellionRate > 25 ? 'increasing' : rebellionRate > 15 ? 'stable' : 'decreasing'
};
})
.filter(r => r.rebellionRate > 0);
return {
title: 'Voting Patterns Analysis',
description: 'Derived from CIA party effectiveness trends and risk data',
lastUpdated: new Date().toISOString(),
analysisPeriod: '2022-2026',
votingMatrix: { labels, partyNames, agreementMatrix },
keyIssues: [],
rebellionTracking,
_source: 'csv'
};
}
/**
* Load all data in parallel.
* @returns Object with all data
*/
async loadAll(): Promise<CIADataPayload> {
const [overview, election, partyPerf, top10, committees, votingPatterns] =
await Promise.all([
this.loadOverviewDashboard(),
this.loadElectionAnalysis(),
this.loadPartyPerformance(),
this.loadTop10Influential(),
this.loadCommitteeNetwork(),
this.loadVotingPatterns()
]);
return {
overview,
election,
partyPerf,
top10,
committees,
votingPatterns
};
}
}
|