Skip to content

Rounds the session metrics to a reasonable precision#29461

Open
tolot27 wants to merge 2 commits intoevcc-io:masterfrom
tolot27:roundSessionMetrices
Open

Rounds the session metrics to a reasonable precision#29461
tolot27 wants to merge 2 commits intoevcc-io:masterfrom
tolot27:roundSessionMetrices

Conversation

@tolot27
Copy link
Copy Markdown
Contributor

@tolot27 tolot27 commented Apr 27, 2026

Some session metrices are stored as raw float64 with unnecessary precision sometimes caused by multiplication/division. These values can/should be rounded to a reasonable precision (four or five digits after decimal point).:

Find out those values:

SELECT id, meter_start_kwh, meter_end_kwh, charged_kwh, ROUND(charged_kwh, 5) new_kwh, price, ROUND(ROUND(charged_kwh, 5) * price_per_kwh, 5) new_price FROM sessions WHERE charged_kwh * 1000. - FLOOR(charged_kwh * 1000.) > .0000;
id meter_start_kwh meter_end_kwh charged_kwh new_kwh price new_price
65 435.44 449.44 17.0300006866455 17.03 4.79224219322205 4.79224
82 638.08 660.45 27.7600002288818 27.76 7.81166406440735 7.81166
93 304.25 325.57 25.5599994659424 25.56 7.19258384971619 7.19258
94 0.26 25.55 25.1599998474121 25.16 7.08002395706177 7.08002
101 539.68 546.96 17.1599998474121 17.16 5.15657995414734 5.15658
103 838.97 864.31 36.4300003051758 36.43 10.9472150917053 10.94722
106 21.26 24.02 7.6399998664856 7.64 2.29581995987892 2.29582
110 0.26 18.08 17.5499992370605 17.55 5.27377477073669 5.27377
115 915.3 936.59 21.2900009155273 21.29 6.39764527511597 6.39764
142 926.53 931.63 11.6800003051758 11.68 3.50984009170532 3.50984
144 347.86 369.81 22.3999996185303 22.4 6.73119988536834 6.7312
151 1025.38 1041.12 20.5599994659424 20.56 6.17827983951569 6.17828
177 1194.34 1196.28 9.27000045776367 9.27 2.78563513755798 2.78563
182 1459.75 1479.57 23.2399997711182 23.24 6.98361993122101 6.98362
192 156.9 210.79 54.0800018310547 54.08 16.2510405502319 16.25104
199 757.94 773.3 15.7700004577637 15.77 3.76272210922241 3.76272
202 1611.02 1630.97 21.2199993133545 21.22 5.06309183616638 5.06309
206 116.21 119.35 13.6700000762939 13.67 3.26166201820374 3.26166
212 129.88 138.74 15.7600002288818 15.76 3.76033605461121 3.76034
241 949.52 949.63 0.150000005960464 0.15 0.0357900014221668 0.03579
258 1000.6 1058.13 66.4300003051758 66.43 15.8501980728149 15.8502
272 1073.69 1082.36 10.1599998474121 10.16 2.42417596359253 2.42418
284 2047.93 2053.06 5.13000011444092 5.13 1.2240180273056 1.22402
318 1988.66 2000.75 21.3299999237061 21.33 5.08933798179627 5.08934

This PR rounds session.ReferenceCo2PerKWh , SolarPercentage, PricePerKWh, Co2PerKWh and ChargedEnergy.

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 call new(lp.energyMetrics.SolarPercentage()) won’t compile because new expects a type, not a function call; consider computing the value first and then passing a *float64 into roundFloat64Ptr (or adjusting the helper to accept a value instead of a pointer).
  • In ClosePendingSessionsInHistory you inline the rounding logic with math.Round(...*1e5)/1e5; for consistency and easier future changes, it would be better to reuse the roundFloat64 helper there instead of duplicating the rounding formula.
  • The various 4 and 5 digit precisions are currently magic numbers; introducing named constants (e.g., energyPrecision, pricePrecision) would make the chosen precisions clearer and easier to adjust later.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The call `new(lp.energyMetrics.SolarPercentage())` won’t compile because `new` expects a type, not a function call; consider computing the value first and then passing a `*float64` into `roundFloat64Ptr` (or adjusting the helper to accept a value instead of a pointer).
- In `ClosePendingSessionsInHistory` you inline the rounding logic with `math.Round(...*1e5)/1e5`; for consistency and easier future changes, it would be better to reuse the `roundFloat64` helper there instead of duplicating the rounding formula.
- The various `4` and `5` digit precisions are currently magic numbers; introducing named constants (e.g., `energyPrecision`, `pricePrecision`) would make the chosen precisions clearer and easier to adjust later.

## Individual Comments

### Comment 1
<location path="core/session/db.go" line_range="92-95" />
<code_context>

 		if session.MeterStart != nil && session.MeterStop != nil {
-			session.ChargedEnergy = *session.MeterStop - *session.MeterStart
+			session.ChargedEnergy = math.Round((*session.MeterStop-*session.MeterStart)*1e5) / 1e5
 			s.Persist(session)
 		}
</code_context>
<issue_to_address>
**suggestion:** Reuse the shared rounding helper instead of duplicating the math here

Since this matches `roundFloat64` with `digits = 5`, please call `roundFloat64(*session.MeterStop-*session.MeterStart, 5)` instead of inlining the `math.Round` and `1e5` factor. This keeps rounding behavior consistent with other energy fields and avoids future divergence.

```suggestion
		if session.MeterStart != nil && session.MeterStop != nil {
			session.ChargedEnergy = roundFloat64(*session.MeterStop-*session.MeterStart, 5)
			s.Persist(session)
		}
```
</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 core/session/db.go
Comment on lines 92 to 95
if session.MeterStart != nil && session.MeterStop != nil {
session.ChargedEnergy = *session.MeterStop - *session.MeterStart
session.ChargedEnergy = math.Round((*session.MeterStop-*session.MeterStart)*1e5) / 1e5
s.Persist(session)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion: Reuse the shared rounding helper instead of duplicating the math here

Since this matches roundFloat64 with digits = 5, please call roundFloat64(*session.MeterStop-*session.MeterStart, 5) instead of inlining the math.Round and 1e5 factor. This keeps rounding behavior consistent with other energy fields and avoids future divergence.

Suggested change
if session.MeterStart != nil && session.MeterStop != nil {
session.ChargedEnergy = *session.MeterStop - *session.MeterStart
session.ChargedEnergy = math.Round((*session.MeterStop-*session.MeterStart)*1e5) / 1e5
s.Persist(session)
}
if session.MeterStart != nil && session.MeterStop != nil {
session.ChargedEnergy = roundFloat64(*session.MeterStop-*session.MeterStart, 5)
s.Persist(session)
}

@andig
Copy link
Copy Markdown
Member

andig commented Apr 28, 2026

These values can/should be rounded

Why would you do this, i.e. which actual problem would that solve?

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.

2 participants