Skip to content
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
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

w-le
Copy link
Contributor

@w-le w-le commented May 24, 2021

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.

@w-le w-le self-assigned this May 24, 2021
@w-le w-le requested a review from caspiano May 24, 2021 14:36
caspiano
caspiano previously approved these changes May 25, 2021

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)
Copy link
Contributor

@caspiano caspiano Jun 2, 2021

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

Copy link
Contributor Author

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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}" }
Copy link
Contributor

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

Comment on lines +99 to 105
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
Copy link
Contributor

@caspiano caspiano Jun 2, 2021

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

@caspiano
Copy link
Contributor

@w-le I can whack in the suggestions I made if that's good for you?

@caspiano caspiano added the type: enhancement new feature or request label Jul 11, 2021
@w-le w-le requested review from caspiano and removed request for caspiano September 22, 2023 06:10
@w-le w-le marked this pull request as draft September 22, 2023 06:10
@w-le w-le requested a review from KesterJJ September 22, 2023 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants