More reports

This commit is contained in:
Rafael Silva 2024-12-19 15:31:12 -03:00
parent 1ce447e37f
commit bc80558e2d
No known key found for this signature in database
6 changed files with 116 additions and 20 deletions

View File

@ -0,0 +1,31 @@
# 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::ActivityCalendar < Rewind::Action::BaseReport
def call
calendar =
Post
.unscoped
.joins(<<~SQL)
RIGHT JOIN
generate_series('#{date.first}', '#{date.last}', '1 day'::interval) ON
posts.created_at::date = generate_series::date AND
posts.user_id = #{user.id} AND
posts.deleted_at IS NULL
SQL
.joins(
"LEFT JOIN user_visits ON generate_series::date = visited_at AND user_visits.user_id = #{user.id}",
)
.select(
"generate_series::date as date, count(posts.id) as post_count, COUNT(visited_at) > 0 as visited",
)
.group("generate_series, user_visits.id")
.order("generate_series")
.map { |row| { date: row.date, post_count: row.post_count, visited: row.visited } }
{ data: calendar, identifier: "activity-calendar" }
end
end
end

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
module DiscourseRewind
class Rewind::Action::BestPosts < Rewind::Action::BaseReport
def call
best_posts =
Post
.where(user_id: user.id)
.where(created_at: date)
.where(deleted_at: nil)
.where("post_number > 1")
.order("like_count DESC NULLS LAST")
.limit(5)
.pluck(:id, :topic_id, :like_count, :reply_count, :raw, :cooked)
{ data: best_posts, identifier: "best-posts" }
end
end
end

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
module DiscourseRewind
class Rewind::Action::BestTopics < Rewind::Action::BaseReport
def call
best_topics =
TopTopic
.includes(:topic)
.references(:topic)
.where(topic: { deleted_at: nil, created_at: date, user_id: user.id })
.order("yearly_score DESC NULLS LAST")
.limit(5)
.pluck(:topic_id, :title, :excerpt, :yearly_score)
.map do |topic_id, title, excerpt, yearly_score|
{ topic_id: topic_id, title: title, excerpt: excerpt, yearly_score: yearly_score }
end
{ data: best_topics, identifier: "best-topics" }
end
end
end

View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
# Topics visited grouped by category
module DiscourseRewind
class Rewind::Action::FavoriteCategories < Rewind::Action::BaseReport
def call
favorite_categories =
TopicViewItem
.joins(:topic)
.joins("INNER JOIN categories ON categories.id = topics.category_id")
.where(user: user)
.where(viewed_at: date)
.group("categories.id, categories.name")
.order("COUNT(*) DESC")
.limit(5)
.pluck("categories.id, categories.name")
.map { |category_id, name| { category_id: category_id, name: name } }
{ data: favorite_categories, identifier: "favorite-categories" }
end
end
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
# Topics visited grouped by tag
module DiscourseRewind
class Rewind::Action::FavoriteTags < Rewind::Action::BaseReport
def call
favorite_tags =
TopicViewItem
.joins(:topic)
.joins("INNER JOIN topic_tags ON topic_tags.topic_id = topics.id")
.joins("INNER JOIN tags ON tags.id = topic_tags.tag_id")
.where(user: user)
.where(viewed_at: date)
.group("tags.id, tags.name")
.order("COUNT(DISTINCT topic_views.topic_id) DESC")
.limit(5)
.pluck("tags.id, tags.name")
.map { |tag_id, name| { tag_id: tag_id, name: name } }
{ data: favorite_tags, identifier: "favorite-tags" }
end
end
end

View File

@ -1,20 +0,0 @@
# 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 < Rewind::Action::BaseReport
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