2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe About do
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "stats cache" do
|
|
|
|
include_examples "stats cacheable"
|
2015-07-06 23:52:19 -05:00
|
|
|
end
|
|
|
|
|
2023-11-09 14:44:05 -06:00
|
|
|
def register_stat(name, stats_block)
|
|
|
|
DiscoursePluginRegistry.register_stat(
|
|
|
|
Stat.new(name, show_in_ui: true, expose_via_api: true, &stats_block),
|
2023-03-01 16:10:16 -06:00
|
|
|
stub(enabled?: true),
|
|
|
|
)
|
|
|
|
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 22:16:00 -05:00
|
|
|
|
2023-03-01 16:10:16 -06:00
|
|
|
after { DiscoursePluginRegistry.reset! }
|
|
|
|
|
|
|
|
describe "#stats" do
|
2024-08-29 05:16:57 -05:00
|
|
|
use_redis_snapshotting
|
|
|
|
|
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 22:16:00 -05:00
|
|
|
it "adds plugin stats to the output" do
|
2023-01-09 05:18:21 -06:00
|
|
|
stats = { :last_day => 1, "7_days" => 10, "30_days" => 100, :count => 1000 }
|
2023-11-09 14:44:05 -06:00
|
|
|
register_stat("some_group", Proc.new { stats })
|
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 22:16:00 -05:00
|
|
|
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,
|
2023-01-09 05:18:21 -06:00
|
|
|
),
|
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 22:16:00 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-08-29 05:16:57 -05:00
|
|
|
it "uses the cache" do
|
|
|
|
cold_cache_count = track_sql_queries { described_class.new.stats }.count
|
|
|
|
hot_cache_count = track_sql_queries { described_class.new.stats }.count
|
|
|
|
|
|
|
|
expect(cold_cache_count + hot_cache_count).to eq(cold_cache_count)
|
|
|
|
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 22:16:00 -05:00
|
|
|
it "does not add plugin stats to the output if they are missing one of the required keys" do
|
2023-01-09 05:18:21 -06:00
|
|
|
stats = { "7_days" => 10, "30_days" => 100, :count => 1000 }
|
2023-11-09 14:44:05 -06:00
|
|
|
register_stat("some_group", Proc.new { stats })
|
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 22:16:00 -05:00
|
|
|
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,
|
2023-01-09 05:18:21 -06:00
|
|
|
),
|
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 22:16:00 -05:00
|
|
|
)
|
|
|
|
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
|
2023-01-09 05:18:21 -06:00
|
|
|
stats = { :last_day => 1, "7_days" => 10, "30_days" => 100, :count => 1000 }
|
2023-11-09 14:44:05 -06:00
|
|
|
register_stat("some_group", Proc.new { stats })
|
|
|
|
register_stat("other_group", Proc.new { raise StandardError })
|
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 22:16:00 -05:00
|
|
|
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,
|
2023-01-09 05:18:21 -06:00
|
|
|
),
|
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 22:16:00 -05:00
|
|
|
)
|
|
|
|
expect { described_class.new.stats.with_indifferent_access }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-31 08:46:58 -05:00
|
|
|
describe "#category_moderators" do
|
2024-09-03 20:38:46 -05:00
|
|
|
fab!(:user)
|
|
|
|
fab!(:public_cat_moderator) { Fabricate(:user, last_seen_at: 1.month.ago) }
|
|
|
|
fab!(:private_cat_moderator) { Fabricate(:user, last_seen_at: 2.month.ago) }
|
|
|
|
fab!(:common_moderator) { Fabricate(:user, last_seen_at: 3.month.ago) }
|
|
|
|
fab!(:common_moderator_2) { Fabricate(:user, last_seen_at: 4.month.ago) }
|
|
|
|
|
|
|
|
fab!(:public_group) do
|
|
|
|
Fabricate(:public_group, users: [public_cat_moderator, common_moderator, common_moderator_2])
|
2019-07-31 08:46:58 -05:00
|
|
|
end
|
|
|
|
|
2024-09-03 20:38:46 -05:00
|
|
|
fab!(:private_group) do
|
|
|
|
Fabricate(:group, users: [private_cat_moderator, common_moderator, common_moderator_2])
|
2019-07-31 08:46:58 -05:00
|
|
|
end
|
|
|
|
|
2024-09-03 20:38:46 -05:00
|
|
|
fab!(:public_cat) { Fabricate(:category) }
|
|
|
|
fab!(:public_category_moderation_group) do
|
|
|
|
Fabricate(:category_moderation_group, category: public_cat, group: public_group)
|
|
|
|
end
|
|
|
|
|
|
|
|
fab!(:private_cat) { Fabricate(:private_category, group: private_group) }
|
|
|
|
fab!(:private_category_moderation_group) do
|
|
|
|
Fabricate(:category_moderation_group, category: private_cat, group: private_group)
|
2023-01-09 05:18:21 -06:00
|
|
|
end
|
2019-07-31 08:46:58 -05:00
|
|
|
|
|
|
|
it "lists moderators of the category that the current user can see" do
|
|
|
|
results = About.new(private_cat_moderator).category_moderators
|
2024-04-05 01:38:54 -05:00
|
|
|
expect(results.map(&:category).map(&:id)).to contain_exactly(public_cat.id, private_cat.id)
|
2019-07-31 08:46:58 -05:00
|
|
|
expect(results.map(&:moderators).flatten.map(&:id).uniq).to contain_exactly(
|
|
|
|
public_cat_moderator.id,
|
|
|
|
common_moderator.id,
|
2019-10-03 13:48:56 -05:00
|
|
|
common_moderator_2.id,
|
2023-01-09 05:18:21 -06:00
|
|
|
private_cat_moderator.id,
|
2019-07-31 08:46:58 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
[public_cat_moderator, user, nil].each do |u|
|
|
|
|
results = About.new(u).category_moderators
|
2024-04-05 01:38:54 -05:00
|
|
|
expect(results.map(&:category).map(&:id)).to contain_exactly(public_cat.id)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(results.map(&:moderators).flatten.map(&:id)).to eq(
|
|
|
|
[public_cat_moderator.id, common_moderator.id, common_moderator_2.id],
|
|
|
|
)
|
2019-10-03 13:48:56 -05:00
|
|
|
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)
|
2023-01-09 05:18:21 -06:00
|
|
|
results.each { |res| expect(res.moderators.size).to eq(2) }
|
2019-07-31 08:46:58 -05:00
|
|
|
end
|
2024-09-03 20:38:46 -05:00
|
|
|
|
|
|
|
it "doesn't list the same user twice as a category mod if the user is member of multiple groups" do
|
|
|
|
Fabricate(:category_moderation_group, category: public_cat, group: private_group)
|
|
|
|
|
|
|
|
results = About.new(nil).category_moderators
|
|
|
|
mods = results.find { |r| r.category.id == public_cat.id }.moderators
|
|
|
|
expect(mods.map(&:id)).to contain_exactly(
|
|
|
|
public_cat_moderator.id,
|
|
|
|
common_moderator.id,
|
|
|
|
common_moderator_2.id,
|
|
|
|
private_cat_moderator.id,
|
|
|
|
)
|
|
|
|
end
|
2019-07-31 08:46:58 -05:00
|
|
|
end
|
2024-05-30 10:44:53 -05:00
|
|
|
|
|
|
|
describe "#admins" do
|
|
|
|
fab!(:admin_mark) { Fabricate(:admin, name: "mark") }
|
|
|
|
fab!(:admin_matt) { Fabricate(:admin, name: "matt") }
|
|
|
|
fab!(:admin_kate) { Fabricate(:admin, name: "kate") }
|
|
|
|
|
|
|
|
context "with about_admins plugin modifier registered" do
|
|
|
|
let(:modifier_block) { Proc.new { |admins| admins.select { |admin| admin.name != "mark" } } }
|
|
|
|
|
|
|
|
it "Applies registered modifiers (filtering out admins in this case)" do
|
|
|
|
plugin_instance = Plugin::Instance.new
|
|
|
|
plugin_instance.register_modifier(:about_admins, &modifier_block)
|
|
|
|
expect(About.new(Fabricate(:user)).admins).to match_array([admin_matt, admin_kate])
|
|
|
|
ensure
|
|
|
|
DiscoursePluginRegistry.unregister_modifier(plugin_instance, :about_admins, &modifier_block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-07-06 23:52:19 -05:00
|
|
|
end
|