mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Use a single session to compile statistics (#88405)
We had two session_scope contexts in compile_statistics which causes a commit to happen twice during the compile.
This commit is contained in:
parent
3ca9f3c0d5
commit
d4376b2041
@ -737,7 +737,10 @@ def compile_statistics(instance: Recorder, start: datetime, fire_events: bool) -
|
||||
end = start + timedelta(minutes=5)
|
||||
|
||||
# Return if we already have 5-minute statistics for the requested period
|
||||
with session_scope(session=instance.get_session()) as session:
|
||||
with session_scope(
|
||||
session=instance.get_session(),
|
||||
exception_filter=_filter_unique_constraint_integrity_error(instance),
|
||||
) as session:
|
||||
if session.query(StatisticsRuns).filter_by(start=start).first():
|
||||
_LOGGER.debug("Statistics already compiled for %s-%s", start, end)
|
||||
return True
|
||||
@ -763,10 +766,6 @@ def compile_statistics(instance: Recorder, start: datetime, fire_events: bool) -
|
||||
current_metadata.update(compiled.current_metadata)
|
||||
|
||||
# Insert collected statistics in the database
|
||||
with session_scope(
|
||||
session=instance.get_session(),
|
||||
exception_filter=_filter_unique_constraint_integrity_error(instance),
|
||||
) as session:
|
||||
for stats in platform_stats:
|
||||
metadata_id = _update_or_add_metadata(
|
||||
session, stats["meta"], current_metadata
|
||||
|
Loading…
x
Reference in New Issue
Block a user