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 history charts not auto refreshing with cached history #12873

Merged
merged 3 commits into from Jun 6, 2022

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Jun 4, 2022

Proposed change

The git archaeology on this is very difficult to sort out. Best I can tell: while fixing the history caching in #12788 made the cache work now (so we aren't constantly overloading the backend with history requests), it also re-introduced a regression similar to #2061 . I'm not sure that original issue was completely fixed, but the working theory is that since the history cache was no longer effective after #7072 it had the effect of solving it and fixing the history cache reintroduced the regression.

Fixes #12859

Unrelated to this PR, but I think getRecent (not getRecentWithCache) is dead code AFAICT.

Type of change

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

Example configuration

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue or discussion:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

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

if (stateHistory.timeline.length) {
mergeTimeline(stateHistory.timeline, cache.data.timeline);
// Replace the timeline array to force an update
cache.data.timeline = [...cache.data.timeline];
Copy link
Member Author

Choose a reason for hiding this comment

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

Since mutating the array doesn't trigger the update I did this to force it. Is there a better way?

Copy link
Member

Choose a reason for hiding this comment

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

I would expect mergeLine and mergeTimeline to create new arrays.

Copy link
Member

Choose a reason for hiding this comment

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

cache.data.timeline = mergeTimeline(stateHistory.timeline, cache.data.timeline);

Copy link
Member Author

Choose a reason for hiding this comment

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

That didn't work out very well unfortunately since the update is a partial update and the current code enumerates the new data so if the entity didn't update in history call it disappears.

Copy link
Member Author

Choose a reason for hiding this comment

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

I ended up reverting the last commit because I think its too risky to refactor without adding a whole lot of tests.

@@ -191,6 +197,8 @@ const mergeLine = (
oldLine.data.push(entity);
}
});
// Replace the cached line data to force an update
oldLine.data = [...oldLine.data];
Copy link
Member Author

Choose a reason for hiding this comment

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

Since mutating the array doesn't trigger the update I did this to force it. Is there a better way?

Copy link
Member Author

Choose a reason for hiding this comment

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

This one is even more complex since its arrays in arrays and it has the same issue of only enumerating the new data

@bdraco bdraco changed the title Fix history charts refreshing with cached history Fix history charts not auto refreshing with cached history Jun 4, 2022
@bdraco bdraco force-pushed the fix_updates_cache_history branch from 9c2f9b5 to 8a60afc Compare June 4, 2022 11:32
@bdraco bdraco marked this pull request as ready for review June 4, 2022 11:36
@bdraco bdraco force-pushed the fix_updates_cache_history branch from 8a60afc to 2b0e265 Compare June 6, 2022 05:31
This reverts commit 2b0e265.
@bdraco
Copy link
Member Author

bdraco commented Jun 6, 2022

Not sure how to proceed on this one. I reverted the last commit since the changes hit my risk tolerance for a bug fix as caching is so easy to get wrong.

There isn't tests for these so I didn't feel comfortable doing a lot of refactoring here since this one is non trivial to test and would be easy to miss a new bug.

@balloob
Copy link
Member

balloob commented Jun 6, 2022

meh looking at this code and it's indeed confusing.

Do we even need caching ? It seems like if we are showing a more-info, we always want to show latest, and if a user is bouncing between tabs it can be locally cached, not at the top-level.

@bdraco
Copy link
Member Author

bdraco commented Jun 6, 2022

I think we only need caching for the case where they have a whole page of history graph cards which I'm learning is apparently a more common use case than expected. 😢

Without caching we would be hammering the backend with requests for 24 hours of history data every time there is a state change for an entity that is being graphed instead of just the new data.

@balloob balloob merged commit a47a0ed into home-assistant:dev Jun 6, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 8, 2022
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.

history-graph card doesn't auto update with new values since 2022.06.0
3 participants