2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-08-12 04:59:00 +08:00
|
|
|
class About
|
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-15 11:16:00 +08:00
|
|
|
def self.displayed_plugin_stat_groups
|
2023-03-02 06:10:16 +08:00
|
|
|
DiscoursePluginRegistry
|
|
|
|
.about_stat_groups
|
|
|
|
.select { |stat_group| stat_group[:show_in_ui] }
|
|
|
|
.map { |stat_group| stat_group[:name] }
|
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-15 11:16:00 +08:00
|
|
|
end
|
|
|
|
|
2019-07-31 21:46:58 +08:00
|
|
|
class CategoryMods
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
attr_reader :category_id, :moderators
|
|
|
|
|
|
|
|
def initialize(category_id, moderators)
|
|
|
|
@category_id = category_id
|
|
|
|
@moderators = moderators
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-12 04:59:00 +08:00
|
|
|
include ActiveModel::Serialization
|
2015-07-07 12:52:19 +08:00
|
|
|
include StatsCacheable
|
2014-08-12 04:59:00 +08:00
|
|
|
|
|
|
|
attr_accessor :moderators, :admins
|
|
|
|
|
2015-07-07 12:52:19 +08:00
|
|
|
def self.stats_cache_key
|
|
|
|
"about-stats"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.fetch_stats
|
|
|
|
About.new.stats
|
|
|
|
end
|
|
|
|
|
2019-07-31 21:46:58 +08:00
|
|
|
def initialize(user = nil)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
2014-08-12 06:15:35 +08:00
|
|
|
def version
|
|
|
|
Discourse::VERSION::STRING
|
|
|
|
end
|
|
|
|
|
2014-11-24 14:54:17 +08:00
|
|
|
def https
|
2016-06-27 17:26:43 +08:00
|
|
|
SiteSetting.force_https
|
2014-11-24 14:54:17 +08:00
|
|
|
end
|
|
|
|
|
2014-08-12 06:15:35 +08:00
|
|
|
def title
|
|
|
|
SiteSetting.title
|
|
|
|
end
|
|
|
|
|
|
|
|
def locale
|
|
|
|
SiteSetting.default_locale
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
SiteSetting.site_description
|
|
|
|
end
|
|
|
|
|
2014-08-12 04:59:00 +08:00
|
|
|
def moderators
|
2014-08-21 00:42:33 +08:00
|
|
|
@moderators ||= User.where(moderator: true, admin: false).human_users.order("last_seen_at DESC")
|
2014-08-12 04:59:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def admins
|
2019-01-15 19:55:22 +08:00
|
|
|
@admins ||= User.where(admin: true).human_users.order("last_seen_at DESC")
|
2014-08-12 04:59:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def stats
|
|
|
|
@stats ||= {
|
2021-04-12 10:50:33 +08:00
|
|
|
topic_count: Topic.listable_topics.count,
|
|
|
|
topics_last_day: Topic.listable_topics.where("created_at > ?", 1.days.ago).count,
|
|
|
|
topics_7_days: Topic.listable_topics.where("created_at > ?", 7.days.ago).count,
|
|
|
|
topics_30_days: Topic.listable_topics.where("created_at > ?", 30.days.ago).count,
|
|
|
|
post_count: Post.count,
|
|
|
|
posts_last_day: Post.where("created_at > ?", 1.days.ago).count,
|
|
|
|
posts_7_days: Post.where("created_at > ?", 7.days.ago).count,
|
|
|
|
posts_30_days: Post.where("created_at > ?", 30.days.ago).count,
|
|
|
|
user_count: User.real.count,
|
|
|
|
users_last_day: User.real.where("created_at > ?", 1.days.ago).count,
|
|
|
|
users_7_days: User.real.where("created_at > ?", 7.days.ago).count,
|
|
|
|
users_30_days: User.real.where("created_at > ?", 30.days.ago).count,
|
|
|
|
active_users_last_day: User.where("last_seen_at > ?", 1.days.ago).count,
|
|
|
|
active_users_7_days: User.where("last_seen_at > ?", 7.days.ago).count,
|
|
|
|
active_users_30_days: User.where("last_seen_at > ?", 30.days.ago).count,
|
|
|
|
like_count: UserAction.where(action_type: UserAction::LIKE).count,
|
|
|
|
likes_last_day:
|
|
|
|
UserAction.where(action_type: UserAction::LIKE).where("created_at > ?", 1.days.ago).count,
|
|
|
|
likes_7_days:
|
|
|
|
UserAction.where(action_type: UserAction::LIKE).where("created_at > ?", 7.days.ago).count,
|
|
|
|
likes_30_days:
|
|
|
|
UserAction.where(action_type: UserAction::LIKE).where("created_at > ?", 30.days.ago).count,
|
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-15 11:16:00 +08:00
|
|
|
}.merge(plugin_stats)
|
|
|
|
end
|
|
|
|
|
|
|
|
def plugin_stats
|
|
|
|
final_plugin_stats = {}
|
2023-03-02 06:10:16 +08:00
|
|
|
DiscoursePluginRegistry.about_stat_groups.each do |stat_group|
|
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-15 11:16:00 +08:00
|
|
|
begin
|
2023-03-02 06:10:16 +08:00
|
|
|
stats = stat_group[:block].call
|
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-15 11:16:00 +08:00
|
|
|
rescue StandardError => err
|
|
|
|
Discourse.warn_exception(
|
|
|
|
err,
|
2023-03-02 06:10:16 +08:00
|
|
|
message: "Unexpected error when collecting #{stat_group[:name]} About 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-15 11:16:00 +08:00
|
|
|
)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if !stats.key?(:last_day) || !stats.key?("7_days") || !stats.key?("30_days") ||
|
|
|
|
!stats.key?(:count)
|
|
|
|
Rails.logger.warn(
|
2023-03-02 06:10:16 +08:00
|
|
|
"Plugin stat group #{stat_group[:name]} for About stats does not have all required keys, skipping.",
|
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-15 11:16:00 +08:00
|
|
|
)
|
|
|
|
else
|
|
|
|
final_plugin_stats.merge!(
|
2023-03-02 06:10:16 +08:00
|
|
|
stats.transform_keys { |key| "#{stat_group[:name]}_#{key}".to_sym },
|
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-15 11:16:00 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
final_plugin_stats
|
2014-08-12 04:59:00 +08:00
|
|
|
end
|
|
|
|
|
2019-07-31 21:46:58 +08:00
|
|
|
def category_moderators
|
2019-10-04 02:48:56 +08:00
|
|
|
allowed_cats = Guardian.new(@user).allowed_category_ids
|
|
|
|
return [] if allowed_cats.blank?
|
2021-03-28 16:25:30 +08:00
|
|
|
|
2019-10-04 02:48:56 +08:00
|
|
|
cats_with_mods = Category.where.not(reviewable_by_group_id: nil).pluck(:id)
|
2021-03-28 16:25:30 +08:00
|
|
|
|
2019-10-04 02:48:56 +08:00
|
|
|
category_ids = cats_with_mods & allowed_cats
|
2019-07-31 21:46:58 +08:00
|
|
|
return [] if category_ids.blank?
|
2019-10-04 02:48:56 +08:00
|
|
|
|
|
|
|
per_cat_limit = category_mods_limit / category_ids.size
|
|
|
|
per_cat_limit = 1 if per_cat_limit < 1
|
2021-03-28 16:25:30 +08:00
|
|
|
|
|
|
|
results = DB.query(<<~SQL, category_ids: category_ids)
|
|
|
|
SELECT c.id category_id
|
|
|
|
, (ARRAY_AGG(u.id ORDER BY u.last_seen_at DESC))[:#{per_cat_limit}] user_ids
|
|
|
|
FROM categories c
|
|
|
|
JOIN group_users gu ON gu.group_id = c.reviewable_by_group_id
|
|
|
|
JOIN users u ON u.id = gu.user_id
|
|
|
|
WHERE c.id IN (:category_ids)
|
|
|
|
GROUP BY c.id
|
|
|
|
ORDER BY c.position
|
2019-07-31 21:46:58 +08:00
|
|
|
SQL
|
2021-03-28 16:25:30 +08:00
|
|
|
|
2021-03-31 05:12:53 +08:00
|
|
|
mods = User.where(id: results.map(&:user_ids).flatten.uniq).index_by(&:id)
|
2021-03-28 16:25:30 +08:00
|
|
|
|
2021-03-31 05:12:53 +08:00
|
|
|
results.map { |row| CategoryMods.new(row.category_id, mods.values_at(*row.user_ids)) }
|
2019-07-31 21:46:58 +08:00
|
|
|
end
|
2019-10-04 02:48:56 +08:00
|
|
|
|
|
|
|
def category_mods_limit
|
|
|
|
@category_mods_limit || 100
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_mods_limit=(number)
|
|
|
|
@category_mods_limit = number
|
|
|
|
end
|
2014-08-12 04:59:00 +08:00
|
|
|
end
|