Skip to content

Commit

Permalink
[VMD] Fix VMD service (#1017)
Browse files Browse the repository at this point in the history
* [VMD] Fix VMD service

* fix rb

* replace slots_7_days

* disable quality workflow for now
  • Loading branch information
mathieuripert committed Dec 6, 2021
1 parent d4d8b44 commit edc92ef
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/quality.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Quality

# triggers on
on: [push, pull_request]
# on: [push, pull_request]

jobs:
quality:
Expand Down
4 changes: 4 additions & 0 deletions .standard_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Auto generated files with errors to ignore.
# Remove from this list as you refactor files.
---
ignore: []
2 changes: 1 addition & 1 deletion app/mailers/slot_alert_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def notify
@passwordless_token = Devise::Passwordless::LoginToken.encode(@user)

distance = distance_delta({lat: @alert.user.lat, lon: @alert.user.lon}, {lat: @slot.latitude, lon: @slot.longitude})
subject = "#{@slot.slots_7_days} créneaux disponibles à #{distance[:delta_in_words]}, dès #{l(@slot.next_rdv, format: "%A %e %B")}"
subject = "#{@slot.slots_count} créneaux disponibles à #{distance[:delta_in_words]}, dès #{l(@slot.next_rdv, format: "%A %e %B")}"
mail(
to: @alert.user.email,
subject: subject
Expand Down
16 changes: 5 additions & 11 deletions app/models/vmd_slot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class VmdSlot < ApplicationRecord
janssen: "Janssen"
}.freeze

def self.build_from_hash(slot)
def self.build_from_hash(slot, last_updated_at = nil)
slot.deep_symbolize_keys!
return if VmdSlot.where(center_id: slot[:internal_id]).where("last_updated_at >= ?", slot[:last_scan_with_availabilities].to_datetime - 1.seconds).any?
return if VmdSlot.where(center_id: slot[:internal_id]).where("last_updated_at >= ?", last_updated_at - 1.seconds).any?
VmdSlot.create(
center_id: slot[:internal_id],
url: slot[:url],
Expand Down Expand Up @@ -47,13 +47,7 @@ def self.build_from_hash(slot)
center_type: slot[:type],
platform: slot[:plateforme],
slots_count: slot[:appointment_count],
last_updated_at: slot[:last_scan_with_availabilities],
slots_0_days: VmdSlot.get_appointment_schedule(slot, "chronodose"),
slots_1_days: VmdSlot.get_appointment_schedule(slot, "1_days"),
slots_2_days: VmdSlot.get_appointment_schedule(slot, "2_days"),
slots_7_days: VmdSlot.get_appointment_schedule(slot, "7_days"),
slots_28_days: VmdSlot.get_appointment_schedule(slot, "28_days"),
slots_49_days: VmdSlot.get_appointment_schedule(slot, "49_days"),
last_updated_at: last_updated_at,
pfizer: (slot[:vaccine_type] || []).include?(VACCINE_TYPES[:pfizer]),
moderna: (slot[:vaccine_type] || []).include?(VACCINE_TYPES[:moderna]),
janssen: (slot[:vaccine_type] || []).include?(VACCINE_TYPES[:janssen]),
Expand Down Expand Up @@ -110,7 +104,7 @@ def reachable_users(user_alerting_intensity = nil)
lat: latitude,
lon: longitude,
user_alerting_intensity: user_alerting_intensity,
limit: slots_7_days * OVERBOOKING_FACTOR
limit: slots_count * OVERBOOKING_FACTOR
}
query = ActiveRecord::Base.send(:sanitize_sql_array, [sql, params])
User.where(id: ActiveRecord::Base.connection.execute(query).to_a.pluck("user_id"))
Expand All @@ -128,7 +122,7 @@ def self.find_slots_for_user(user_id)
.where("last_updated_at >= ?", 11.minutes.ago)
.where("(pfizer is true or moderna is true) and astrazeneca is false")
.where("(SQRT(((latitude - ?)*110.574)^2 + ((longitude - ?)*111.320*COS(latitude::float*3.14159/180))^2)) < ? ", user.lat, user.lon, user.max_distance_km)
.where("slots_7_days > 10")
.where("slots_count > 10")
.order("next_rdv asc")
.limit(100)
end
Expand Down
9 changes: 3 additions & 6 deletions app/services/slot_alert_service.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class SlotAlertService
def initialize(days = 2, threshold = 20, user_alerting_intensity = nil)
@days = days
def initialize(threshold = 20, user_alerting_intensity = nil)
@threshold = threshold
@user_alerting_intensity = user_alerting_intensity
end

def call
Rails.logger.info("[SlotAlertService] for days=#{@days} threshold=#{@threshold} user_alerting_intensity=#{user_alerting_intensity}")
Rails.logger.info("[SlotAlertService] threshold=#{@threshold} user_alerting_intensity=#{user_alerting_intensity}")
slots.group_by { |x| x.center_id }.each do |center, slots|
slot = slots.first
SendAlertsForSlotJob.perform_later({slot_id: slot.id, user_alerting_intensity: @user_alerting_intensity})
Expand All @@ -17,9 +16,7 @@ def slots
slots = VmdSlot
.where("created_at >= ?", 11.minutes.ago)
.where("(pfizer is true or moderna is true) and astrazeneca is false")
slots = slots.where("slots_1_days >= ?", @threshold) if @days <= 1
slots = slots.where("slots_2_days >= ?", @threshold) if @days <= 2
slots = slots.where("slots_7_days >= ?", @threshold) if @days <= 7
slots = slots.where("slots_count >= ?", @threshold)
slots.order("created_at desc")
end
end
16 changes: 9 additions & 7 deletions app/services/vmd_parser_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ class VmdParserService

def parse
slots.each do |slot|
VmdSlot.build_from_hash(slot)
VmdSlot.build_from_hash(slot, last_updated_at)
end
end

def data
@data ||= self.class.get("/info_centres.json")
def slots
@slots ||= data["centres_disponibles"]
end

def slots
@centres ||= data.map do |k, v|
v["centres_disponibles"]
end.flatten
def last_updated_at
@last_updated_at ||= data["last_updated"].to_datetime
end

def data
@data ||= self.class.get("/info_centres.json")
end
end
2 changes: 1 addition & 1 deletion app/views/slot_alert_mailer/notify.mjml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<mj-column>
<mj-text padding-bottom="0px">
<h1>
<%= "#{@slot.slots_7_days} créneaux sont disponibles à #{distance[:delta_in_words]}" %>
<%= "#{@slot.slots_count} créneaux sont disponibles à #{distance[:delta_in_words]}" %>
</h1>
</mj-text>
</mj-column>
Expand Down
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit edc92ef

Please sign in to comment.