diff --git a/app/models/period_highlights.rb b/app/models/period_highlights.rb index 391b5b75c..9e62403d0 100644 --- a/app/models/period_highlights.rb +++ b/app/models/period_highlights.rb @@ -3,6 +3,8 @@ # so that we reuse the same summary for users with different time zones or different accesses as long as the activity # is the same. This is important to keep AI costs down. class PeriodHighlights < ApplicationRecord + has_many :weekly_highlights, class_name: "User::WeeklyHighlights", dependent: :destroy + class << self def create_or_find_for(collections, starts_at:, duration: 1.week) self.for(collections, starts_at:, duration:) || create_for(collections, starts_at:, duration:) diff --git a/app/models/user/highlights.rb b/app/models/user/highlights.rb index 43a6745f8..86a015c04 100644 --- a/app/models/user/highlights.rb +++ b/app/models/user/highlights.rb @@ -1,6 +1,10 @@ module User::Highlights extend ActiveSupport::Concern + included do + has_many :weekly_highlights, class_name: "User::WeeklyHighlights", dependent: :destroy + end + class_methods do def generate_all_weekly_highlights_later User::Highlights::GenerateAllJob.perform_later @@ -15,20 +19,31 @@ module User::Highlights def generate_weekly_highlights(date = Time.current) in_time_zone do - date = date - 1.day if date.sunday? - PeriodHighlights.create_or_find_for collections, starts_at: highlights_starts_at(date), duration: 1.week + weekly_highlights_for(date) || create_weekly_highlights_for(date) end end def weekly_highlights_for(date) in_time_zone do - PeriodHighlights.for(collections, starts_at: highlights_starts_at(date), duration: 1.week) + weekly_highlights.find_by(starts_at: highlights_starts_at(date))&.period_highlights end end private + def create_weekly_highlights_for(date) + date = date - 1.day if date.sunday? + + # Outside of transaction as generating highlights can be a slow operation + PeriodHighlights.create_or_find_for(collections, starts_at: highlights_starts_at(date), duration: 1.week).tap do |period_highlights| + if period_highlights + puts "Generating highlights for #{date}: #{period_highlights.inspect}" + weekly_highlights.create! period_highlights: period_highlights, starts_at: highlights_starts_at(date) + end + end + end + def highlights_starts_at(date = Time.current) date = date.in_time_zone(timezone) - date.beginning_of_week(:sunday) + date.beginning_of_week(:sunday).to_date end end diff --git a/app/models/user/weekly_highlights.rb b/app/models/user/weekly_highlights.rb new file mode 100644 index 000000000..502992886 --- /dev/null +++ b/app/models/user/weekly_highlights.rb @@ -0,0 +1,6 @@ +# This acts as a join table between users and period_highlights so that we can reuse the +# same highlights for different users. +class User::WeeklyHighlights < ApplicationRecord + belongs_to :user + belongs_to :period_highlights +end diff --git a/app/views/events/_weekly_highlights.html.erb b/app/views/events/_weekly_highlights.html.erb index 023bca756..e0bf21538 100644 --- a/app/views/events/_weekly_highlights.html.erb +++ b/app/views/events/_weekly_highlights.html.erb @@ -1,3 +1,4 @@ +
REQUESTING WEEKLY HIGHLIGHTS FOR <%= day_timeline.week_starts_at %>: <%= day_timeline.week_starts_at - 1.week%>
<% if day_timeline.has_weekly_highlights? %>