Rounds the session metrics to a reasonable precision#29461
Open
tolot27 wants to merge 2 commits intoevcc-io:masterfrom
Open
Rounds the session metrics to a reasonable precision#29461tolot27 wants to merge 2 commits intoevcc-io:masterfrom
tolot27 wants to merge 2 commits intoevcc-io:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The call
new(lp.energyMetrics.SolarPercentage())won’t compile becausenewexpects a type, not a function call; consider computing the value first and then passing a*float64intoroundFloat64Ptr(or adjusting the helper to accept a value instead of a pointer). - In
ClosePendingSessionsInHistoryyou inline the rounding logic withmath.Round(...*1e5)/1e5; for consistency and easier future changes, it would be better to reuse theroundFloat64helper there instead of duplicating the rounding formula. - The various
4and5digit 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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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) | ||
| } |
Contributor
There was a problem hiding this comment.
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) | |
| } |
Member
Why would you do this, i.e. which actual problem would that solve? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
This PR rounds
session.ReferenceCo2PerKWh,SolarPercentage,PricePerKWh,Co2PerKWhandChargedEnergy.