-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(mulesoft/exporter): status vars, perf, style, format #173
Draft
w-le
wants to merge
4
commits into
master
Choose a base branch
from
fix/mulesoft_exporter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cb27903
fix(mulesoft/exporter): status vars, perf, style, format
w-le d25cf4c
Merge branch 'master' into fix/mulesoft_exporter
w-le 822368e
style(mulesoft/exporter): whitespace
w-le c8f3f18
fix(mulesoft/exporter): clone booking before transform; clearer namin…
w-le File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,8 +5,8 @@ require "place_calendar" | |||||
|
||||||
class MuleSoft::CalendarExporter < PlaceOS::Driver | ||||||
descriptive_name "MuleSoft Bookings to Calendar Events Exporter" | ||||||
generic_name :Bookings | ||||||
description %(Retrieves and creates bookings using the MuleSoft API) | ||||||
generic_name :MulesoftExport | ||||||
description %(Listens for new MuleSoft bookings and creates matching Events in a Calendar) | ||||||
|
||||||
default_settings({ | ||||||
calendar_time_zone: "Australia/Sydney", | ||||||
|
@@ -34,12 +34,13 @@ class MuleSoft::CalendarExporter < PlaceOS::Driver | |||||
|
||||||
@time_zone_string = setting?(String, :calendar_time_zone).presence | ||||||
@time_zone = Time::Location.load(@time_zone_string.not_nil!) if @time_zone_string | ||||||
self[:timezone] = Time.local.to_s | ||||||
self[:timezone] = @time_zone.to_s | ||||||
|
||||||
subscription = system.subscribe(:Bookings_1, :bookings) do |_subscription, mulesoft_bookings| | ||||||
logger.debug { "DETECTED changed in Mulesoft Bookings.." } | ||||||
logger.debug { "DETECTED change in Mulesoft Bookings..." } | ||||||
@bookings = Array(Hash(String, Int64 | String | Nil)).from_json(mulesoft_bookings) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
logger.debug { "#{@bookings.size} bookings in total" } | ||||||
self[:total_bookings] = @bookings.size | ||||||
|
||||||
update_events | ||||||
@bookings.each { |b| export_booking(b) } | ||||||
|
@@ -57,6 +58,7 @@ class MuleSoft::CalendarExporter < PlaceOS::Driver | |||||
logger.debug { "FETCHING existing Calendar events..." } | ||||||
@existing_events = fetch_events() | ||||||
logger.debug { "#{@existing_events.size} events in total" } | ||||||
self[:total_events] = @existing_events.size | ||||||
end | ||||||
|
||||||
protected def fetch_events(past_span : Time::Span = 14.days, future_span : Time::Span = 14.days) | ||||||
|
@@ -71,10 +73,11 @@ class MuleSoft::CalendarExporter < PlaceOS::Driver | |||||
).get.as_a | ||||||
end | ||||||
|
||||||
protected def export_booking(booking : Hash(String, Int64 | String | Nil)) | ||||||
# Mulesoft booking titles are often nil. Use the body instead in this case | ||||||
booking["title"] = booking["body"] if booking["title"].nil? | ||||||
booking["title"] = "#{booking["recurring_master_id"]} #{booking["title"]}" | ||||||
protected def export_booking(mulesoft_booking : Hash(String, Int64 | String | Nil)) | ||||||
# The resulting Calendar Event is to be slightly different to the Mulesoft Booking, so clone here and transform | ||||||
booking = mulesoft_booking.clone | ||||||
# Add the course code to the front of the booking title/body | ||||||
booking["title"] = "#{booking["recurring_master_id"]} #{booking["title"] || booking["body"]}" | ||||||
w-le marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
logger.debug { "Checking for existing events that match: #{booking}" } | ||||||
|
||||||
unless event_already_exists?(booking, @existing_events) | ||||||
|
@@ -88,28 +91,25 @@ class MuleSoft::CalendarExporter < PlaceOS::Driver | |||||
attendees: [@just_this_system], | ||||||
location: system.name.not_nil!, | ||||||
} | ||||||
logger.debug { ">>> EXPORTING booking #{new_event}" } | ||||||
logger.debug { ">>> EXPORTING booking as: #{new_event}" } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. protected def export_booking(booking : MulesoftBooking)
unless event_already_exists?(booking, @existing_events)
new_event = booking.to_place_calendar(@time_zone_string, system.email, system.name)
logger.debug { ">>> EXPORTING booking as: #{new_event}" }
calendar.create(**new_event)
end
end |
||||||
calendar.create_event(**new_event) | ||||||
end | ||||||
end | ||||||
|
||||||
protected def event_already_exists?(new_event : Hash(String, Int64 | String | Nil), existing_events : Array(JSON::Any)) | ||||||
existing_events.each do |existing_event| | ||||||
return true if events_match?(new_event, existing_event.as_h) | ||||||
end | ||||||
false | ||||||
protected def event_already_exists?(booking : Hash(String, Int64 | String | Nil), existing_events : Array(JSON::Any)) | ||||||
existing_events.any? { |existing_event| booking_matches_event?(booking, existing_event.as_h) } | ||||||
end | ||||||
|
||||||
protected def events_match?(event_a : Hash(String, Int64 | String | Nil), event_b : Hash(String, JSON::Any)) | ||||||
event_a.select("event_start", "event_end", "title") == event_b.select("event_start", "event_end", "title") | ||||||
protected def booking_matches_event?(booking : Hash(String, Int64 | String | Nil), event : Hash(String, JSON::Any)) | ||||||
booking.select("event_start", "event_end", "title") == event.select("event_start", "event_end", "title") | ||||||
end | ||||||
Comment on lines
+99
to
105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. protected def event_already_exists?(booking : MulesoftBooking, existing_events : Array(JSON::Any))
logger.debug { "Checking for existing events that match: #{booking.resolved_title}" }
existing_events.any? { |event| booking.matches?(event) }
end |
||||||
|
||||||
def delete_all_events(past_days : Int32 = 14, future_days : Int32 = 14) | ||||||
events = fetch_events(past_span: past_days.days, future_span: future_days.days) | ||||||
event_ids = events.map { |e| e["id"] } | ||||||
event_ids.each do |event_id| | ||||||
calendar.delete_event(calendar_id: system.email.not_nil!, event_id: event_id) | ||||||
events.each do |event| | ||||||
calendar.delete_event(calendar_id: system.email.not_nil!, event_id: event["id"]) | ||||||
end | ||||||
"Deleted #{event_ids.size} events" | ||||||
logger.debug { "DELETED #{events.size} events" } | ||||||
events.size | ||||||
end | ||||||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing something along these lines instead of the
Hash
would be much easier to maintain, faster, and ensures correctnessThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much, I'll grok this after some other tasks, then see if anything else should change and then test.
Yeah I was originally intending the booking hash and calendar event to be the same object but couldn't get that to work just due to my crystal inexperience. Ran out of time due to a delivery deadline so just implemented it this way.
Now that I have time it'll be great (and very educational for me) to implement it properly.