[英]Using SQLalchemy ORM for Python In my REST api, how can I aggregate resources to the hour to the day?
我有一个 MySql 数据库表,它看起来像:
time_slot | sales
2022-08-026T01:00:00 | 100
2022-08-026T01:06:40 | 103
...
我通过 api 向客户提供数据。 FE 工程师希望在查询期间(atm 是一周)内每天按小时汇总数据。 因此,他给出了from
和to
并希望将每天每个小时内的销售额总和作为嵌套数组。 因为是一周,所以它是一个 7 元素数组,其中每个元素都是一个数组,其中包含我们拥有数据的所有每小时时段。
[
[
"07:00": 567,
"08:00": 657,
....
],
[], [], ...
]
api 内置于 python 中。 数据有一个 ORM (sqlalchemy),如下所示:
class HourlyData(Base):
hour: Column(Datetime)
sales: Column(Float)
我可以查询每小时数据,然后在 python memory 将其聚合到列表列表中。 但为了节省计算时间(和概念复杂性),我想通过 orm 查询运行聚合。
实现此目的的 sqlalchemy 语法是什么?
下面应该让您开始,其中解决方案是使用现有工具的 SQL 和 Python 的混合,它应该适用于任何 RDBMS。
from itertools import groupby
import json
class TimelyData(Base):
__tablename__ = "timely_data"
id = Column(Integer, primary_key=True)
time_slot = Column(DateTime)
sales = Column(Float)
# below works for Posgresql (tested), and should work for MySQL as well
# see: https://mode.com/blog/date-trunc-sql-timestamp-function-count-on
col_hour = func.date_trunc("hour", TimelyData.time_slot)
q = (
session.query(
col_hour.label("hour"),
func.sum(TD.sales).label("total_sales"),
)
.group_by(col_hour)
.order_by(col_hour) # this is important for `groupby` function later on
)
groupby
再次按日期对结果进行分组groups = groupby(q.all(), key=lambda row: row.hour.date())
# truncate and format the final list as required
data = [
[(f"{row.hour:%H}:00", int(row.total_sales)) for row in rows]
for _, rows in groups
]
[[["01:00", 201], ["02:00", 102]], [["01:00", 103]], [["08:00", 104]]]
我不熟悉 MySQL,但使用 Postgresql,由于广泛的 JSON 支持,可以在 DB 级别实现所有功能。 但是,我认为该实现的可读性不会提高,假设我们从数据库中获取最多 168 行 = 7 天 x 24 小时)的速度也不会提高。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.