Repository navigation
Expand file tree
/
Copy pathapi_savings.go
More file actions
582 lines (552 loc) · 18.3 KB
/
Copy pathapi_savings.go
File metadata and controls
582 lines (552 loc) · 18.3 KB
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
package api
import (
"context"
"log/slog"
"net/http"
"strconv"
"sync"
"time"
"github.com/srcfl/ftw/go/internal/config"
"github.com/srcfl/ftw/go/internal/localzone"
"github.com/srcfl/ftw/go/internal/savings"
"github.com/srcfl/ftw/go/internal/state"
)
const (
savingsValueScope = "site_total"
savingsBaseline = "no_pv_no_battery_vehicle_energy_at_daily_average"
// A built-in self-use mode on the same cells runs a bit below the
// planner's nameplate efficiency and keeps a backup it will not spend.
dumbEffHaircut = 0.05
dumbEffFloor = 0.80
dumbReserveFrac = 0.10
batterySOCMetric = "battery_soc"
)
// daySavings is the cached per-local-day cost breakdown that powers
// /api/savings/daily. Mirrors the immutable-day pattern dailyCache uses.
// Past days never re-render; only today is recomputed each request.
type daySavings struct {
ImportWh float64
ExportWh float64
LoadWh float64
EVWh float64
ImportCostOre float64
ExportRevenueOre float64
BaselineHouseOre float64
BaselineEvOre float64
BaselineCostOre float64
AvgImportOreKwh float64
AvgExportOreKwh float64
PriceSlotCount int
ActualCostOre float64
FlatCostOre float64
SavedOre float64
ExpectedMs int64
HistoryCoveredMs int64
PricedCoveredMs int64
HistoryCoverage float64
PricedCoverage float64
Resolution string // "slot" or "no_prices"
}
func fromBreakdown(b state.DayCostBreakdown, resolution string) daySavings {
return daySavings{
ImportWh: b.ImportWh,
ExportWh: b.ExportWh,
LoadWh: b.LoadWh,
EVWh: b.EVWh,
ImportCostOre: b.ImportCostOre,
ExportRevenueOre: b.ExportRevenueOre,
BaselineHouseOre: b.BaselineHouseOre,
BaselineEvOre: b.BaselineEvOre,
BaselineCostOre: b.BaselineCostOre,
AvgImportOreKwh: b.AvgImportOreKwh,
AvgExportOreKwh: b.AvgExportOreKwh,
PriceSlotCount: b.PriceSlotCount,
ActualCostOre: b.ActualCostOre(),
FlatCostOre: b.FlatCostOre(),
SavedOre: b.SavedOre(),
ExpectedMs: b.ExpectedMs,
HistoryCoveredMs: b.HistoryCoveredMs,
PricedCoveredMs: b.PricedCoveredMs,
HistoryCoverage: b.HistoryCoveragePct(),
PricedCoverage: b.PricedCoveragePct(),
Resolution: resolution,
}
}
// savingsCache is created lazily on first request. Process-lifetime.
// Keyed on YYYY-MM-DD; immutable days are cached forever. Cleared on
// process restart, which is the only practical way config-driven
// export-pricing changes invalidate it — operators changing
// cfg.Price.ExportBonusOreKwh mid-run will see stale historical answers
// until a restart. Acceptable for an MVP — those fields rarely change.
type savingsCacheT struct {
mu sync.Mutex
m map[string]daySavings
}
// handleSavingsDaily returns per-local-day actual net cost vs the load-only
// no-PV/no-battery baseline, with vehicle energy priced at the day's average.
// This is combined site value, not incremental optimizer value. The endpoint
// name and existing fields are kept for compatibility.
//
// GET /api/savings/daily?days=N
//
// Response:
//
// {
// "days": [
// {
// "day": "YYYY-MM-DD",
// "import_wh": ..., "export_wh": ..., "load_wh": ...,
// "import_cost_ore": ..., "export_revenue_ore": ...,
// "actual_cost_ore": ..., "baseline_cost_ore": ..., "saved_ore": ...,
// "avg_import_ore_kwh": ..., "avg_export_ore_kwh": ...,
// "expected_ms": ..., "history_covered_ms": ..., "priced_covered_ms": ...,
// "history_coverage_pct": ..., "priced_coverage_pct": ...,
// "resolution": "slot" | "no_prices"
// },
// ...
// ],
// "totals": { "import_wh": ..., "export_wh": ..., "load_wh": ...,
// "actual_cost_ore": ..., "baseline_cost_ore": ..., "saved_ore": ...,
// "expected_ms": ..., "history_covered_ms": ..., "priced_covered_ms": ...,
// "history_coverage_pct": ..., "priced_coverage_pct": ... },
// "tz": "Europe/Stockholm", "value_scope": "site_total",
// "baseline": "no_pv_no_battery_vehicle_energy_at_daily_average"
// }
//
// Days where the prices table has no slot for the zone come back with
// resolution="no_prices" and zeroed costs. Volume columns are still
// populated for those days so the UI can distinguish "no data" from
// "data but no prices yet".
func (s *Server) handleSavingsDaily(w http.ResponseWriter, r *http.Request) {
if s.deps.State == nil {
writeJSON(w, 200, map[string]any{
"days": []any{}, "value_scope": savingsValueScope, "baseline": savingsBaseline,
})
return
}
days := 7
if v := r.URL.Query().Get("days"); v != "" {
if n, err := strconv.Atoi(v); err == nil && n > 0 {
days = n
}
}
if days > 90 {
days = 90
}
// Pull export pricing + zone from current config. Take the config
// mutex briefly to copy the small set of scalars we need so handler
// work doesn't block hot-path readers.
zone := ""
ep := state.ExportPricing{}
if s.deps.CfgMu != nil && s.deps.Cfg != nil {
s.deps.CfgMu.RLock()
if s.deps.Cfg.Price != nil {
zone = s.deps.Cfg.Price.Zone
ep.BonusOreKwh = s.deps.Cfg.Price.ExportBonusOreKwh
ep.FeeOreKwh = s.deps.Cfg.Price.ExportFeeOreKwh
ep.FloorOreKwh = s.deps.Cfg.Price.ExportFloorOreKwh
}
if s.deps.Cfg.Planner != nil {
ep.FlatOreKwh = s.deps.Cfg.Planner.ExportOrePerKWh
}
s.deps.CfgMu.RUnlock()
}
if zone == "" {
// No price provider configured → nothing to compare against.
writeJSON(w, 200, map[string]any{
"days": []any{}, "tz": localzone.Name(),
"value_scope": savingsValueScope, "baseline": savingsBaseline,
})
return
}
now := time.Now()
loc := now.Location()
todayMidnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
if wrote, err := s.writeLedgerSavings(w, r, days, now, loc, todayMidnight, zone, ep); wrote || err != nil {
if err != nil {
slog.Error("handleSavingsDaily: ledger savings failed", "err", err)
http.Error(w, "savings load failed", http.StatusInternalServerError)
}
return
}
s.ensureSavingsCache()
sourceThrough, sourceMode, err := s.deps.State.HistorySourceProgress(r.Context())
if err != nil {
http.Error(w, "savings load failed", http.StatusInternalServerError)
return
}
out := make([]map[string]any, 0, days)
var tImpWh, tExpWh, tLoadWh, tActual, tBaseline, tSaved float64
var tExpectedMs, tHistoryCoveredMs, tPricedCoveredMs int64
for i := days - 1; i >= 0; i-- {
dayStart := todayMidnight.AddDate(0, 0, -i)
dayKey := dayStart.Format("2006-01-02")
cacheKey := dayKey
if sourceMode {
cacheKey += ":source_interval_balance_v1"
}
isToday := i == 0
dayEnd := dayStart.AddDate(0, 0, 1)
var ds daySavings
if isToday || (sourceMode && sourceThrough < dayEnd.UnixMilli()) {
b, err := s.deps.State.DailyCostBreakdownContext(r.Context(), dayStart.UnixMilli(), min(now.UnixMilli(), dayEnd.UnixMilli()), zone, ep)
if err != nil {
slog.Error("handleSavingsDaily: DailyCostBreakdown failed", "err", err, "day", dayKey)
http.Error(w, "savings load failed", http.StatusInternalServerError)
return
}
ds = fromBreakdown(b, resolutionFor(b))
} else {
s.savingsCacheMu.Lock()
cached, ok := s.savingsCache[cacheKey]
s.savingsCacheMu.Unlock()
if ok {
ds = cached
} else {
dayEnd := dayStart.AddDate(0, 0, 1)
b, err := s.deps.State.DailyCostBreakdownContext(r.Context(), dayStart.UnixMilli(), dayEnd.UnixMilli(), zone, ep)
if err != nil {
slog.Error("handleSavingsDaily: DailyCostBreakdown failed", "err", err, "day", dayKey)
http.Error(w, "savings load failed", http.StatusInternalServerError)
return
}
ds = fromBreakdown(b, resolutionFor(b))
s.savingsCacheMu.Lock()
s.savingsCache[cacheKey] = ds
s.savingsCacheMu.Unlock()
}
}
tImpWh += ds.ImportWh
tExpWh += ds.ExportWh
tLoadWh += ds.LoadWh
tActual += ds.ActualCostOre
tBaseline += ds.BaselineCostOre
tSaved += ds.SavedOre
tExpectedMs += ds.ExpectedMs
tHistoryCoveredMs += ds.HistoryCoveredMs
tPricedCoveredMs += ds.PricedCoveredMs
out = append(out, map[string]any{
"day": dayKey,
"import_wh": ds.ImportWh,
"export_wh": ds.ExportWh,
"load_wh": ds.LoadWh,
"ev_wh": ds.EVWh,
"import_cost_ore": ds.ImportCostOre,
"export_revenue_ore": ds.ExportRevenueOre,
"actual_cost_ore": ds.ActualCostOre,
"baseline_house_ore": ds.BaselineHouseOre,
"baseline_ev_ore": ds.BaselineEvOre,
"baseline_cost_ore": ds.BaselineCostOre,
// Deprecated compatibility alias: now equals baseline_cost_ore
// (house slot-priced + EV at daily-avg), not a flat-average tariff.
"flat_cost_ore": ds.FlatCostOre,
"saved_ore": ds.SavedOre,
"avg_import_ore_kwh": ds.AvgImportOreKwh,
"avg_export_ore_kwh": ds.AvgExportOreKwh,
"expected_ms": ds.ExpectedMs,
"history_covered_ms": ds.HistoryCoveredMs,
"priced_covered_ms": ds.PricedCoveredMs,
"history_coverage_pct": ds.HistoryCoverage,
"priced_coverage_pct": ds.PricedCoverage,
"resolution": ds.Resolution,
})
}
writeJSON(w, 200, map[string]any{
"days": out,
"totals": map[string]any{
"import_wh": tImpWh,
"export_wh": tExpWh,
"load_wh": tLoadWh,
"actual_cost_ore": tActual,
"baseline_cost_ore": tBaseline,
// Deprecated compatibility alias for older UI callers.
"flat_cost_ore": tBaseline,
"saved_ore": tSaved,
"expected_ms": tExpectedMs,
"history_covered_ms": tHistoryCoveredMs,
"priced_covered_ms": tPricedCoveredMs,
"history_coverage_pct": boundedCoveragePct(tHistoryCoveredMs, tExpectedMs),
"priced_coverage_pct": boundedCoveragePct(tPricedCoveredMs, tExpectedMs),
},
"tz": localzone.Name(),
"value_scope": savingsValueScope,
"baseline": savingsBaseline,
})
}
func boundedCoveragePct(coveredMs, expectedMs int64) float64 {
if coveredMs <= 0 || expectedMs <= 0 {
return 0
}
if coveredMs >= expectedMs {
return 1
}
return float64(coveredMs) / float64(expectedMs)
}
// resolutionFor reports whether the breakdown saw any price data. A day
// with zero-price slots is still priced; only zero overlapping price slots
// means the UI should render "awaiting prices".
func resolutionFor(b state.DayCostBreakdown) string {
if b.PriceSlotCount == 0 {
return "no_prices"
}
return "slot"
}
// writeLedgerSavings answers from the energy ledger when it has rows in the
// window. An empty ledger leaves the caller on the older point-log path.
func (s *Server) writeLedgerSavings(w http.ResponseWriter, r *http.Request, days int, now time.Time, loc *time.Location, todayMidnight time.Time, zone string, ep state.ExportPricing) (bool, error) {
windowStart := todayMidnight.AddDate(0, 0, -(days - 1))
buckets, err := s.deps.State.LedgerFlowBuckets(r.Context(), windowStart.UnixMilli(), now.UnixMilli())
if err != nil || len(buckets) == 0 {
return false, err
}
rawSlots, err := s.deps.State.CostSlots(r.Context(), zone, windowStart.UnixMilli(), now.UnixMilli())
if err != nil {
return false, err
}
slots := make([]savings.PriceSlot, len(rawSlots))
for i, sl := range rawSlots {
slots[i] = savings.PriceSlot{StartMs: sl.StartMs, EndMs: sl.EndMs, ImportOreKwh: sl.ImportOreKwh, SpotOreKwh: sl.SpotOreKwh}
}
flows := make([]savings.FlowBucket, len(buckets))
for i, b := range buckets {
flows[i] = savings.FlowBucket{
StartMs: b.StartMs, LenMs: b.LenMs,
ImportWh: b.ImportWh, ExportWh: b.ExportWh, PVWh: b.PVWh, LoadWh: b.LoadWh,
EVChargeWh: b.EVChargeWh, EVDischargeWh: b.EVDischargeWh,
}
}
windows := make([]savings.DayWindow, days)
keys := make([]string, days)
for i := 0; i < days; i++ {
start := todayMidnight.AddDate(0, 0, -(days - 1 - i))
end := start.AddDate(0, 0, 1)
if !end.Before(now) && !start.After(now) {
end = now
}
windows[i] = savings.DayWindow{StartMs: start.UnixMilli(), EndMs: end.UnixMilli()}
keys[i] = start.Format("2006-01-02")
}
bat, socDrivers := ledgerBattery(s.deps.Cfg)
socPts := s.batterySOCSamples(r.Context(), socDrivers, windowStart.UnixMilli(), now.UnixMilli())
if bat.CapacityWh > 0 {
if wh, ok := socWhAt(socPts, windowStart.UnixMilli(), bat.CapacityWh); ok && wh > bat.StartWh {
bat.StartWh = wh
}
}
priced := savings.EvaluateLedger(flows, slots, windows, bat, ep)
out := make([]map[string]any, 0, days)
var tImp, tExp, tLoad, tActual, tBase, tSaved, tSelf, tSelfSaved float64
var tExpected, tHist, tPrice int64
for i, d := range priced {
expected := windows[i].EndMs - windows[i].StartMs
resolution := "no_prices"
slotCount := 0
var importSum, exportSum, importMs, exportMs float64
for _, sl := range slots {
if sl.EndMs <= windows[i].StartMs || sl.StartMs >= windows[i].EndMs {
continue
}
slotCount++
overlap := min64(sl.EndMs, windows[i].EndMs) - max64(sl.StartMs, windows[i].StartMs)
importSum += sl.ImportOreKwh * float64(overlap)
exportSum += sl.SpotOreKwh * float64(overlap)
importMs += float64(overlap)
exportMs += float64(overlap)
}
if slotCount > 0 {
resolution = "slot"
}
selfSaved := d.SelfSavedOre()
row := map[string]any{
"day": keys[i], "import_wh": d.ImportWh, "export_wh": d.ExportWh,
"load_wh": d.LoadWh, "ev_wh": d.EVWh, "pv_wh": d.PVWh,
"import_cost_ore": d.ImportCostOre, "export_revenue_ore": d.ExportRevenueOre,
"actual_cost_ore": d.ImportCostOre - d.ExportRevenueOre,
"baseline_house_ore": d.NoPVCostOre, "baseline_ev_ore": 0.0,
"baseline_cost_ore": d.NoPVCostOre, "flat_cost_ore": d.NoPVCostOre,
"saved_ore": d.NoPVSavedOre(),
"self_consumption_cost_ore": d.SelfCostOre,
"self_consumption_saved_ore": selfSaved,
"self_consumption_import_wh": d.SelfImportWh,
"self_consumption_export_wh": d.SelfExportWh,
"avg_import_ore_kwh": divOrZero(importSum, importMs),
"avg_export_ore_kwh": divOrZero(exportSum, exportMs),
"expected_ms": expected,
"history_covered_ms": d.CoveredMs,
"priced_covered_ms": d.PricedMs,
"history_coverage_pct": boundedCoveragePct(d.CoveredMs, expected),
"priced_coverage_pct": boundedCoveragePct(d.PricedMs, expected),
"resolution": resolution,
}
out = append(out, row)
tImp += d.ImportWh
tExp += d.ExportWh
tLoad += d.LoadWh
tActual += d.ImportCostOre - d.ExportRevenueOre
tBase += d.NoPVCostOre
tSaved += d.NoPVSavedOre()
tSelf += d.SelfCostOre
tSelfSaved += selfSaved
tExpected += expected
tHist += d.CoveredMs
tPrice += d.PricedMs
}
writeJSON(w, 200, map[string]any{
"days": out,
"totals": map[string]any{
"import_wh": tImp, "export_wh": tExp, "load_wh": tLoad,
"actual_cost_ore": tActual, "baseline_cost_ore": tBase, "flat_cost_ore": tBase,
"saved_ore": tSaved,
"self_consumption_cost_ore": tSelf, "self_consumption_saved_ore": tSelfSaved,
"expected_ms": tExpected, "history_covered_ms": tHist, "priced_covered_ms": tPrice,
"history_coverage_pct": boundedCoveragePct(tHist, tExpected),
"priced_coverage_pct": boundedCoveragePct(tPrice, tExpected),
},
"tz": localzone.Name(), "value_scope": savingsValueScope, "baseline": savingsBaseline,
"baselines": []string{"no_pv_no_battery", "self_consumption"},
})
return true, nil
}
func ledgerBattery(cfg *config.Config) (savings.Battery, []string) {
if cfg == nil {
return savings.Battery{}, nil
}
var capacity, chargeW, dischargeW float64
var names []string
for _, d := range cfg.Drivers {
if d.BatteryCapacityWh <= 0 || d.BatteryTelemetryOnly {
continue
}
capacity += d.BatteryCapacityWh
c, dis := d.MaxChargeW, d.MaxDischargeW
if c <= 0 {
c = 5000
}
if dis <= 0 {
dis = 5000
}
chargeW += c
dischargeW += dis
if d.Name != "" {
names = append(names, d.Name)
}
}
chg, disEff := dumbEfficiency(0), dumbEfficiency(0)
if cfg.Planner != nil {
if cfg.Planner.ChargeEfficiency > 0 {
chg = dumbEfficiency(cfg.Planner.ChargeEfficiency)
}
if cfg.Planner.DischargeEfficiency > 0 {
disEff = dumbEfficiency(cfg.Planner.DischargeEfficiency)
}
}
reserve := capacity * dumbReserveFrac
return savings.Battery{
CapacityWh: capacity, MaxChargeW: chargeW, MaxDischargeW: dischargeW,
StartWh: reserve, ReserveWh: reserve,
ChargeEfficiency: chg, DischargeEfficiency: disEff,
}, names
}
func dumbEfficiency(configured float64) float64 {
if configured <= 0 || configured > 1 {
configured = 0.95
}
v := configured - dumbEffHaircut
if v < dumbEffFloor {
return dumbEffFloor
}
return v
}
func (s *Server) batterySOCSamples(ctx context.Context, drivers []string, since, until int64) []state.SeriesPoint {
if s == nil || s.deps.State == nil || len(drivers) == 0 || until <= since {
return nil
}
var best []state.SeriesPoint
for _, name := range drivers {
// Coarse on purpose. A fine cap returns only the latest day, and the
// stored-energy correction would then land on that day alone.
span := until - since
points := int(span/(2*time.Hour.Milliseconds())) + 2
if points < 8 {
points = 8
}
if points > 1500 {
points = 1500
}
pts, err := s.deps.State.LoadSeriesBucketsContext(ctx, name, batterySOCMetric, since, until, points)
if err != nil {
slog.Warn("savings battery soc", "driver", name, "err", err)
continue
}
if len(pts) > len(best) {
best = pts
}
}
return best
}
func socWhAt(points []state.SeriesPoint, at int64, capacityWh float64) (float64, bool) {
frac, ok := socFractionAt(points, at)
if !ok || capacityWh <= 0 {
return 0, false
}
return frac * capacityWh, true
}
func socFractionAt(points []state.SeriesPoint, at int64) (float64, bool) {
if len(points) == 0 {
return 0, false
}
best := 0
bestAbs := int64(1 << 62)
for i, p := range points {
d := p.TsMs - at
if d < 0 {
d = -d
}
if d < bestAbs {
bestAbs = d
best = i
}
}
if bestAbs > 6*time.Hour.Milliseconds() {
return 0, false
}
v := points[best].V
if points[best].Last != nil {
v = *points[best].Last
}
if v > 1.5 {
v /= 100
}
if v < 0 {
v = 0
}
if v > 1 {
v = 1
}
return v, true
}
func divOrZero(sum, weight float64) float64 {
if weight <= 0 {
return 0
}
return sum / weight
}
func min64(a, b int64) int64 {
if a < b {
return a
}
return b
}
func max64(a, b int64) int64 {
if a > b {
return a
}
return b
}
func (s *Server) ensureSavingsCache() {
s.savingsCacheMu.Lock()
defer s.savingsCacheMu.Unlock()
if s.savingsCache == nil {
s.savingsCache = make(map[string]daySavings)
}
}