Efficiently retrieving ice_cube schedules for a given time period
Asked Answered
K

2

7

I'm looking into using Ice Cube https://github.com/seejohnrun/ice_cube for recurring events. My question is, if I then need to get any events that fall within a given time period (say, on a day or within a week), is there any better way than to loop through them all like this:

items = Records.find(:all)
items.each do |item|
  schedule = item.schedule
  if schedule.occurs_on?(Date.new)
      #if today is a recurrence, add to array
  end
end

This seems horribly inefficient but I'm not sure how else to go about it.

Kane answered 30/5, 2012 at 22:20 Comment(1)
What did you end up doing about it? im getting stuck in the same place?thanksSoddy
M
5

That's one approach - but what people do more often is end up denormalizing their schedules into a format that is conveniently queryable.

You may have a collection called something like ScheduleOccurrences - that you build each week / and then query that instead.

Its unfortunate it has to work this way, but sticking to the iCal way of managing schedules has led IceCube to need to format its data in certain ways (specifically ways that can line up with the requirements of the iCal RFC).

I've been doing some thinking recently about what a library would look like that shook away some of those restrictions, for greater flexibility like this - but its definitely still a bit off.

Hope this helps

Mogador answered 4/6, 2012 at 16:5 Comment(2)
Thanks, John. But what happens when the user browses in the calendar past next week?Kane
Maybe you could generate ScheduleOccurrences as they are needed? So if someone requests next week's schedule, the server could save the rows. I'm currently trying to figure out how to do repeating events in my rails ap...Phyllisphylloclade
C
0

I faced a similar problem and here was my approach:

Create a column on Event table to store the next occurrence date, and write a method which stores that value after_save. (method available through ice_cube. Perhaps index column too for faster querying.)

Then you can query the database for occurrences happening in the timeframe you need. See below:

Event.where(next_occurrence: Date.today.all_day)

Store EventOccurrences on a separate table.

Update the next_occurrence column for the rows returned to you by your query. Or something similar. This works for me because I'm running a daily job, so that update next_occurrence will run regularly. But you may need to tweak a bit.

Cleveland answered 1/6, 2018 at 16:21 Comment(1)
It was an answer that I hadn't fully tested yet. I revised it to try and be more clear.Cleveland

© 2022 - 2024 — McMap. All rights reserved.