Render weekly highlights considering periods of inactivity

This commit is contained in:
Jorge Manrubia
2025-09-01 17:09:43 +02:00
parent 5b68750713
commit dfbc77c2e5
8 changed files with 64 additions and 26 deletions
+16 -3
View File
@@ -1,14 +1,23 @@
class PeriodHighlights < ApplicationRecord
class << self
def create_or_find_for(collections, starts_at:, duration: 1.week)
starts_at = normalize_anchor_date(starts_at)
self.for(collections, starts_at:, duration:) || create_for(collections, starts_at:, duration:)
end
def create_for(collections, starts_at:, duration: 1.week)
starts_at = normalize_anchor_date(starts_at)
key = key_for(collections)
events = Event.where(collection: collections).where(created_at: starts_at..starts_at + duration)
create_or_find_by!(key:, starts_at:, duration:) do |record|
if events.any?
summarizer = Event::Summarizer.new(events)
record.content = summarizer.summarized_content
record.cost_in_microcents = summarizer.cost.in_microcents
summarized_content = summarizer.summarized_content # outside of transaction as this can be slow
create_or_find_by!(key:, starts_at:, duration:) do |record|
record.content = summarized_content
record.cost_in_microcents = summarizer.cost.in_microcents
end
end
end
@@ -28,6 +37,10 @@ class PeriodHighlights < ApplicationRecord
end
end
def ends_at
starts_at + duration
end
def to_html
renderer = Redcarpet::Render::HTML.new
markdowner = Redcarpet::Markdown.new(renderer, autolink: true, tables: true, fenced_code_blocks: true, strikethrough: true, superscript: true,)
+12
View File
@@ -29,7 +29,19 @@ class User::DayTimeline
day.yesterday.beginning_of_day
end
def has_weekly_highlights?
first_day_with_activity_this_week? && weekly_highlights.present?
end
def weekly_highlights
@weekly_highlights ||= user.weekly_highlights_for(day - 1.week)
end
private
def first_day_with_activity_this_week?
day.monday? || (earliest_time.present? && earliest_time < day.beginning_of_week(:monday))
end
def filtered_events
@filtered_events ||= begin
events = Event.where(collection: collections)
+6 -6
View File
@@ -13,16 +13,16 @@ module User::Highlights
end
end
def generate_weekly_highlights
PeriodHighlights.create_for collections, starts_at: current_highlights_starts_at, duration: 1.week
def generate_weekly_highlights(date = Time.current)
PeriodHighlights.create_or_find_for collections, starts_at: highlights_starts_at(date), duration: 1.week
end
def current_weekly_highlights
PeriodHighlights.for collections, starts_at: current_highlights_starts_at, duration: 1.week
def weekly_highlights_for(date)
PeriodHighlights.for collections, starts_at: highlights_starts_at(date), duration: 1.week
end
private
def current_highlights_starts_at
Time.current.utc.beginning_of_week(:sunday)
def highlights_starts_at(date = Time.current)
date.utc.beginning_of_week(:sunday)
end
end
+11 -9
View File
@@ -1,16 +1,18 @@
<section class="events__day" data-controller="related-element"
data-related-element-highlight-class="event--related">
<% if day_timeline.has_activity? %>
<h2 class="events__day-header">
<span class="events__day-time min-width max-width">
<span class="overflow-ellipsis">
<span>Highlights for</span>
<%= local_datetime_tag day_timeline.day, style: :agoorweekday %>
</span>
</span>
</h2>
<%= render "events/weekly_summary" %>
<% if day_timeline.has_weekly_highlights? %>
<h2 class="events__day-header">
<span class="events__day-time min-width max-width">
<span class="overflow-ellipsis">
<span>Highlights for this week</span>
</span>
</span>
</h2>
<% end %>
<%= render "events/weekly_highlights", day_timeline: day_timeline if day_timeline.has_weekly_highlights? %>
<div class="events__columns">
<%= render "events/day_timeline_column", event_type: "added", day_timeline: day_timeline %>
@@ -0,0 +1,3 @@
<div class="events__activity-summary txt-small">
<%= day_timeline.weekly_highlights.to_html %>
</div>
@@ -1,5 +0,0 @@
<% if weekly_highlights = Current.user.current_weekly_highlights %>
<div class="events__activity-summary txt-small">
<%= weekly_highlights.to_html %>
</div>
<% end %>
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby
require_relative "../config/environment"
WEEKS_TO_BACKFILL = 20
ApplicationRecord.with_each_tenant do |tenant|
WEEKS_TO_BACKFILL.times do |index|
User.find_each do |user|
user.generate_weekly_highlights(Time.current - index.weeks)
end
end
end
+3 -3
View File
@@ -9,17 +9,17 @@ class PeriodHighlightTest < ActiveSupport::TestCase
test "generate period highlights" do
period_highlights = assert_difference -> { PeriodHighlights.count }, 1 do
PeriodHighlights.create_for(@user.collections, starts_at: 1.month.ago, duration: 2.months)
PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months)
end
assert_match /logo/i, period_highlights.to_html
end
test "don't generate highlights for existing periods" do
new_period_highlights = PeriodHighlights.create_for(@user.collections, starts_at: 1.month.ago, duration: 2.months)
new_period_highlights = PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months)
existing_period_highlights = assert_no_difference -> { PeriodHighlights.count } do
PeriodHighlights.create_for(@user.collections, starts_at: 1.month.ago, duration: 2.months)
PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months)
end
assert_equal new_period_highlights, existing_period_highlights