discourse/app/models/stat.rb
Vinoth Kannan b3238bfc34
FEATURE: call hub API to update Discourse discover enrollment. (#25634)
Now forums can enroll their sites to be showcased in the Discourse [Discover](https://discourse.org/discover) directory. Once they enable the site setting `include_in_discourse_discover` to enroll their forum the `CallDiscourseHub` job will ping the `api.discourse.org/api/discover/enroll` endpoint. Then the Discourse Hub will fetch the basic details from the forum and add it to the review queue. If the site is approved then the forum details will be displayed in the `/discover` page.
2024-02-23 11:42:28 +05:30

65 lines
1.6 KiB
Ruby

# frozen_string_literal: true
class Stat
def initialize(name, show_in_ui: false, expose_via_api: false, &block)
@name = name
@show_in_ui = show_in_ui
@expose_via_api = expose_via_api
@block = block
end
attr_reader :name, :expose_via_api, :show_in_ui
def calculate
@block.call.transform_keys { |key| build_key(key) }
rescue StandardError => err
Discourse.warn_exception(err, message: "Unexpected error when collecting #{@name} About stats.")
{}
end
def self.all_stats
calculate(_all_stats)
end
def self.api_stats
calculate(_api_stats)
end
private
def build_key(key)
"#{@name}_#{key}".to_sym
end
def self._all_stats
core_stats.concat(plugin_stats)
end
def self.calculate(stats)
stats.map { |stat| stat.calculate }.reduce(Hash.new, :merge)
end
def self.core_stats
[
Stat.new("topics", show_in_ui: true, expose_via_api: true) { Statistics.topics },
Stat.new("posts", show_in_ui: true, expose_via_api: true) { Statistics.posts },
Stat.new("users", show_in_ui: true, expose_via_api: true) { Statistics.users },
Stat.new("active_users", show_in_ui: true, expose_via_api: true) { Statistics.active_users },
Stat.new("likes", show_in_ui: true, expose_via_api: true) { Statistics.likes },
Stat.new("discourse_discover", show_in_ui: false, expose_via_api: true) do
Statistics.discourse_discover
end,
]
end
def self._api_stats
_all_stats.select { |stat| stat.expose_via_api }
end
def self.plugin_stats
DiscoursePluginRegistry.stats
end
private_class_method :_all_stats, :calculate, :core_stats, :_api_stats, :plugin_stats
end