close
Skip to content

Session: add reference price and CO2#28712

Merged
andig merged 2 commits into
masterfrom
feat/session_reference_price_co2
Apr 6, 2026
Merged

Session: add reference price and CO2#28712
andig merged 2 commits into
masterfrom
feat/session_reference_price_co2

Conversation

@naltatis
Copy link
Copy Markdown
Member

@naltatis naltatis commented Apr 1, 2026

addresses #11160

Snapshot average grid price and CO2 intensity (next 24h) when a charging session is created.
Adds referencePricePerKWh and referenceCo2PerKWh to the session DB schema and JSON/CSV response. This provides stable reference values for future savings calculations instead of relying on volatile forecast data.

Existing sessions get NULL via GORM auto-migration.

Next steps: (separate PRs)

  • Add a UI option to (bulk-)backfill reference data for old charging sessions.
  • Replace the static savings calculation (hard coded region values) and use the new data fields.

@naltatis naltatis requested a review from andig April 1, 2026 10:11
@naltatis naltatis added the enhancement New feature or request label Apr 1, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The AverageRate helper currently computes a simple arithmetic mean of all overlapping rates, which biases the result if the underlying rate periods have different durations; consider making this a duration-weighted average over the [now, now+d) interval instead.
  • AverageRate hardcodes time.Now() inside the function, which makes behavior time-dependent and harder to test; passing the start time as a parameter (or injecting a clock) would make the function more predictable and reusable.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `AverageRate` helper currently computes a simple arithmetic mean of all overlapping rates, which biases the result if the underlying rate periods have different durations; consider making this a duration-weighted average over the [now, now+d) interval instead.
- `AverageRate` hardcodes `time.Now()` inside the function, which makes behavior time-dependent and harder to test; passing the start time as a parameter (or injecting a clock) would make the function more predictable and reusable.

## Individual Comments

### Comment 1
<location path="tariff/tariffs.go" line_range="47-56" />
<code_context>
 	return rr
 }

+// AverageRate returns the arithmetic mean of rates in [now, now+d), or nil if unavailable.
+func AverageRate(t api.Tariff, d time.Duration) *float64 {
+	rr := Rates(t)
+	if rr == nil {
+		return nil
+	}
+
+	now := time.Now()
+	end := now.Add(d)
+
+	var sum float64
+	var count int
+	for _, r := range rr {
+		if r.Start.Before(end) && r.End.After(now) {
+			sum += r.Value
+			count++
+		}
+	}
+
+	if count == 0 {
+		return nil
+	}
+
+	avg := sum / float64(count)
+	return &avg
+}
</code_context>
<issue_to_address>
**suggestion:** Consider weighting the average by time instead of counting overlapping rate entries equally.

Right now each overlapping rate contributes equally to the mean, regardless of how long it applies within `[now, now+d)`. If rate periods can vary a lot in length, this can significantly skew the result (e.g. brief spikes vs long stable periods). You may want to compute a time‑weighted average instead: for each overlapping rate, accumulate `r.Value * overlapDuration` and then divide the total by the sum of all overlap durations.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread tariff/tariffs.go
Comment thread tariff/tariffs.go Outdated
@andig andig enabled auto-merge (squash) April 6, 2026 14:43
@andig andig merged commit 188a5d8 into master Apr 6, 2026
7 checks passed
@andig andig deleted the feat/session_reference_price_co2 branch April 6, 2026 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants