gpt4 book ai didi

ruby-on-rails - Ruby,时间间隔

转载 作者:数据小太阳 更新时间:2023-10-29 07:57:24 26 4
gpt4 key购买 nike

我有一个查询(针对 MongoDB 数据库),它返回已映射缩减的对象,每 15 分钟报告一次对象,但问题是,如果说我们在其中一台服务器中出现严重错误,那么这段时间将是下落不明。

以这个数组为例:

[
{:timestamp=>2011-09-26 19:00:00 UTC, :count=>318},
{:timestamp=>2011-09-26 19:15:00 UTC, :count=>308},
{:timestamp=>2011-09-26 19:30:00 UTC, :count=>222},
{:timestamp=>2011-09-26 19:45:00 UTC, :count=>215},
{:timestamp=>2011-09-26 20:00:00 UTC, :count=>166},
{:timestamp=>2011-09-26 21:15:00 UTC, :count=>149},
{:timestamp=>2011-09-26 21:30:00 UTC, :count=>145},
{:timestamp=>2011-09-26 21:45:00 UTC, :count=>107},
{:timestamp=>2011-09-26 22:00:00 UTC, :count=>137},
{:timestamp=>2011-09-26 22:15:00 UTC, :count=>135},
{:timestamp=>2011-09-26 22:30:00 UTC, :count=>191},
{:timestamp=>2011-09-26 22:45:00 UTC, :count=>235}
]

您会注意到时间范围内缺少时间:

{:timestamp=>2011-09-26 20:15:00 UTC},
{:timestamp=>2011-09-26 20:30:00 UTC},
{:timestamp=>2011-09-26 20:45:00 UTC},
{:timestamp=>2011-09-26 21:00:00 UTC}

我如何将顶部作为输入并推断出那些将是缺失的行?时间增量将始终为 15 分钟,它实际上是一个真实的日期对象,而不是那样的字符串。

我无法想象如何对此进行迭代。

如有任何帮助,我们将不胜感激。

最佳答案

我能想到的最简单的方法是按时间戳对数组进行排序,然后执行如下操作:

missing_times = []
reports.each_with_index do |report, index|
if reports[index + 1]
if report.timestamp.advance(minutes: 15) < report[index + 1].timestamp
i = 0
while(report.timestamp.advance(minutes: 15*i) < report[index+1].timestamp)
missing_times << report.timestamp.advance(minutes: 15*i)
end
end
end
end

我以前写过类似的代码来查找一系列约会中的半小时间隔

尽管看起来我的解决方案会在 reports.first 和 reports.last 之间的 15 分钟增量内循环多次,但实际上它只会在 reports.first 和 reports.last 之间的所有可用增量内循环一次

关于ruby-on-rails - Ruby,时间间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7562741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com