2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-03-19 23:18:00 -04:00
|
|
|
class AdminDashboardData
|
2015-07-07 00:52:19 -04:00
|
|
|
include StatsCacheable
|
2013-03-19 23:18:00 -04:00
|
|
|
|
2024-05-22 21:29:08 -04:00
|
|
|
cattr_reader :problem_messages, default: []
|
2021-12-19 18:59:11 -05:00
|
|
|
|
2019-04-01 08:35:09 -04:00
|
|
|
# kept for backward compatibility
|
2024-10-15 22:09:07 -04:00
|
|
|
GLOBAL_REPORTS = []
|
2019-04-01 08:35:09 -04:00
|
|
|
|
2021-12-19 18:59:11 -05:00
|
|
|
PROBLEM_MESSAGE_PREFIX = "admin-problem:"
|
2023-11-02 21:05:29 -04:00
|
|
|
SCHEDULED_PROBLEM_STORAGE_KEY = "admin-found-scheduled-problems-list"
|
2021-12-19 18:59:11 -05:00
|
|
|
|
2019-04-01 06:39:49 -04:00
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_json
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_json(_options = nil)
|
|
|
|
@json ||= get_json
|
|
|
|
end
|
|
|
|
|
2013-04-25 17:53:31 -04:00
|
|
|
def problems
|
2015-08-25 20:07:40 -04:00
|
|
|
problems = []
|
2021-12-19 18:59:11 -05:00
|
|
|
self.class.problem_messages.each do |i18n_key|
|
|
|
|
message = self.class.problem_message_check(i18n_key)
|
2024-03-13 22:55:01 -04:00
|
|
|
problems << ProblemCheck::Problem.new(message) if message.present?
|
2016-04-05 14:42:24 -04:00
|
|
|
end
|
2024-03-13 22:55:01 -04:00
|
|
|
problems.concat(ProblemCheck.realtime.flat_map { |c| c.call(@opts).map(&:to_h) })
|
|
|
|
|
2016-04-08 16:44:04 -04:00
|
|
|
problems.compact!
|
|
|
|
|
|
|
|
if problems.empty?
|
|
|
|
self.class.clear_problems_started
|
|
|
|
else
|
|
|
|
self.class.set_problems_started
|
|
|
|
end
|
|
|
|
|
|
|
|
problems
|
|
|
|
end
|
|
|
|
|
2021-12-19 18:59:11 -05:00
|
|
|
def self.add_problem_check(*syms, &blk)
|
2024-03-19 20:52:25 -04:00
|
|
|
Discourse.deprecate(
|
|
|
|
"`AdminDashboardData#add_problem_check` is deprecated. Implement a class that inherits `ProblemCheck` instead.",
|
|
|
|
drop_from: "3.3",
|
|
|
|
)
|
2016-04-08 16:44:04 -04:00
|
|
|
end
|
|
|
|
|
2021-12-19 18:59:11 -05:00
|
|
|
##
|
|
|
|
# We call this method in the class definition below
|
|
|
|
# so all of the problem checks in this class are registered on
|
|
|
|
# boot. These problem checks are run when the problems are loaded in
|
|
|
|
# the admin dashboard controller.
|
|
|
|
#
|
|
|
|
# This method also can be used in testing to reset checks between
|
|
|
|
# tests. It will also fire multiple times in development mode because
|
|
|
|
# classes are not cached.
|
2015-09-01 16:32:35 -04:00
|
|
|
def self.reset_problem_checks
|
2024-05-22 21:29:08 -04:00
|
|
|
@@problem_messages = []
|
2013-04-25 17:53:31 -04:00
|
|
|
end
|
2014-05-21 18:19:40 -04:00
|
|
|
|
2021-12-19 18:59:11 -05:00
|
|
|
def self.fetch_stats
|
|
|
|
new.as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reports(source)
|
|
|
|
source.map { |type| Report.find(type).as_json }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stats_cache_key
|
|
|
|
"dashboard-data-#{Report::SCHEMA_VERSION}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.problems_started_key
|
|
|
|
"dash-problems-started-at"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_problems_started
|
|
|
|
existing_time = Discourse.redis.get(problems_started_key)
|
|
|
|
Discourse.redis.setex(problems_started_key, 14.days.to_i, existing_time || Time.zone.now.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_problems_started
|
|
|
|
Discourse.redis.del problems_started_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.problems_started_at
|
|
|
|
s = Discourse.redis.get(problems_started_key)
|
|
|
|
s ? Time.zone.parse(s) : nil
|
|
|
|
end
|
|
|
|
|
2018-01-11 01:46:10 -05:00
|
|
|
def self.fetch_problems(opts = {})
|
2021-12-19 18:59:11 -05:00
|
|
|
new(opts).problems
|
2013-03-29 15:48:26 -04:00
|
|
|
end
|
|
|
|
|
2016-04-05 14:42:24 -04:00
|
|
|
def self.problem_message_check(i18n_key)
|
2019-12-03 04:05:53 -05:00
|
|
|
if Discourse.redis.get(problem_message_key(i18n_key))
|
|
|
|
I18n.t(i18n_key, base_path: Discourse.base_path)
|
2023-01-09 07:20:10 -05:00
|
|
|
else
|
2019-12-03 04:05:53 -05:00
|
|
|
nil
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2016-04-05 14:42:24 -04:00
|
|
|
end
|
|
|
|
|
2021-12-19 18:59:11 -05:00
|
|
|
##
|
|
|
|
# Arbitrary messages cannot be added here, they must already be defined
|
|
|
|
# in the @problem_messages array which is defined in reset_problem_checks.
|
|
|
|
# The array is iterated over and each key that exists in redis will be added
|
|
|
|
# to the final problems output in #problems.
|
2016-04-05 14:42:24 -04:00
|
|
|
def self.add_problem_message(i18n_key, expire_seconds = nil)
|
|
|
|
if expire_seconds.to_i > 0
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.setex problem_message_key(i18n_key), expire_seconds.to_i, 1
|
2016-04-05 14:42:24 -04:00
|
|
|
else
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.set problem_message_key(i18n_key), 1
|
2016-04-05 14:42:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_problem_message(i18n_key)
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.del problem_message_key(i18n_key)
|
2016-04-05 14:42:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.problem_message_key(i18n_key)
|
2021-12-19 18:59:11 -05:00
|
|
|
"#{PROBLEM_MESSAGE_PREFIX}#{i18n_key}"
|
2016-04-05 14:42:24 -04:00
|
|
|
end
|
2013-06-19 16:11:11 -04:00
|
|
|
end
|