All files / src/browser/cia/loaders top10.ts

100% Statements 12/12
50% Branches 12/24
100% Functions 6/6
100% Lines 12/12

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                                                    2x           2x 2x 3x       2x 4x 1x     2x 3x 3x                   5x                     2x                  
/**
 * @module CIA/Loaders/Top10
 * @category Intelligence Platform - Data Acquisition & Pipeline Management
 *
 * @description
 * Builds the top-10 most influential MPs ranking from CIA CSV exports.
 * Joins influence metrics with the MP risk summary and ranks by network
 * connections.
 *
 * @author Hack23 AB - Data Pipeline Engineering
 * @license Apache-2.0
 * @since 2026
 */
 
import type { CSVRow, MPRanking, Top10Influential } from '../types.js';
import type { LoadCSV } from '../csv-utils.js';
import { CSV_SOURCES } from '../sources.js';
 
/**
 * Build `Top10Influential` ranking from CSV sources.
 * Replaces the legacy `top10-influential-mps.json` static export.
 *
 * @param loadCSV - CSV loader closure
 * @returns Top 10 MPs ranked by network connections, joined with risk data
 */
export async function loadTop10Influential(loadCSV: LoadCSV): Promise<Top10Influential> {
  const [influence, riskSummary] = await Promise.all([
    loadCSV(CSV_SOURCES.influenceMetrics.local),
    loadCSV(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'
  };
}