mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Improve the use of bakeries in recorder (#69418)
This commit is contained in:
parent
dd6a0e3a89
commit
def305cf46
@ -121,8 +121,6 @@ QUERY_STATISTIC_META_ID = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
STATISTICS_BAKERY = "recorder_statistics_bakery"
|
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
|
# Convert pressure and temperature statistics from the native unit used for statistics
|
||||||
@ -187,8 +185,6 @@ class ValidationIssue:
|
|||||||
def async_setup(hass: HomeAssistant) -> None:
|
def async_setup(hass: HomeAssistant) -> None:
|
||||||
"""Set up the history hooks."""
|
"""Set up the history hooks."""
|
||||||
hass.data[STATISTICS_BAKERY] = baked.bakery()
|
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:
|
def _entity_id_changed(event: Event) -> None:
|
||||||
"""Handle entity_id changed."""
|
"""Handle entity_id changed."""
|
||||||
@ -422,7 +418,7 @@ def compile_hourly_statistics(
|
|||||||
|
|
||||||
# Compute last hour's average, min, max
|
# Compute last hour's average, min, max
|
||||||
summary: dict[str, StatisticData] = {}
|
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)
|
lambda session: session.query(*QUERY_STATISTICS_SUMMARY_MEAN)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -481,7 +477,7 @@ def compile_hourly_statistics(
|
|||||||
"sum": _sum,
|
"sum": _sum,
|
||||||
}
|
}
|
||||||
else:
|
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)
|
lambda session: session.query(*QUERY_STATISTICS_SUMMARY_SUM_LEGACY)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -664,7 +660,7 @@ def get_metadata_with_session(
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Fetch metatadata from the database
|
# 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)
|
lambda session: session.query(*QUERY_STATISTIC_META)
|
||||||
)
|
)
|
||||||
if statistic_ids is not None:
|
if statistic_ids is not None:
|
||||||
@ -824,16 +820,13 @@ def _statistics_during_period_query(
|
|||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
end_time: datetime | None,
|
end_time: datetime | None,
|
||||||
statistic_ids: list[str] | None,
|
statistic_ids: list[str] | None,
|
||||||
bakery: Any,
|
baked_query: baked.BakedQuery,
|
||||||
base_query: Iterable,
|
|
||||||
table: type[Statistics | StatisticsShortTerm],
|
table: type[Statistics | StatisticsShortTerm],
|
||||||
) -> Callable:
|
) -> Callable:
|
||||||
"""Prepare a database query for statistics during a given period.
|
"""Prepare a database query for statistics during a given period.
|
||||||
|
|
||||||
This prepares a baked query, so we don't insert the parameters yet.
|
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"))
|
baked_query += lambda q: q.filter(table.start >= bindparam("start_time"))
|
||||||
|
|
||||||
if end_time is not None:
|
if end_time is not None:
|
||||||
@ -970,17 +963,18 @@ def statistics_during_period(
|
|||||||
if statistic_ids is not None:
|
if statistic_ids is not None:
|
||||||
metadata_ids = [metadata_id for metadata_id, _ in metadata.values()]
|
metadata_ids = [metadata_id for metadata_id, _ in metadata.values()]
|
||||||
|
|
||||||
|
bakery = hass.data[STATISTICS_BAKERY]
|
||||||
if period == "5minute":
|
if period == "5minute":
|
||||||
bakery = STATISTICS_SHORT_TERM_BAKERY
|
baked_query = bakery(
|
||||||
base_query = QUERY_STATISTICS_SHORT_TERM
|
lambda session: session.query(*QUERY_STATISTICS_SHORT_TERM)
|
||||||
|
)
|
||||||
table = StatisticsShortTerm
|
table = StatisticsShortTerm
|
||||||
else:
|
else:
|
||||||
bakery = STATISTICS_BAKERY
|
baked_query = bakery(lambda session: session.query(*QUERY_STATISTICS))
|
||||||
base_query = QUERY_STATISTICS
|
|
||||||
table = Statistics
|
table = Statistics
|
||||||
|
|
||||||
baked_query = _statistics_during_period_query(
|
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(
|
stats = execute(
|
||||||
@ -1029,14 +1023,13 @@ def _get_last_statistics(
|
|||||||
if not metadata:
|
if not metadata:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
bakery = hass.data[STATISTICS_BAKERY]
|
||||||
if table == StatisticsShortTerm:
|
if table == StatisticsShortTerm:
|
||||||
bakery = STATISTICS_SHORT_TERM_BAKERY
|
baked_query = bakery(
|
||||||
base_query = QUERY_STATISTICS_SHORT_TERM
|
lambda session: session.query(*QUERY_STATISTICS_SHORT_TERM)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
bakery = STATISTICS_BAKERY
|
baked_query = bakery(lambda session: session.query(*QUERY_STATISTICS))
|
||||||
base_query = QUERY_STATISTICS
|
|
||||||
|
|
||||||
baked_query = hass.data[bakery](lambda session: session.query(*base_query))
|
|
||||||
|
|
||||||
baked_query += lambda q: q.filter_by(metadata_id=bindparam("metadata_id"))
|
baked_query += lambda q: q.filter_by(metadata_id=bindparam("metadata_id"))
|
||||||
metadata_id = metadata[statistic_id][0]
|
metadata_id = metadata[statistic_id][0]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user