import type { BoxMaxes, BoxTops, SummaryData, YearFlows, YearLabels, YearSums, YearTotals } from '../../index'; import { DataService } from '../data/DataService'; import { ConfigurationService } from "../ConfigurationService"; /** * Summary Service Implementation * * Processes raw energy data into * totals, flows, labels, and statistical summaries needed for visualization. * * Include comprehensive multi-layer caching. * Handles complex mathematical operations for energy flow summary data including * totals calculation, maximum value detection, and statistical analysis. * * Mathematical Operations: * - Fuel totals calculation across all consumption sectors * - Sector totals calculation across all fuel types * - Maximum value detection for scaling calculations * - Box positioning calculations for visual layout * - Flow data preparation for animation sequences */ export declare class SummaryService { private dataService; private configService; summary: SummaryData | null; totals: YearTotals[]; flows: YearFlows[]; labels: YearLabels[]; yearSums: YearSums; maxes: BoxMaxes; boxTops: BoxTops | null; constructor(dataService: DataService, configService: ConfigurationService); /** * Extract expensive calculation to separate method (same logic as original) */ private buildSummary; /** * Calculate Energy Flow Totals - Triple Nested Loop Algorithm * * MATHEMATICAL COMPLEXITY: O(n³) where n = years × fuels × sectors * This is the most computationally expensive method in the entire application, * processing every combination of Year × Fuel × Consumption Sector. * * ALGORITHM STRUCTURE: * * Level 1 (i): Years Loop - Process each chronological data point * └─ Iterates through energy data points from 1800-2021+ * └─ Creates YearTotals, YearFlows, YearLabels structures for each year * * Level 2 (j): Fuels Loop - Process each energy source type * └─ solar, nuclear, hydro, wind, geo, gas, coal, bio, petro * └─ Skips electricity (j=0) as it's processed separately * └─ Calculates fuel totals and label positioning * * Level 3 (k): Sectors Loop - Process each consumption category * └─ elec (electricity), res (residential), ag (agriculture), * └─ indus (industrial), trans (transportation) * └─ Performs cross-tabulation: fuel → sector energy flows * * MATHEMATICAL OPERATIONS PER ITERATION: * 1. Flow counting: Increment flow counters for non-zero values * 2. Sector totals: Accumulate energy by consumption sector * 3. Fuel totals: Accumulate energy by fuel source type * 4. Electricity integration: Add electricity to non-elec sectors * 5. Waste heat calculation: Always include waste heat values * 6. Label positioning: Calculate visual label Y-coordinates * 7. Height accumulation: Track fuel stack heights for layout * * PERFORMANCE OPTIMIZATIONS (LAYER 3 CACHING): * - Configuration constants cached locally (eliminates property access) * - Direct array access patterns optimized for V8 engine * - Type assertions used sparingly to maintain performance * - Mathematical operations use compound assignment for speed * * ENERGY INDUSTRY DOMAIN LOGIC: * - Electricity is treated as both fuel source AND consumption vector * - Waste heat represents thermodynamic losses in electricity generation * - Cross-tabulation enables Sankey flow visualization of energy paths * - Sector totals enable proportional box sizing in visual representation * * EXAMPLE CALCULATION FLOW: * Year 2021, Coal, Industrial Sector: * 1. coal.indus = 15.6 (Quads) - Raw data value * 2. total.indus += 15.6 - Add to industrial sector total * 3. total.coal += 15.6 - Add to coal fuel total * 4. flow.indus++ - Increment industrial flow count * 5. Electricity waste heat added if applicable * * VISUALIZATION MATHEMATICS: * - SCALE (0.02): Converts energy units (Quads) to pixel heights * - LEFT_GAP: Visual spacing between fuel source boxes * - ELEC_BOX positioning: Special coordinate system for electricity flows * - Label positioning: Y-coordinates calculated for fuel source labels */ buildTotals(): void; /** * Calculate Maximum Energy Values - Statistical Analysis with Caching * * MATHEMATICAL PURPOSE: * Determines the maximum energy consumption value for each sector across all years. * Critical for proportional visualization - largest values determine visual scale. * * ALGORITHM: Statistical Maximum Detection * For each consumption sector (res, ag, indus, trans, elec): * 1. Extract all year values for that sector: [1970: 15.2, 1980: 18.4, ...] * 2. Apply Math.max() to find peak consumption year * 3. Cache result to avoid repeated Math.max() calls (expensive operation) * * VISUALIZATION APPLICATION: * Max values determine box heights in Sankey diagram: * - Residential sector max → residential box height scale * - Industrial sector max → industrial box height scale * - Transportation sector max → transportation box height scale * */ private buildMaxes; /** * Calculate Consumption Sector Box Positions - Layout Algorithm with Caching * * MATHEMATICAL PURPOSE: * Calculates Y-coordinate positions for consumption sector boxes in the right-hand column * of the Sankey diagram. Each box position depends on the cumulative heights of boxes above it. * * LAYOUT ALGORITHM: Sequential Stacking with Proportional Heights * 1. Start with residential (res) box at base position: ELEC_BOX[1] + 50 * 2. Each subsequent box stacks below with: previous_top + previous_max_height + gap * 3. Box heights are proportional to maximum energy consumption (maxes values) * 4. Visual gaps (RIGHT_GAP) separate boxes for clarity * * MATHEMATICAL FORMULA for Box Positioning: * box_top[i] = box_top[i-1] + maxes[i-1] × SCALE + RIGHT_GAP * * Where: * - maxes[sector]: Peak energy consumption for that sector across all years * - SCALE (0.02): Energy-to-pixel conversion factor * - RIGHT_GAP: Visual spacing between consumption boxes * * VISUAL LAYOUT SEQUENCE: * 1. Residential (res): ELEC_BOX[1] + 50 * 2. Agriculture (ag): res_top + res_max_height + gap * 3. Industrial (indus): ag_top + ag_max_height + gap * 4. Transportation (trans): indus_top + indus_max_height + gap * * EXAMPLE CALCULATION (SCALE = 0.02, RIGHT_GAP = 15): * res_top = 350, res_max = 30.5 Quads * → ag_top = 350 + (30.5 × 0.02) + 15 = 365.61 pixels */ private buildBoxTops; } //# sourceMappingURL=SummaryService.d.ts.map