-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix compiling missing statistics losing rows #101616
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
aa1711b
Always fetch orm rows for latest short term stats
bdraco fb7bf69
cover
bdraco 388a9f9
new test
bdraco f560d98
missing stats
bdraco 0624083
got problem to happen
bdraco cc63143
fixes
bdraco def891a
fixes
bdraco b6a640d
fixes
bdraco 9acc20c
merge
bdraco 8d419c8
Merge remote-tracking branch 'upstream/dev' into orm_rows_short_term_…
bdraco ab8be71
merge
bdraco 250d83d
energy tests
bdraco b6f1f87
avoid timeout on postgresql
bdraco fd3bc75
remove get_latest_short_term_statistics as its unused now
bdraco dba417b
make test work on postgresql/mysql
bdraco 444eba6
patch out issue since we are manually stop/start and we still test on…
bdraco 34d0416
Merge branch 'dev' into orm_rows_short_term_stats
bdraco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
get_instance, | ||
history, | ||
statistics, | ||
util as recorder_util, | ||
) | ||
from homeassistant.components.recorder.models import ( | ||
StatisticData, | ||
|
@@ -374,27 +373,7 @@ def _timestamp_to_isoformat_or_none(timestamp: float | None) -> str | None: | |
return dt_util.utc_from_timestamp(timestamp).isoformat() | ||
|
||
|
||
def compile_statistics( | ||
hass: HomeAssistant, start: datetime.datetime, end: datetime.datetime | ||
) -> statistics.PlatformCompiledStatistics: | ||
"""Compile statistics for all entities during start-end. | ||
|
||
Note: This will query the database and must not be run in the event loop | ||
""" | ||
# There is already an active session when this code is called since | ||
# it is called from the recorder statistics. We need to make sure | ||
# this session never gets committed since it would be out of sync | ||
# with the recorder statistics session so we mark it as read only. | ||
# | ||
# If we ever need to write to the database from this function we | ||
# will need to refactor the recorder statistics to use a single | ||
# session. | ||
with recorder_util.session_scope(hass=hass, read_only=True) as session: | ||
compiled = _compile_statistics(hass, session, start, end) | ||
return compiled | ||
|
||
|
||
def _compile_statistics( # noqa: C901 | ||
def compile_statistics( # noqa: C901 | ||
hass: HomeAssistant, | ||
session: Session, | ||
start: datetime.datetime, | ||
|
@@ -471,8 +450,8 @@ def _compile_statistics( # noqa: C901 | |
if "sum" in wanted_statistics[entity_id]: | ||
to_query.add(entity_id) | ||
|
||
last_stats = statistics.get_latest_short_term_statistics( | ||
hass, to_query, {"last_reset", "state", "sum"}, metadata=old_metadatas | ||
last_stats = statistics.get_latest_short_term_statistics_with_session( | ||
hass, session, to_query, {"last_reset", "state", "sum"}, metadata=old_metadatas | ||
) | ||
Comment on lines
+453
to
455
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the only call that created a new session here which is the source of the issue |
||
for ( # pylint: disable=too-many-nested-blocks | ||
entity_id, | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have refactored this as soon as I noticed this was happening when I added this comment in f6f3565 This wasn't the source of the problem, but its when I noticed this pattern.
I added the comment because I was worried this was a bit brittle but I didn't realize it was actually a problem as well at the time because I didn't understand the full impact of the nested sessions.
The irony is, I was too concerned about refactoring risk that I under-estimated the impact here even though I thought it was a problem enough to add this comment 🤦