i have to insert large data in lets say 20k i doubt i have written an optimised query.
users = MergeField.get_user_field_values(notification_template.merge_field,scope_users) **#returns 20k users**
if users.present?
users.each_slice(100) do |record|
record.each do |user_record|
user = User.find_by(id: user_record.user_id)
text = notification_template.title
notification_template.description = MustacheDescription.render(notification_template,user_record)
text += " " + notification_template.description
Rails.logger.info "Merge field message: #{notification_template.description}"
construct_user_notifications(notification_template,user_record.user_id) **#=> this calls another method below which actually create notifications for every user.**
badge = (notification_template.display_screen == "suggestion") ? user.unread_suggestion_notifications.count : user.unread_option_notifications.count
devices = user.devices.with_notification_token
if devices.present?
devices.each do |device|
PushNotification.notify_ios(text,device.notification_token,badge,{screen: notification_template.display_screen})
Rails.logger.info "Sending push to user_id #{user_record.user_id} token #{device.notification_token}"
end
end
end
end
end
def self.construct_user_notifications(notification_template,user_id)
notification_template.user_notifications.build.tap do |user_notification|
user_notification.title = notification_template.title
user_notification.subtitle = notification_template.subtitle
user_notification.description = notification_template.description
user_notification.merge_field = notification_template.merge_field
user_notification.cta = notification_template.cta
user_notification.cta_key = notification_template.cta_key
user_notification.secondary_cta = notification_template.secondary_cta
user_notification.secondary_cta_key = notification_template.secondary_cta_key
user_notification.show_useful = notification_template.show_useful
user_notification.category = notification_template.category
user_notification.display_screen = notification_template.display_screen
user_notification.sent_at = Time.current
user_notification.user_id = user_id
user_notification.filter_preferences = notification_template.filter_preferences
user_notification.save
end
end
I have tested this for 100 users and it takes 30-40 secs. god knows how much it would take for 20k users in prod.
Aucun commentaire:
Enregistrer un commentaire