1

在我的项目中,我想从我的数据库中选择记录,然后将它们按与特定时间范围内的最新记录相似的时间发生的记录组合在一起。

例如,时间范围为 1 小时。如果用户在下午 4:30 到下午 5:15 之间创建了 3 个帖子,在下午 1:15 到下午 1:30 之间创建了 2 个帖子,在上午 10:00 发布了 1 个帖子,我希望创建如下结构:

user.posts.find(:all).group_by do |post|
  # (posts have a created_at column containing both a date and time)
  # Algorithm here
end

结果:

[
 [Tue March 31 5:15pm, [post6,post5,post4]]
 [Tue March 31 1:30pm, [post3,post2]]
 [Tue March 31 10:00am, [post1]]
]

对执行此操作的最佳算法有任何想法吗?如果您不了解 Ruby,伪代码也可以。

编辑:谢谢乔尔。这是我最终使用的代码(提要而不是帖子):

  def aggregate(feeds, timeLimit)
    return [] if feeds.blank?
    result = []
    bin = []
    feeds = feeds.sort_by { |f| -f.created_at.to_i }
    bin_time = feeds.first.created_at
    feeds.each do |feed|
      if (bin_time - feed.created_at) < timeLimit
        bin << feed
      else
        result << [bin_time, bin]
        bin_time = feed.created_at
        bin = [feed]
      end
    end
    result << [bin_time, bin]
    result
  end
4

2 回答 2

1

基本概念很简单,将帖子累积到垃圾箱中,然后当时间超出范围时,启动一个新垃圾箱。这是一个 Python 版本:

帖子 = [('post6', 1715), ('post5', 1645), ('post4', 1630)
    , ('post3', 1330), ('post2', 1315), ('post1', 1000)
    ]

rslt = []
斌 = []
binTime = 1 << 31
对于 postData,postTime 在帖子中:
    如果(postTime >= binTime - 100):
        bin.append(postData)
    别的:
        如果斌:
            rslt.append([binTime, bin])
        binTime = postTime
        bin = [postData]

如果斌:
    rslt.append([binTime, bin])

打印 rslt
于 2009-04-01T01:14:41.900 回答
0
if post.created_at - group_start > limit
    output current group if non-empty
    set group to current post
    set group_start to post.created_at
else
    add post to current group

然后,在循环之外,如果非空,则输出当前组。根据您访问帖子的顺序调整 if 条件。

于 2009-04-01T00:54:22.213 回答