Adjust get_latest_short_term_statistics query to be postgresql compatible (#70953)

This commit is contained in:
J. Nick Koston 2022-04-27 18:19:36 -10:00 committed by GitHub
parent 79c9d22893
commit 27a4a9eed4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1136,16 +1136,20 @@ def get_latest_short_term_statistics(
] ]
most_recent_statistic_row = ( most_recent_statistic_row = (
session.query( session.query(
StatisticsShortTerm.id, StatisticsShortTerm.metadata_id,
func.max(StatisticsShortTerm.start), func.max(StatisticsShortTerm.start).label("start_max"),
) )
.filter(StatisticsShortTerm.metadata_id.in_(metadata_ids))
.group_by(StatisticsShortTerm.metadata_id) .group_by(StatisticsShortTerm.metadata_id)
.having(StatisticsShortTerm.metadata_id.in_(metadata_ids))
).subquery() ).subquery()
stats = execute( stats = execute(
session.query(*QUERY_STATISTICS_SHORT_TERM).join( session.query(*QUERY_STATISTICS_SHORT_TERM).join(
most_recent_statistic_row, most_recent_statistic_row,
StatisticsShortTerm.id == most_recent_statistic_row.c.id, (
StatisticsShortTerm.metadata_id # pylint: disable=comparison-with-callable
== most_recent_statistic_row.c.metadata_id
)
& (StatisticsShortTerm.start == most_recent_statistic_row.c.start_max),
) )
) )
if not stats: if not stats: