mirror of
https://github.com/home-assistant/core.git
synced 2025-08-03 12:45:28 +02:00
reduce
This commit is contained in:
@@ -1976,24 +1976,6 @@ def get_latest_short_term_statistics(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _get_most_recent_statistics_subquery(
|
|
||||||
metadata_ids: set[int], table: type[StatisticsBase], start_time_ts: float
|
|
||||||
) -> Subquery:
|
|
||||||
"""Generate the subquery to find the most recent statistic row."""
|
|
||||||
return (
|
|
||||||
select(
|
|
||||||
# https://github.com/sqlalchemy/sqlalchemy/issues/9189
|
|
||||||
# pylint: disable-next=not-callable
|
|
||||||
func.max(table.start_ts).label("max_start_ts"),
|
|
||||||
table.metadata_id.label("max_metadata_id"),
|
|
||||||
)
|
|
||||||
.filter(table.start_ts < start_time_ts)
|
|
||||||
.filter(table.metadata_id.in_(metadata_ids))
|
|
||||||
.group_by(table.metadata_id)
|
|
||||||
.subquery()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _statistics_at_time(
|
def _statistics_at_time(
|
||||||
session: Session,
|
session: Session,
|
||||||
metadata_ids: set[int],
|
metadata_ids: set[int],
|
||||||
@@ -2017,8 +1999,19 @@ def _statistics_at_time(
|
|||||||
columns = columns.add_columns(table.sum)
|
columns = columns.add_columns(table.sum)
|
||||||
|
|
||||||
start_time_ts = start_time.timestamp()
|
start_time_ts = start_time.timestamp()
|
||||||
most_recent_statistic_ids = _get_most_recent_statistics_subquery(
|
most_recent_statistic_ids = (
|
||||||
metadata_ids, table, start_time_ts
|
# https://github.com/sqlalchemy/sqlalchemy/issues/9189
|
||||||
|
# pylint: disable-next=not-callable
|
||||||
|
lambda_stmt(
|
||||||
|
lambda: select(
|
||||||
|
func.max(table.start_ts).label("max_start_ts"),
|
||||||
|
table.metadata_id.label("max_metadata_id"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.filter(table.start_ts < start_time_ts)
|
||||||
|
.filter(table.metadata_id.in_(metadata_ids))
|
||||||
|
.group_by(table.metadata_id)
|
||||||
|
.subquery()
|
||||||
)
|
)
|
||||||
stmt = lambda_stmt(lambda: columns).join(
|
stmt = lambda_stmt(lambda: columns).join(
|
||||||
most_recent_statistic_ids,
|
most_recent_statistic_ids,
|
||||||
|
Reference in New Issue
Block a user