2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe About do
|
2015-07-07 00:52:19 -04:00
|
|
|
|
|
|
|
describe 'stats cache' do
|
2021-05-20 21:43:47 -04:00
|
|
|
include_examples 'stats cacheable'
|
2015-07-07 00:52:19 -04:00
|
|
|
end
|
|
|
|
|
FEATURE: Add plugin API to register About stat group (#17442)
This commit introduces a new plugin API to register
a group of stats that will be included in about.json
and also conditionally in the site about UI at /about.
The usage is like this:
```ruby
register_about_stat_group("chat_messages", show_in_ui: true) do
{
last_day: 1,
"7_days" => 10,
"30_days" => 100,
count: 1000,
previous_30_days: 120
}
end
```
In reality the stats will be generated any way the implementer
chooses within the plugin. The `last_day`, `7_days`, `30_days,` and `count`
keys must be present but apart from that additional stats may be added.
Only those core 4 stat keys will be shown in the UI, but everything will be shown
in about.json.
The stat group name is used to prefix the stats in about.json like so:
```json
"chat_messages_last_day": 2322,
"chat_messages_7_days": 2322,
"chat_messages_30_days": 2322,
"chat_messages_count": 2322,
```
The `show_in_ui` option (default false) is used to determine whether the
group of stats is shown on the site About page in the Site Statistics
table. Some stats may be needed purely for reporting purposes and thus
do not need to be shown in the UI to admins/users. An extension to the Site
serializer, `displayed_about_plugin_stat_groups`, has been added so this
can be inspected on the client-side.
2022-07-14 23:16:00 -04:00
|
|
|
describe "#stats" do
|
|
|
|
after do
|
|
|
|
About.clear_plugin_stat_groups
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds plugin stats to the output" do
|
|
|
|
stats = { last_day: 1, "7_days" => 10, "30_days" => 100, count: 1000 }
|
|
|
|
About.add_plugin_stat_group("some_group", show_in_ui: true) do
|
|
|
|
stats
|
|
|
|
end
|
|
|
|
expect(described_class.new.stats.with_indifferent_access).to match(
|
|
|
|
hash_including(
|
|
|
|
some_group_last_day: 1,
|
|
|
|
some_group_7_days: 10,
|
|
|
|
some_group_30_days: 100,
|
|
|
|
some_group_count: 1000,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not add plugin stats to the output if they are missing one of the required keys" do
|
|
|
|
stats = { "7_days" => 10, "30_days" => 100, count: 1000 }
|
|
|
|
About.add_plugin_stat_group("some_group", show_in_ui: true) do
|
|
|
|
stats
|
|
|
|
end
|
|
|
|
expect(described_class.new.stats).not_to match(
|
|
|
|
hash_including(
|
|
|
|
some_group_last_day: 1,
|
|
|
|
some_group_7_days: 10,
|
|
|
|
some_group_30_days: 100,
|
|
|
|
some_group_count: 1000,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not error if any of the plugin stat blocks throw an error and still adds the non-errored stats to output" do
|
|
|
|
stats = { last_day: 1, "7_days" => 10, "30_days" => 100, count: 1000 }
|
|
|
|
About.add_plugin_stat_group("some_group", show_in_ui: true) do
|
|
|
|
stats
|
|
|
|
end
|
|
|
|
About.add_plugin_stat_group("other_group", show_in_ui: true) do
|
|
|
|
raise StandardError
|
|
|
|
end
|
|
|
|
expect(described_class.new.stats.with_indifferent_access).to match(
|
|
|
|
hash_including(
|
|
|
|
some_group_last_day: 1,
|
|
|
|
some_group_7_days: 10,
|
|
|
|
some_group_30_days: 100,
|
|
|
|
some_group_count: 1000,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
expect { described_class.new.stats.with_indifferent_access }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow duplicate displayed stat groups" do
|
|
|
|
stats = { last_day: 1, "7_days" => 10, "30_days" => 100, count: 1000 }
|
|
|
|
About.add_plugin_stat_group("some_group", show_in_ui: true) do
|
|
|
|
stats
|
|
|
|
end
|
|
|
|
About.add_plugin_stat_group("some_group", show_in_ui: true) do
|
|
|
|
stats
|
|
|
|
end
|
|
|
|
expect(described_class.displayed_plugin_stat_groups).to eq(["some_group"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-31 09:46:58 -04:00
|
|
|
describe "#category_moderators" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
2019-10-03 14:48:56 -04:00
|
|
|
let(:public_cat_moderator) { Fabricate(:user, last_seen_at: 1.month.ago) }
|
|
|
|
let(:private_cat_moderator) { Fabricate(:user, last_seen_at: 2.month.ago) }
|
|
|
|
let(:common_moderator) { Fabricate(:user, last_seen_at: 3.month.ago) }
|
|
|
|
let(:common_moderator_2) { Fabricate(:user, last_seen_at: 4.month.ago) }
|
2019-07-31 09:46:58 -04:00
|
|
|
|
|
|
|
let(:public_group) do
|
|
|
|
group = Fabricate(:public_group)
|
|
|
|
group.add(public_cat_moderator)
|
|
|
|
group.add(common_moderator)
|
2019-10-03 14:48:56 -04:00
|
|
|
group.add(common_moderator_2)
|
2019-07-31 09:46:58 -04:00
|
|
|
group
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:private_group) do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
group.add(private_cat_moderator)
|
|
|
|
group.add(common_moderator)
|
2019-10-03 14:48:56 -04:00
|
|
|
group.add(common_moderator_2)
|
2019-07-31 09:46:58 -04:00
|
|
|
group
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:public_cat) { Fabricate(:category, reviewable_by_group: public_group) }
|
|
|
|
let!(:private_cat) { Fabricate(:private_category, group: private_group, reviewable_by_group: private_group) }
|
|
|
|
|
|
|
|
it "lists moderators of the category that the current user can see" do
|
|
|
|
results = About.new(private_cat_moderator).category_moderators
|
|
|
|
expect(results.map(&:category_id)).to contain_exactly(public_cat.id, private_cat.id)
|
|
|
|
expect(results.map(&:moderators).flatten.map(&:id).uniq).to contain_exactly(
|
|
|
|
public_cat_moderator.id,
|
|
|
|
common_moderator.id,
|
2019-10-03 14:48:56 -04:00
|
|
|
common_moderator_2.id,
|
2019-07-31 09:46:58 -04:00
|
|
|
private_cat_moderator.id
|
|
|
|
)
|
|
|
|
|
|
|
|
[public_cat_moderator, user, nil].each do |u|
|
|
|
|
results = About.new(u).category_moderators
|
|
|
|
expect(results.map(&:category_id)).to contain_exactly(public_cat.id)
|
2019-10-03 14:48:56 -04:00
|
|
|
expect(results.map(&:moderators).flatten.map(&:id)).to eq([
|
2019-07-31 09:46:58 -04:00
|
|
|
public_cat_moderator.id,
|
2019-10-03 14:48:56 -04:00
|
|
|
common_moderator.id,
|
|
|
|
common_moderator_2.id
|
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "limit category moderators when there are too many for perf reasons" do
|
|
|
|
about = About.new(private_cat_moderator)
|
|
|
|
about.category_mods_limit = 4
|
|
|
|
results = about.category_moderators
|
|
|
|
expect(results.size).to eq(2)
|
|
|
|
results.each do |res|
|
|
|
|
expect(res.moderators.size).to eq(2)
|
2019-07-31 09:46:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-07-07 00:52:19 -04:00
|
|
|
end
|