Skip to content

fix(finance): correct energy-balance period aggregation#72

Merged
tekwani merged 1 commit intotetherto:developfrom
mukama:feat/site-energy-calculations
May 1, 2026
Merged

fix(finance): correct energy-balance period aggregation#72
tekwani merged 1 commit intotetherto:developfrom
mukama:feat/site-energy-calculations

Conversation

@mukama
Copy link
Copy Markdown
Contributor

@mukama mukama commented May 1, 2026

The shared aggregateByPeriod summed every numeric field, which is wrong for rates and per-MW denominators on weekly/monthly buckets. Adds an opt-in meanKeys option (existing callers untouched), uses it from getEnergyBalance, and exposes a few new fields.

FE work to consume this (useEnergyBalance.hook.ts)

  • Map FE periodType → BE period (daily|weekly|monthly); the schema now accepts weekly.
  • Read entry.sitePowerMW and entry.energyRevenueBTC_MW/_USD_MW directly instead of deriving from powerW / energyRevenuePerMWh.
  • Drive cost-tab metric cards from the summary: avgPowerConsumption, avgEnergyCostPerMWh, avgOperationalCostPerMWh, avgRevenuePerMWh.
  • Format chart x-axis labels via the FE's getPeriodKey(entry.ts, periodType) — BE does not return a period string.

The shared aggregateByPeriod summed every numeric field, which is
correct for totals but produced nonsense for rates/means (curtailment
rate, btcPrice, sitePowerMW, etc.) on weekly/monthly buckets.

- Add an optional `meanKeys` arg to aggregateByPeriod that averages
  listed fields instead of summing. Default behavior is unchanged for
  the 6 existing callers.
- getEnergyBalance now passes meanKeys for sitePowerMW, btcPrice and
  the rate fields, then post-processes to recompute energyRevenue*_MW
  against the (correctly meaned) sitePowerMW denominator.
- Add sitePowerMW + energyRevenueBTC_MW/energyRevenueUSD_MW to log
  entries; add avgPowerConsumption + avgEnergyCostPerMWh +
  avgOperationalCostPerMWh to summary.
- Schema: add 'weekly' to the period enum.
@tekwani tekwani merged commit add4db7 into tetherto:develop May 1, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants