如何聚合特定时间段内的 activerecord 模型数据?

发布于 2024-09-01 15:58:06 字数 177 浏览 1 评论 0原文

我每隔约 10 秒从系统收集一次数据(此时间差因与网络设备的通信时间而异)。我想每天计算此 activerecord 模型的存储值的平均值和总和。所有记录均以 UTC 格式存储。

例如,前一天从午夜到东部标准时间午夜的求和和平均值的正确方法是什么?我可以在 sql 中执行此操作,但不知道进行此计算的“rails way”。

I'm collecting data from a system every ~10s (this time difference varies due to communication time with networked devices). I'd like to calculate averages and sums of the stored values for this activerecord model on a daily basis. All records are stored in UTC.

What's the correct way to sum and average values for, e.g., the previous day from midnight to midnight EST? I can do this in sql but don't know the "rails way" to make this calculation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖正好 2024-09-08 15:58:06

试试这个:

tz = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")
range = 1.day.ago(tz.now).beginning_of_day..1.day.ago(tz.now).end_of_day
total = Model.sum(:column, :conditions => {created_at >= range})

Try this:

tz = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")
range = 1.day.ago(tz.now).beginning_of_day..1.day.ago(tz.now).end_of_day
total = Model.sum(:column, :conditions => {created_at >= range})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文