2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-03-19 22:18:00 -05:00
|
|
|
class AdminDashboardData
|
2015-07-06 23:52:19 -05:00
|
|
|
include StatsCacheable
|
2013-03-19 22:18:00 -05:00
|
|
|
|
2024-03-19 19:52:25 -05:00
|
|
|
cattr_reader :problem_messages
|
2021-12-19 17:59:11 -06:00
|
|
|
|
2019-04-01 07:35:09 -05:00
|
|
|
# kept for backward compatibility
|
|
|
|
GLOBAL_REPORTS ||= []
|
|
|
|
|
2021-12-19 17:59:11 -06:00
|
|
|
PROBLEM_MESSAGE_PREFIX = "admin-problem:"
|
2023-11-02 20:05:29 -05:00
|
|
|
SCHEDULED_PROBLEM_STORAGE_KEY = "admin-found-scheduled-problems-list"
|
2021-12-19 17:59:11 -06:00
|
|
|
|
2019-04-01 05:39:49 -05:00
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_json
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_json(_options = nil)
|
|
|
|
@json ||= get_json
|
|
|
|
end
|
|
|
|
|
2013-04-25 16:53:31 -05:00
|
|
|
def problems
|
2015-08-25 19:07:40 -05:00
|
|
|
problems = []
|
2021-12-19 17:59:11 -06:00
|
|
|
self.class.problem_messages.each do |i18n_key|
|
|
|
|
message = self.class.problem_message_check(i18n_key)
|
2024-03-13 21:55:01 -05:00
|
|
|
problems << ProblemCheck::Problem.new(message) if message.present?
|
2016-04-05 13:42:24 -05:00
|
|
|
end
|
2024-03-13 21:55:01 -05:00
|
|
|
problems.concat(ProblemCheck.realtime.flat_map { |c| c.call(@opts).map(&:to_h) })
|
|
|
|
|
2021-12-19 17:59:11 -06:00
|
|
|
problems += self.class.load_found_scheduled_check_problems
|
2016-04-08 15:44:04 -05:00
|
|
|
problems.compact!
|
|
|
|
|
|
|
|
if problems.empty?
|
|
|
|
self.class.clear_problems_started
|
|
|
|
else
|
|
|
|
self.class.set_problems_started
|
|
|
|
end
|
|
|
|
|
|
|
|
problems
|
|
|
|
end
|
|
|
|
|
2021-12-19 17:59:11 -06:00
|
|
|
def self.add_problem_check(*syms, &blk)
|
2024-03-19 19:52:25 -05:00
|
|
|
Discourse.deprecate(
|
|
|
|
"`AdminDashboardData#add_problem_check` is deprecated. Implement a class that inherits `ProblemCheck` instead.",
|
|
|
|
drop_from: "3.3",
|
|
|
|
)
|
2016-04-08 15:44:04 -05:00
|
|
|
end
|
|
|
|
|
2021-12-19 17:59:11 -06:00
|
|
|
def self.add_found_scheduled_check_problem(problem)
|
|
|
|
problems = load_found_scheduled_check_problems
|
|
|
|
if problem.identifier.present?
|
|
|
|
return if problems.find { |p| p.identifier == problem.identifier }
|
|
|
|
end
|
2023-11-02 20:05:29 -05:00
|
|
|
set_found_scheduled_check_problem(problem)
|
2016-04-08 15:44:04 -05:00
|
|
|
end
|
|
|
|
|
2023-11-02 20:05:29 -05:00
|
|
|
def self.set_found_scheduled_check_problem(problem)
|
|
|
|
Discourse.redis.rpush(SCHEDULED_PROBLEM_STORAGE_KEY, JSON.dump(problem.to_h))
|
2015-08-25 19:07:40 -05:00
|
|
|
end
|
|
|
|
|
2021-12-19 17:59:11 -06:00
|
|
|
def self.clear_found_scheduled_check_problems
|
|
|
|
Discourse.redis.del(SCHEDULED_PROBLEM_STORAGE_KEY)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_found_problem(identifier)
|
|
|
|
problems = load_found_scheduled_check_problems
|
2023-11-02 20:05:29 -05:00
|
|
|
problem = problems.find { |p| p.identifier == identifier }
|
|
|
|
Discourse.redis.lrem(SCHEDULED_PROBLEM_STORAGE_KEY, 1, JSON.dump(problem.to_h))
|
2021-12-19 17:59:11 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_found_scheduled_check_problems
|
2023-11-02 20:05:29 -05:00
|
|
|
found_problems = Discourse.redis.lrange(SCHEDULED_PROBLEM_STORAGE_KEY, 0, -1)
|
|
|
|
|
|
|
|
return [] if found_problems.blank?
|
|
|
|
|
|
|
|
found_problems.filter_map do |problem|
|
|
|
|
begin
|
2024-03-13 21:55:01 -05:00
|
|
|
ProblemCheck::Problem.from_h(JSON.parse(problem))
|
2023-11-02 20:05:29 -05:00
|
|
|
rescue JSON::ParserError => err
|
|
|
|
Discourse.warn_exception(
|
|
|
|
err,
|
|
|
|
message: "Error parsing found problem JSON in admin dashboard: #{problem}",
|
|
|
|
)
|
|
|
|
nil
|
|
|
|
end
|
2021-12-19 17:59:11 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# 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 15:32:35 -05:00
|
|
|
def self.reset_problem_checks
|
2023-01-09 06:20:10 -06:00
|
|
|
@@problem_messages = %w[
|
|
|
|
dashboard.bad_favicon_url
|
|
|
|
dashboard.poll_pop3_timeout
|
|
|
|
dashboard.poll_pop3_auth_error
|
2016-04-08 16:33:47 -05:00
|
|
|
]
|
2013-04-25 16:53:31 -05:00
|
|
|
end
|
2015-09-01 15:32:35 -05:00
|
|
|
reset_problem_checks
|
2014-05-21 17:19:40 -05:00
|
|
|
|
2021-12-19 17:59:11 -06: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
|
2023-01-09 06:20:10 -06:00
|
|
|
"dash-problems-started-at"
|
2021-12-19 17:59:11 -06:00
|
|
|
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 00:46:10 -06:00
|
|
|
def self.fetch_problems(opts = {})
|
2021-12-19 17:59:11 -06:00
|
|
|
new(opts).problems
|
2013-03-29 14:48:26 -05:00
|
|
|
end
|
|
|
|
|
2016-04-05 13:42:24 -05:00
|
|
|
def self.problem_message_check(i18n_key)
|
2023-01-09 06:20:10 -06:00
|
|
|
if Discourse.redis.get(problem_message_key(i18n_key))
|
|
|
|
I18n.t(i18n_key, base_path: Discourse.base_path)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2016-04-05 13:42:24 -05:00
|
|
|
end
|
|
|
|
|
2021-12-19 17:59:11 -06: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.
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.add_problem_message(i18n_key, expire_seconds = nil)
|
2016-04-05 13:42:24 -05:00
|
|
|
if expire_seconds.to_i > 0
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.setex problem_message_key(i18n_key), expire_seconds.to_i, 1
|
2016-04-05 13:42:24 -05:00
|
|
|
else
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.set problem_message_key(i18n_key), 1
|
2016-04-05 13:42:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_problem_message(i18n_key)
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.del problem_message_key(i18n_key)
|
2016-04-05 13:42:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.problem_message_key(i18n_key)
|
2021-12-19 17:59:11 -06:00
|
|
|
"#{PROBLEM_MESSAGE_PREFIX}#{i18n_key}"
|
2016-04-05 13:42:24 -05:00
|
|
|
end
|
2013-06-19 15:11:11 -05:00
|
|
|
end
|