Add reading_time and posting calendar reports (#1)

This commit is contained in:
Rafael dos Santos Silva 2024-12-12 11:12:04 -03:00 committed by GitHub
parent 52acb18ff0
commit 906f9d6e9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 84 additions and 24 deletions

View File

@ -1,23 +0,0 @@
# frozen_string_literal: true
module DiscourseRewind
class Rewind::Action::CreatePostsCountReport < Service::ActionBase
option :params
option :guardian
option :date
option :user
delegate :username, :year, to: :params, private: true
def call
p date
p user
p guardian
p username
p year
p params
{ data: { count: 5 }, identifier: "posts-count-report" }
end
end
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
# For a GitHub like calendar
# https://docs.github.com/assets/cb-35216/mw-1440/images/help/profile/contributions-graph.webp
module DiscourseRewind
class Rewind::Action::PostingCalendar < Service::ActionBase
option :user
option :date
def call
calendar =
Post
.where(user: user)
.where(created_at: date)
.select("DATE(created_at) as date, count(*) as count")
.group("DATE(created_at)")
.order("DATE(created_at)")
.map { |post| { date: post.date, count: post.count } }
{ data: calendar, identifier: "posting-calendar" }
end
end
end

View File

@ -0,0 +1,57 @@
# frozen_string_literal: true
# For showcasing the reading time of a user
# Should we show book covers or just the names?
module DiscourseRewind
class Rewind::Action::ReadingTime < Service::ActionBase
option :user
option :date
def call
reading_time = UserVisit.where(user: user).where(visited_at: date).sum(:time_read)
{
data: {
reading_time: reading_time,
books: best_book_fit(reading_time),
},
identifier: "reading-time",
}
end
def popular_book_reading_time
{
"The Hunger Games" => 19_740,
"The Metamorphosis" => 3120,
"To Kill a Mockingbird" => 22_800,
"Pride and Prejudice" => 25_200,
"1984" => 16_800,
"The Lord of the Rings" => 108_000,
"Harry Potter and the Sorcerer's Stone" => 24_600,
"The Great Gatsby" => 12_600,
"The Little Prince" => 5400,
"Animal Farm" => 7200,
"The Catcher in the Rye" => 18_000,
"Jane Eyre" => 34_200,
"Fahrenheit 451" => 15_000,
"The Hobbit" => 27_000,
"The Da Vinci Code" => 37_800,
"Little Women" => 30_000,
"One Hundred Years of Solitude" => 46_800,
"And Then There Were None" => 16_200,
"The Alchemist" => 10_800,
"The Hitchhiker's Guide to the Galaxy" => 12_600,
}
end
def best_book_fit(reading_time)
reading_time_rest = reading_time
books = []
while reading_time_rest > 0
books << popular_book_reading_time.min_by { |_, v| (v - reading_time_rest).abs }.first
reading_time_rest -= popular_book_reading_time[books.last]
end
books.group_by(&:itself).transform_values(&:count)
end
end
end

View File

@ -23,7 +23,10 @@ module DiscourseRewind
# @return [Service::Base::Context] # @return [Service::Base::Context]
# order matters, rewinds are displayed in the order they are defined # order matters, rewinds are displayed in the order they are defined
REPORTS = [DiscourseRewind::Rewind::Action::CreatePostsCountReport] REPORTS = [
DiscourseRewind::Rewind::Action::ReadingTime,
DiscourseRewind::Rewind::Action::PostingCalendar,
].freeze
CACHE_DURATION = 5.minutes CACHE_DURATION = 5.minutes