From e5f3c26d20ae9195684056c4f83c8d96d33d8e7c Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Tue, 5 Sep 2023 11:17:18 +0530 Subject: [PATCH] FEATURE: add group filter for admin reports (#23381) FEATURE: add group filter for admin reports DEV: add plugin outlet for admin dashboard tabs --- .../admin/addon/templates/dashboard.hbs | 2 + .../addon/templates/dashboard_general.hbs | 1 + app/models/post.rb | 10 +++- app/models/post_action.rb | 9 ++++ app/models/topic.rb | 12 ++++- spec/models/post_action_spec.rb | 34 +++++++++++++ spec/models/post_spec.rb | 48 +++++++++++++++++++ spec/models/topic_spec.rb | 10 ++++ 8 files changed, 124 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/admin/addon/templates/dashboard.hbs b/app/assets/javascripts/admin/addon/templates/dashboard.hbs index 19f614fc360..4f612dcd82d 100644 --- a/app/assets/javascripts/admin/addon/templates/dashboard.hbs +++ b/app/assets/javascripts/admin/addon/templates/dashboard.hbs @@ -49,6 +49,8 @@ {{/if}} + + {{outlet}} diff --git a/app/assets/javascripts/admin/addon/templates/dashboard_general.hbs b/app/assets/javascripts/admin/addon/templates/dashboard_general.hbs index 4b8bc39d995..1294dda7a65 100644 --- a/app/assets/javascripts/admin/addon/templates/dashboard_general.hbs +++ b/app/assets/javascripts/admin/addon/templates/dashboard_general.hbs @@ -199,6 +199,7 @@ \ No newline at end of file diff --git a/app/models/post.rb b/app/models/post.rb index 31a9015fe0e..d0068ac25be 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -868,7 +868,8 @@ class Post < ActiveRecord::Base start_date, end_date, category_id = nil, - include_subcategories = false + include_subcategories = false, + group_ids = nil ) result = public_posts.where( @@ -884,6 +885,13 @@ class Post < ActiveRecord::Base result = result.where("topics.category_id = ?", category_id) end end + if group_ids + result = + result + .joins("INNER JOIN users ON users.id = posts.user_id") + .joins("INNER JOIN group_users ON group_users.user_id = users.id") + .where("group_users.group_id IN (?)", group_ids) + end result.group("date(posts.created_at)").order("date(posts.created_at)").count end diff --git a/app/models/post_action.rb b/app/models/post_action.rb index d339316367e..45f2e3cbfd6 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -82,6 +82,15 @@ class PostAction < ActiveRecord::Base result = result.joins(post: :topic).where("topics.category_id = ?", opts[:category_id]) end end + + if opts[:group_ids] + result = + result + .joins("INNER JOIN users ON users.id = post_actions.user_id") + .joins("INNER JOIN group_users ON group_users.user_id = users.id") + .where("group_users.group_id IN (?)", opts[:group_ids]) + end + result.group("date(post_actions.created_at)").order("date(post_actions.created_at)").count end diff --git a/app/models/topic.rb b/app/models/topic.rb index 4995b995a21..7e4219e15dc 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -652,7 +652,8 @@ class Topic < ActiveRecord::Base start_date, end_date, category_id = nil, - include_subcategories = false + include_subcategories = false, + group_ids = nil ) result = listable_topics.where( @@ -665,6 +666,15 @@ class Topic < ActiveRecord::Base result.where( category_id: include_subcategories ? Category.subcategory_ids(category_id) : category_id, ) if category_id + + if group_ids + result = + result + .joins("INNER JOIN users ON users.id = topics.user_id") + .joins("INNER JOIN group_users ON group_users.user_id = users.id") + .where("group_users.group_id IN (?)", group_ids) + end + result.count end diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 5be13af4f28..d0aafcd4e97 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -987,4 +987,38 @@ RSpec.describe PostAction do end end end + + describe "count_per_day_for_type" do + before { PostActionCreator.create(eviltrout, post, :like) } + + it "returns the correct count" do + expect(PostAction.count_per_day_for_type(PostActionType.types[:like])).to eq( + Time.now.utc.to_date => 1, + ) + end + + it "returns the correct count when there are multiple actions" do + PostActionCreator.create(codinghorror, post, :like) + expect(PostAction.count_per_day_for_type(PostActionType.types[:like])).to eq( + Time.now.utc.to_date => 2, + ) + end + + it "returns the correct count when there are multiple types" do + PostActionCreator.create(eviltrout, post, :spam) + expect(PostAction.count_per_day_for_type(PostActionType.types[:spam])).to eq( + Time.now.utc.to_date => 1, + ) + end + + it "returns the correct count with group filter" do + group = Fabricate(:group) + group.add(codinghorror) + + PostActionCreator.create(codinghorror, post, :like) + expect( + PostAction.count_per_day_for_type(PostActionType.types[:like], { group_ids: [group.id] }), + ).to eq(Time.now.utc.to_date => 1) + end + end end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 09c78fa8d25..0d4fe09d689 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -2109,4 +2109,52 @@ RSpec.describe Post do expect(post4.canonical_url).to eq("#{topic_url}?page=2#post_#{post4.post_number}") end end + + describe "public_posts_count_per_day" do + before do + freeze_time DateTime.parse("2017-03-01 12:00") + + Fabricate(:post) + Fabricate(:post, created_at: 1.day.ago) + Fabricate(:post, created_at: 1.day.ago) + Fabricate(:post, created_at: 2.days.ago) + Fabricate(:post, created_at: 4.days.ago) + end + + let(:listable_topics_count_per_day) do + { 1.day.ago.to_date => 2, 2.days.ago.to_date => 1, Time.now.utc.to_date => 1 } + end + + it "collect closed interval public post count" do + expect(Post.public_posts_count_per_day(2.days.ago, Time.now)).to include( + listable_topics_count_per_day, + ) + expect(Post.public_posts_count_per_day(2.days.ago, Time.now)).not_to include( + 4.days.ago.to_date => 1, + ) + end + + it "returns the correct number of public posts per day when there are no public posts" do + Fabricate(:post, post_type: Post.types[:whisper], created_at: 6.days.ago) + Fabricate(:post, post_type: Post.types[:whisper], created_at: 7.days.ago) + + expect(Post.public_posts_count_per_day(10.days.ago, 5.days.ago)).to be_empty + end + + it "returns the correct number of public posts per day with group filter" do + user = Fabricate(:user) + group_user = Fabricate(:user) + group = Fabricate(:group) + group.add(group_user) + + Fabricate(:post, user: user, created_at: 6.days.ago) + Fabricate(:post, user: user, created_at: 7.days.ago) + Fabricate(:post, user: group_user, created_at: 6.days.ago) + Fabricate(:post, user: group_user, created_at: 7.days.ago) + + expect( + Post.public_posts_count_per_day(10.days.ago, 5.days.ago, nil, false, [group.id]), + ).to eq(6.days.ago.to_date => 1, 7.days.ago.to_date => 1) + end + end end diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 8d372a6bd48..e902ea49cc6 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -2573,6 +2573,16 @@ RSpec.describe Topic do 4.days.ago.to_date => 1, ) end + + it "returns the correct count with group filter" do + group = Fabricate(:group) + group.add(user) + topic = Fabricate(:topic, user: user) + + expect(Topic.listable_count_per_day(2.days.ago, Time.now, nil, false, [group.id])).to include( + Time.now.utc.to_date => 1, + ) + end end describe "#secure_category?" do