-
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
base: master
Are you sure you want to change the base?
Conversation
|
||
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 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 correctness
record MulesoftBooking,
getter title : String?,
getter recurring_master_id : Int64,
getter event_start : Int64,
getter event_end : Int64,
getter body : String do
include JSON::Serializable
@[JSON::Field(ignore: true)]
# Not sure about the efficiency of doing this pattern in a struct.
getter resolved_title : String do
"#{recurring_master_id} #{title.presence || body}"
end
def matches?(booking)
booking = booking.as_h if booking.is_a?(JSON::Any)
event_start == booking["event_start"]? && event_end == booking["event_end"]? && resolved_title == booking["title"]?
end
def to_place_calendar(
timezone : String,
system_email : String,
system_name : String,
)
{
title: resolved_title,
event_start: event_start,
event_end: event_end,
timezone: timezone,
description: body,
user_id: system_email,
location: system_name,
attendees: [{email: system_email, name: system_name}],
}
end
end
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.
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.
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
@bookings = Array(Hash(String, Int64 | String | Nil)).from_json(mulesoft_bookings) | |
@bookings = Array(MulesoftBooking).from_json(mulesoft_bookings) |
@@ -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 comment
The 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
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 |
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.
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
@w-le I can whack in the suggestions I made if that's good for you? |
Thanks for these recommendations! The changes here should address them. Do let me know if anything else.
Apologies for committing directly to main branch instead of a PR from feat>main. I was in the middle of a client deployment change window and realised that the UAT instance was running off my fork of drivers while the PROD instance was on placeos/drivers. Will make sure to have new drivers reviewed in a PR PRIOR to deployments in the future.