Improve the use of bakeries in recorder (#69418)

pull/59194/head
Erik Montnemery 2022-04-06 20:00:10 +02:00 committed by GitHub
parent dd6a0e3a89
commit def305cf46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 22 deletions

View File

@ -121,8 +121,6 @@ QUERY_STATISTIC_META_ID = [
]
STATISTICS_BAKERY = "recorder_statistics_bakery"
STATISTICS_META_BAKERY = "recorder_statistics_meta_bakery"
STATISTICS_SHORT_TERM_BAKERY = "recorder_statistics_short_term_bakery"
# Convert pressure and temperature statistics from the native unit used for statistics
@ -187,8 +185,6 @@ class ValidationIssue:
def async_setup(hass: HomeAssistant) -> None:
"""Set up the history hooks."""
hass.data[STATISTICS_BAKERY] = baked.bakery()
hass.data[STATISTICS_META_BAKERY] = baked.bakery()
hass.data[STATISTICS_SHORT_TERM_BAKERY] = baked.bakery()
def _entity_id_changed(event: Event) -> None:
"""Handle entity_id changed."""
@ -422,7 +418,7 @@ def compile_hourly_statistics(
# Compute last hour's average, min, max
summary: dict[str, StatisticData] = {}
baked_query = instance.hass.data[STATISTICS_SHORT_TERM_BAKERY](
baked_query = instance.hass.data[STATISTICS_BAKERY](
lambda session: session.query(*QUERY_STATISTICS_SUMMARY_MEAN)
)
@ -481,7 +477,7 @@ def compile_hourly_statistics(
"sum": _sum,
}
else:
baked_query = instance.hass.data[STATISTICS_SHORT_TERM_BAKERY](
baked_query = instance.hass.data[STATISTICS_BAKERY](
lambda session: session.query(*QUERY_STATISTICS_SUMMARY_SUM_LEGACY)
)
@ -664,7 +660,7 @@ def get_metadata_with_session(
"""
# Fetch metatadata from the database
baked_query = hass.data[STATISTICS_META_BAKERY](
baked_query = hass.data[STATISTICS_BAKERY](
lambda session: session.query(*QUERY_STATISTIC_META)
)
if statistic_ids is not None:
@ -824,16 +820,13 @@ def _statistics_during_period_query(
hass: HomeAssistant,
end_time: datetime | None,
statistic_ids: list[str] | None,
bakery: Any,
base_query: Iterable,
baked_query: baked.BakedQuery,
table: type[Statistics | StatisticsShortTerm],
) -> Callable:
"""Prepare a database query for statistics during a given period.
This prepares a baked query, so we don't insert the parameters yet.
"""
baked_query = hass.data[bakery](lambda session: session.query(*base_query))
baked_query += lambda q: q.filter(table.start >= bindparam("start_time"))
if end_time is not None:
@ -970,17 +963,18 @@ def statistics_during_period(
if statistic_ids is not None:
metadata_ids = [metadata_id for metadata_id, _ in metadata.values()]
bakery = hass.data[STATISTICS_BAKERY]
if period == "5minute":
bakery = STATISTICS_SHORT_TERM_BAKERY
base_query = QUERY_STATISTICS_SHORT_TERM
baked_query = bakery(
lambda session: session.query(*QUERY_STATISTICS_SHORT_TERM)
)
table = StatisticsShortTerm
else:
bakery = STATISTICS_BAKERY
base_query = QUERY_STATISTICS
baked_query = bakery(lambda session: session.query(*QUERY_STATISTICS))
table = Statistics
baked_query = _statistics_during_period_query(
hass, end_time, statistic_ids, bakery, base_query, table
hass, end_time, statistic_ids, baked_query, table
)
stats = execute(
@ -1029,14 +1023,13 @@ def _get_last_statistics(
if not metadata:
return {}
bakery = hass.data[STATISTICS_BAKERY]
if table == StatisticsShortTerm:
bakery = STATISTICS_SHORT_TERM_BAKERY
base_query = QUERY_STATISTICS_SHORT_TERM
baked_query = bakery(
lambda session: session.query(*QUERY_STATISTICS_SHORT_TERM)
)
else:
bakery = STATISTICS_BAKERY
base_query = QUERY_STATISTICS
baked_query = hass.data[bakery](lambda session: session.query(*base_query))
baked_query = bakery(lambda session: session.query(*QUERY_STATISTICS))
baked_query += lambda q: q.filter_by(metadata_id=bindparam("metadata_id"))
metadata_id = metadata[statistic_id][0]