From 934633c3260bb82849543cc7f1eed00b28ad884f Mon Sep 17 00:00:00 2001 From: Philip Kheav Date: Mon, 15 Mar 2021 15:52:55 +0000 Subject: [PATCH] feat(event scrape): add filtering of events by time --- drivers/place/event_scrape.cr | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/drivers/place/event_scrape.cr b/drivers/place/event_scrape.cr index f53519c93c..2a34c78815 100644 --- a/drivers/place/event_scrape.cr +++ b/drivers/place/event_scrape.cr @@ -46,6 +46,9 @@ class Place::EventScrape < PlaceOS::Driver logger.debug { "Getting bookings for zones" } logger.debug { @zone_ids.inspect } + start_epoch = Time.utc.at_beginning_of_day.to_unix + end_epoch = start_epoch + 86400 # seconds in a day + @zone_ids.each do |z_id| staff_api.systems(zone_id: z_id).get.as_a.each do |sys| sys_id = sys["id"].as_s @@ -55,7 +58,7 @@ class Place::EventScrape < PlaceOS::Driver response[:systems][sys_id] = SystemWithEvents.new( name: sys["name"].as_s, zones: Array(String).from_json(sys["zones"].to_json), - events: get_system_bookings(sys_id) + events: get_system_bookings(sys_id, start_epoch, end_epoch) ) end end @@ -63,11 +66,24 @@ class Place::EventScrape < PlaceOS::Driver response end - def get_system_bookings(sys_id : String) : Array(Event) + def get_system_bookings(sys_id : String, start_epoch : Int64?, end_epoch : Int64?) : Array(Event) booking_module = staff_api.modules_from_system(sys_id).get.as_a.find { |mod| mod["name"] == "Bookings" } # If the system has a booking module with bookings if booking_module && (bookings = staff_api.get_module_state(booking_module["id"].as_s).get["bookings"]?) - JSON.parse(bookings.as_s).as_a.map { |b| Event.from_json(b.to_json) } + bookings = JSON.parse(bookings.as_s).as_a.map { |b| Event.from_json(b.to_json) } + + # If both start_epoch and end_epoch are passed + if start_epoch && end_epoch + # Convert start/end_epoch to Time object as Event.event_start.class == Time + start_time = Time.unix(start_epoch) + end_time = Time.unix(end_epoch) + range = (start_time..end_time) + # Only select bookings within start_epoch and end_epoch + bookings.select! { |b| range.includes?(b.event_start) } + bookings + end + + bookings else [] of Event end