diff --git a/app/controllers/admin/prompt_sandboxes_controller.rb b/app/controllers/admin/prompt_sandboxes_controller.rb index b4d6578ea..15b54280d 100644 --- a/app/controllers/admin/prompt_sandboxes_controller.rb +++ b/app/controllers/admin/prompt_sandboxes_controller.rb @@ -5,10 +5,10 @@ class Admin::PromptSandboxesController < AdminController @llm_model = params[:llm_model] || Event::Summarizer::LLM_MODEL if @prompt = cookies[:prompt].presence - @weekly_highlights = build_weekly_highlights + @weekly_summary = build_weekly_summary cookies.delete :prompt else - @weekly_highlights = @day_timeline.weekly_highlights + @weekly_summary = @day_timeline.weekly_summary @prompt = Event::Summarizer::PROMPT end end @@ -21,10 +21,10 @@ class Admin::PromptSandboxesController < AdminController end private - def build_weekly_highlights - period = PeriodHighlights::Period.new(Current.user.collections, starts_at: @day_timeline.day.beginning_of_week(:sunday), duration: 1.week) + def build_weekly_summary + period = PeriodSummary::Period.new(Current.user.collections, starts_at: @day_timeline.day.beginning_of_week(:sunday), duration: 1.week) summarizer = Event::Summarizer.new(period.events, prompt: @prompt, llm_model: @llm_model) content = summarizer.summarized_content - PeriodHighlights.new(content: content, cost_in_microcents: summarizer.cost.in_microcents) + PeriodSummary.new(content: content, cost_in_microcents: summarizer.cost.in_microcents) end end diff --git a/app/jobs/user/highlights/generate_all_job.rb b/app/jobs/user/summaries/generate_all_job.rb similarity index 51% rename from app/jobs/user/highlights/generate_all_job.rb rename to app/jobs/user/summaries/generate_all_job.rb index bc6f7d78e..c7ff01076 100644 --- a/app/jobs/user/highlights/generate_all_job.rb +++ b/app/jobs/user/summaries/generate_all_job.rb @@ -1,9 +1,9 @@ -class User::Highlights::GenerateAllJob < ApplicationJob +class User::Summaries::GenerateAllJob < ApplicationJob queue_as :backend def perform ApplicationRecord.with_each_tenant do |tenant| - User.generate_all_weekly_highlights + User.generate_all_weekly_summaries end end end diff --git a/app/models/period_highlights.rb b/app/models/period_summary.rb similarity index 93% rename from app/models/period_highlights.rb rename to app/models/period_summary.rb index 9861d93ee..6674001bc 100644 --- a/app/models/period_highlights.rb +++ b/app/models/period_summary.rb @@ -2,8 +2,8 @@ # and a set of collections. We only store a key derived from the accessible events for those collections, # 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" +class PeriodSummary < ApplicationRecord + has_many :weekly_summaries, class_name: "User::WeeklySummary" class << self def create_or_find_for(collections, starts_at:, duration: 1.week) diff --git a/app/models/period_highlights/period.rb b/app/models/period_summary/period.rb similarity index 95% rename from app/models/period_highlights/period.rb rename to app/models/period_summary/period.rb index a0fe2b2b2..51f496c14 100644 --- a/app/models/period_highlights/period.rb +++ b/app/models/period_summary/period.rb @@ -1,4 +1,4 @@ -class PeriodHighlights::Period +class PeriodSummary::Period MIN_EVENTS_TO_BE_INTERESTING = 7 attr_reader :collections, :starts_at, :duration diff --git a/app/models/user.rb b/app/models/user.rb index 4757d4e37..9f6db5a4f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,5 @@ class User < ApplicationRecord - include Accessor, AiQuota, Assignee, Attachable, Configurable, Conversational, Highlights, + include Accessor, AiQuota, Assignee, Attachable, Configurable, Conversational, Summaries, Identifiable, Mentionable, Named, Notifiable, Role, Searcher, Staff, Transferable, Watcher include Timelined # Depends on Accessor diff --git a/app/models/user/day_timeline.rb b/app/models/user/day_timeline.rb index d04a10683..d20d48798 100644 --- a/app/models/user/day_timeline.rb +++ b/app/models/user/day_timeline.rb @@ -29,12 +29,12 @@ class User::DayTimeline day.yesterday.beginning_of_day end - def has_weekly_highlights? - !filter.used? && first_day_with_activity_this_week? && weekly_highlights.present? + def has_weekly_summary? + !filter.used? && first_day_with_activity_this_week? && weekly_summary.present? end - def weekly_highlights - @weekly_highlights ||= user.weekly_highlights_for(week_starts_at) + def weekly_summary + @weekly_summary ||= user.weekly_summary_for(week_starts_at) end def week_starts_at @@ -46,7 +46,7 @@ class User::DayTimeline end def cache_key - ActiveSupport::Cache.expand_cache_key [ user, filter, day.to_date, events, weekly_highlights ], "day-timeline" + ActiveSupport::Cache.expand_cache_key [ user, filter, day.to_date, events, weekly_summary ], "day-timeline" end private diff --git a/app/models/user/highlights.rb b/app/models/user/highlights.rb deleted file mode 100644 index 95b24341c..000000000 --- a/app/models/user/highlights.rb +++ /dev/null @@ -1,46 +0,0 @@ -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 - end - - def generate_all_weekly_highlights - # We're not interested in parallelizing individual generation. Better for AI quota limits and, also, - # most summaries will be reused for users accessing the same collections. - active.find_each(&:generate_weekly_highlights) - end - end - - def generate_weekly_highlights(date = Time.current) - in_time_zone do - weekly_highlights_for(date) || create_weekly_highlights_for(date) - end - end - - def weekly_highlights_for(date) - in_time_zone do - weekly_highlights.find_by(starts_at: highlights_starts_at(date).to_date)&.period_highlights - end - end - - private - def create_weekly_highlights_for(date) - # 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 - weekly_highlights.create! period_highlights: period_highlights, starts_at: highlights_starts_at(date).to_date - end - end - end - - def highlights_starts_at(date = Time.current) - date = date.in_time_zone(timezone) - date.beginning_of_week(:sunday) - 1.week - end -end diff --git a/app/models/user/summaries.rb b/app/models/user/summaries.rb new file mode 100644 index 000000000..cc13fa50f --- /dev/null +++ b/app/models/user/summaries.rb @@ -0,0 +1,46 @@ +module User::Summaries + extend ActiveSupport::Concern + + included do + has_many :weekly_summaries, class_name: "User::WeeklySummary", dependent: :destroy + end + + class_methods do + def generate_all_weekly_summaries_later + User::Summaries::GenerateAllJob.perform_later + end + + def generate_all_weekly_summaries + # We're not interested in parallelizing individual generation. Better for AI quota limits and, also, + # most summaries will be reused for users accessing the same collections. + active.find_each(&:generate_weekly_summary) + end + end + + def generate_weekly_summary(date = Time.current) + in_time_zone do + weekly_summary_for(date) || create_weekly_summary_for(date) + end + end + + def weekly_summary_for(date) + in_time_zone do + weekly_summaries.find_by(starts_at: summary_starts_at(date).to_date)&.period_summary + end + end + + private + def create_weekly_summary_for(date) + # Outside of transaction as generating summaries can be a slow operation + PeriodSummary.create_or_find_for(collections, starts_at: summary_starts_at(date), duration: 1.week).tap do |period_summary| + if period_summary + weekly_summaries.create! period_summary: period_summary, starts_at: summary_starts_at(date).to_date + end + end + end + + def summary_starts_at(date = Time.current) + date = date.in_time_zone(timezone) + date.beginning_of_week(:sunday) - 1.week + end +end diff --git a/app/models/user/weekly_highlights.rb b/app/models/user/weekly_highlights.rb deleted file mode 100644 index 502992886..000000000 --- a/app/models/user/weekly_highlights.rb +++ /dev/null @@ -1,6 +0,0 @@ -# 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/models/user/weekly_summary.rb b/app/models/user/weekly_summary.rb new file mode 100644 index 000000000..1e9ca47ae --- /dev/null +++ b/app/models/user/weekly_summary.rb @@ -0,0 +1,6 @@ +# This acts as a join table between users and period_summaries so that we can reuse the +# same summaries for different users. +class User::WeeklySummary < ApplicationRecord + belongs_to :user + belongs_to :period_summary +end diff --git a/app/views/admin/prompt_sandboxes/show.html.erb b/app/views/admin/prompt_sandboxes/show.html.erb index 73537537f..14e9ae904 100644 --- a/app/views/admin/prompt_sandboxes/show.html.erb +++ b/app/views/admin/prompt_sandboxes/show.html.erb @@ -28,7 +28,7 @@