Skip to content
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 statistic_during_period wrongly prioritizing ST statistics over LT #115291

Merged
merged 10 commits into from
Jun 10, 2024

Conversation

karwosts
Copy link
Contributor

@karwosts karwosts commented Apr 9, 2024

Proposed change

I believe the statistic_during_period WS API function has a bug where it may prioritize a short term statistic as the oldest datapoint over a long term statistic.

If the requested period is not hour aligned, we first look for the oldest short term statistic matching the head range and then return this as the oldest datapoint, but there may be older datapoints in long term statistics that are ignored in favor of returning this ST statistic.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@karwosts karwosts requested a review from a team as a code owner April 9, 2024 15:41
@home-assistant
Copy link

home-assistant bot commented Apr 9, 2024

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (recorder) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of recorder can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign recorder Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@emontnemery
Copy link
Contributor

@karwosts Could you please write a test case which fails without your changes and pass with it, the new test case should be added to tests/components/recorder/test_statistics.py

I'd be happy to discuss on Discord if you need some help with the test case

@karwosts
Copy link
Contributor Author

karwosts commented Jun 7, 2024

I might be willing to take a look at a test at some point but it seems like quite a significant learning curve, could be a little while.

I think all that's needed need is just a mocked statistics with overlapping long term and short term statistics like the following, and then check statistic_during_period for the window that straddles the switchover from LT to ST stats.

image

I believe the current function is only returning the change starting from the oldest short term stat to the end of the request window, ignoring the change between the start of the request window and the oldest short term stat.

@karwosts
Copy link
Contributor Author

karwosts commented Jun 8, 2024

I've made the start of a test, but as I add a lot of corner cases to the test, I'm still seeing some oddness I don't understand, and results that don't seem like what I would expect either before or after my fix.

I've left some annotations in the test if you want to look it over and see if it's making any sense to you, perhaps there are more bugs yet to be found in the code.

@emontnemery emontnemery added this to the 2024.6.2 milestone Jun 10, 2024
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Thanks a lot @karwosts 👍

@emontnemery emontnemery merged commit aa41968 into home-assistant:dev Jun 10, 2024
38 checks passed
@karwosts karwosts deleted the rolling_interval_fix branch June 10, 2024 18:25
)

# Six minutes of growth in STS-only
# 5-minute Change includes start times exactly on or before a statistics start, but end times are not counted unless they are greater than start.
Copy link
Member

Choose a reason for hiding this comment

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

Please break long comments around max 88 characters per line.

frenck pushed a commit that referenced this pull request Jun 11, 2024
#115291)

* Fix statistic_during_period wrongly prioritizing ST statistics over LT

* comment

* start of a test

* more testcases

* fix sts insertion range

* update from review

* remove unneeded comments

* update logic

* min/mean/max testing
@frenck frenck mentioned this pull request Jun 11, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Statistic card - when period is rolling window it only access the last 11-12 days of data
4 participants