2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-03-20 11:18:00 +08:00
|
|
|
class AdminDashboardData
|
2015-07-07 12:52:19 +08:00
|
|
|
include StatsCacheable
|
2013-03-20 11:18:00 +08:00
|
|
|
|
2019-04-01 20:35:09 +08:00
|
|
|
# kept for backward compatibility
|
|
|
|
GLOBAL_REPORTS ||= []
|
|
|
|
|
2019-04-01 18:39:49 +08:00
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.fetch_stats
|
|
|
|
new.as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_json
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_json(_options = nil)
|
|
|
|
@json ||= get_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
|
2013-03-20 11:18:00 +08:00
|
|
|
|
2015-08-26 08:07:40 +08:00
|
|
|
def self.add_problem_check(*syms, &blk)
|
|
|
|
@problem_syms.push(*syms) if syms
|
|
|
|
@problem_blocks << blk if blk
|
|
|
|
end
|
2016-04-06 02:42:24 +08:00
|
|
|
class << self; attr_reader :problem_syms, :problem_blocks, :problem_messages; end
|
2015-08-26 08:07:40 +08:00
|
|
|
|
2013-04-26 05:53:31 +08:00
|
|
|
def problems
|
2015-08-26 08:07:40 +08:00
|
|
|
problems = []
|
|
|
|
AdminDashboardData.problem_syms.each do |sym|
|
2019-05-07 09:57:55 +08:00
|
|
|
problems << public_send(sym)
|
2015-08-26 08:07:40 +08:00
|
|
|
end
|
|
|
|
AdminDashboardData.problem_blocks.each do |blk|
|
|
|
|
problems << instance_exec(&blk)
|
|
|
|
end
|
2016-04-06 02:42:24 +08:00
|
|
|
AdminDashboardData.problem_messages.each do |i18n_key|
|
|
|
|
problems << AdminDashboardData.problem_message_check(i18n_key)
|
|
|
|
end
|
2016-04-09 04:44:04 +08:00
|
|
|
problems.compact!
|
|
|
|
|
|
|
|
if problems.empty?
|
|
|
|
self.class.clear_problems_started
|
|
|
|
else
|
|
|
|
self.class.set_problems_started
|
|
|
|
end
|
|
|
|
|
|
|
|
problems
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.problems_started_key
|
2019-04-01 18:39:49 +08:00
|
|
|
'dash-problems-started-at'
|
2016-04-09 04:44:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_problems_started
|
2019-12-03 17:05:53 +08:00
|
|
|
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)
|
2016-04-09 04:44:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_problems_started
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.del problems_started_key
|
2016-04-09 04:44:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.problems_started_at
|
2019-12-03 17:05:53 +08:00
|
|
|
s = Discourse.redis.get(problems_started_key)
|
2016-04-09 04:44:04 +08:00
|
|
|
s ? Time.zone.parse(s) : nil
|
2015-08-26 08:07:40 +08:00
|
|
|
end
|
|
|
|
|
2015-09-02 04:32:35 +08:00
|
|
|
# used for testing
|
|
|
|
def self.reset_problem_checks
|
|
|
|
@problem_syms = []
|
|
|
|
@problem_blocks = []
|
2016-04-09 05:33:47 +08:00
|
|
|
|
|
|
|
@problem_messages = [
|
|
|
|
'dashboard.bad_favicon_url',
|
|
|
|
'dashboard.poll_pop3_timeout',
|
2019-11-08 07:58:19 +08:00
|
|
|
'dashboard.poll_pop3_auth_error',
|
2019-11-09 08:28:48 +08:00
|
|
|
'dashboard.deprecated_api_usage',
|
|
|
|
'dashboard.update_mail_receiver'
|
2016-04-09 05:33:47 +08:00
|
|
|
]
|
2015-09-02 04:32:35 +08:00
|
|
|
|
2018-01-11 14:46:10 +08:00
|
|
|
add_problem_check :rails_env_check, :host_names_check, :force_https_check,
|
2016-05-25 19:08:48 +08:00
|
|
|
:ram_check, :google_oauth2_config_check,
|
2015-09-02 04:32:35 +08:00
|
|
|
:facebook_config_check, :twitter_config_check,
|
2019-05-01 21:44:45 +08:00
|
|
|
:github_config_check, :s3_config_check,
|
2019-01-09 08:46:11 +08:00
|
|
|
:image_magick_check, :failing_emails_check,
|
2016-10-06 00:14:56 +08:00
|
|
|
:subfolder_ends_in_slash_check,
|
2018-08-03 07:53:48 +08:00
|
|
|
:pop3_polling_configuration, :email_polling_errored_recently,
|
2019-08-01 01:33:49 +08:00
|
|
|
:out_of_date_themes, :unreachable_themes, :watched_words_check
|
2015-09-02 04:32:35 +08:00
|
|
|
|
|
|
|
add_problem_check do
|
|
|
|
sidekiq_check || queue_size_check
|
|
|
|
end
|
2013-04-26 05:53:31 +08:00
|
|
|
end
|
2015-09-02 04:32:35 +08:00
|
|
|
reset_problem_checks
|
2014-05-22 06:19:40 +08:00
|
|
|
|
2018-01-11 14:46:10 +08:00
|
|
|
def self.fetch_problems(opts = {})
|
|
|
|
AdminDashboardData.new(opts).problems
|
2013-03-30 03:48:26 +08:00
|
|
|
end
|
|
|
|
|
2016-04-06 02:42:24 +08:00
|
|
|
def self.problem_message_check(i18n_key)
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.get(problem_message_key(i18n_key)) ? I18n.t(i18n_key, base_path: Discourse.base_path) : nil
|
2016-04-06 02:42:24 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.add_problem_message(i18n_key, expire_seconds = nil)
|
2016-04-06 02:42:24 +08:00
|
|
|
if expire_seconds.to_i > 0
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.setex problem_message_key(i18n_key), expire_seconds.to_i, 1
|
2016-04-06 02:42:24 +08:00
|
|
|
else
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.set problem_message_key(i18n_key), 1
|
2016-04-06 02:42:24 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_problem_message(i18n_key)
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.del problem_message_key(i18n_key)
|
2016-04-06 02:42:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.problem_message_key(i18n_key)
|
|
|
|
"admin-problem:#{i18n_key}"
|
|
|
|
end
|
|
|
|
|
2013-03-20 11:18:00 +08:00
|
|
|
def rails_env_check
|
2014-08-18 14:42:48 +08:00
|
|
|
I18n.t("dashboard.rails_env_warning", env: Rails.env) unless Rails.env.production?
|
2013-03-20 11:18:00 +08:00
|
|
|
end
|
2013-03-21 03:38:28 +08:00
|
|
|
|
|
|
|
def host_names_check
|
|
|
|
I18n.t("dashboard.host_names_warning") if ['localhost', 'production.localhost'].include?(Discourse.current_hostname)
|
|
|
|
end
|
2013-03-21 04:16:23 +08:00
|
|
|
|
2013-03-22 23:35:32 +08:00
|
|
|
def sidekiq_check
|
|
|
|
last_job_performed_at = Jobs.last_job_performed_at
|
2017-02-15 16:21:17 +08:00
|
|
|
I18n.t('dashboard.sidekiq_warning') if Jobs.queued > 0 && (last_job_performed_at.nil? || last_job_performed_at < 2.minutes.ago)
|
2013-03-22 23:35:32 +08:00
|
|
|
end
|
|
|
|
|
2015-08-07 04:46:49 +08:00
|
|
|
def queue_size_check
|
|
|
|
queue_size = Jobs.queued
|
|
|
|
I18n.t('dashboard.queue_size_warning', queue_size: queue_size) unless queue_size < 100_000
|
|
|
|
end
|
|
|
|
|
2013-03-23 03:47:25 +08:00
|
|
|
def ram_check
|
2019-04-12 07:44:29 +08:00
|
|
|
I18n.t('dashboard.memory_warning') if MemInfo.new.mem_total && MemInfo.new.mem_total < 950_000
|
2013-03-23 03:47:25 +08:00
|
|
|
end
|
2013-03-30 01:31:00 +08:00
|
|
|
|
2014-05-22 06:19:40 +08:00
|
|
|
def google_oauth2_config_check
|
2018-11-08 00:59:42 +08:00
|
|
|
if SiteSetting.enable_google_oauth2_logins && (SiteSetting.google_oauth2_client_id.blank? || SiteSetting.google_oauth2_client_secret.blank?)
|
|
|
|
I18n.t('dashboard.google_oauth2_config_warning', base_path: Discourse.base_path)
|
|
|
|
end
|
2014-05-22 06:19:40 +08:00
|
|
|
end
|
|
|
|
|
2013-03-30 01:31:00 +08:00
|
|
|
def facebook_config_check
|
2018-11-08 00:59:42 +08:00
|
|
|
if SiteSetting.enable_facebook_logins && (SiteSetting.facebook_app_id.blank? || SiteSetting.facebook_app_secret.blank?)
|
|
|
|
I18n.t('dashboard.facebook_config_warning', base_path: Discourse.base_path)
|
|
|
|
end
|
2013-03-30 01:31:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def twitter_config_check
|
2018-11-08 00:59:42 +08:00
|
|
|
if SiteSetting.enable_twitter_logins && (SiteSetting.twitter_consumer_key.blank? || SiteSetting.twitter_consumer_secret.blank?)
|
|
|
|
I18n.t('dashboard.twitter_config_warning', base_path: Discourse.base_path)
|
|
|
|
end
|
2013-03-30 01:31:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def github_config_check
|
2018-11-08 00:59:42 +08:00
|
|
|
if SiteSetting.enable_github_logins && (SiteSetting.github_client_id.blank? || SiteSetting.github_client_secret.blank?)
|
|
|
|
I18n.t('dashboard.github_config_warning', base_path: Discourse.base_path)
|
|
|
|
end
|
2013-06-20 04:11:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def s3_config_check
|
2018-07-09 13:38:06 +08:00
|
|
|
# if set via global setting it is validated during the `use_s3?` call
|
|
|
|
if !GlobalSetting.use_s3?
|
|
|
|
bad_keys = (SiteSetting.s3_access_key_id.blank? || SiteSetting.s3_secret_access_key.blank?) && !SiteSetting.s3_use_iam_profile
|
2014-07-06 06:16:13 +08:00
|
|
|
|
2018-11-08 00:59:42 +08:00
|
|
|
if SiteSetting.enable_s3_uploads && (bad_keys || SiteSetting.s3_upload_bucket.blank?)
|
|
|
|
return I18n.t('dashboard.s3_config_warning', base_path: Discourse.base_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
if SiteSetting.backup_location == BackupLocationSiteSetting::S3 && (bad_keys || SiteSetting.s3_backup_bucket.blank?)
|
|
|
|
return I18n.t('dashboard.s3_backup_config_warning', base_path: Discourse.base_path)
|
|
|
|
end
|
2018-07-09 13:38:06 +08:00
|
|
|
end
|
2014-03-18 03:56:59 +08:00
|
|
|
nil
|
2013-03-30 01:31:00 +08:00
|
|
|
end
|
2013-04-23 01:37:16 +08:00
|
|
|
|
2013-06-20 04:36:56 +08:00
|
|
|
def image_magick_check
|
2017-02-15 16:21:17 +08:00
|
|
|
I18n.t('dashboard.image_magick_warning') if SiteSetting.create_thumbnails && !system("command -v convert >/dev/null;")
|
2013-06-20 04:36:56 +08:00
|
|
|
end
|
|
|
|
|
2013-04-23 01:37:16 +08:00
|
|
|
def failing_emails_check
|
|
|
|
num_failed_jobs = Jobs.num_email_retry_jobs
|
2018-11-08 00:59:42 +08:00
|
|
|
I18n.t('dashboard.failing_emails_warning', num_failed_jobs: num_failed_jobs, base_path: Discourse.base_path) if num_failed_jobs > 0
|
2013-04-23 01:37:16 +08:00
|
|
|
end
|
2013-04-23 03:38:48 +08:00
|
|
|
|
2015-09-07 11:20:59 +08:00
|
|
|
def subfolder_ends_in_slash_check
|
|
|
|
I18n.t('dashboard.subfolder_ends_in_slash') if Discourse.base_uri =~ /\/$/
|
|
|
|
end
|
|
|
|
|
2016-02-17 18:25:49 +08:00
|
|
|
def pop3_polling_configuration
|
|
|
|
POP3PollingEnabledSettingValidator.new.error_message if SiteSetting.pop3_polling_enabled
|
|
|
|
end
|
|
|
|
|
2016-03-17 04:17:48 +08:00
|
|
|
def email_polling_errored_recently
|
|
|
|
errors = Jobs::PollMailbox.errors_in_past_24_hours
|
2018-11-08 00:59:42 +08:00
|
|
|
I18n.t('dashboard.email_polling_errored_recently', count: errors, base_path: Discourse.base_path) if errors > 0
|
2016-03-17 04:17:48 +08:00
|
|
|
end
|
|
|
|
|
2016-05-30 23:11:17 +08:00
|
|
|
def missing_mailgun_api_key
|
|
|
|
return unless SiteSetting.reply_by_email_enabled
|
2019-04-01 18:39:49 +08:00
|
|
|
return unless ActionMailer::Base.smtp_settings[:address]['smtp.mailgun.org']
|
2016-05-30 23:11:17 +08:00
|
|
|
return unless SiteSetting.mailgun_api_key.blank?
|
|
|
|
I18n.t('dashboard.missing_mailgun_api_key')
|
|
|
|
end
|
|
|
|
|
2018-01-11 14:46:10 +08:00
|
|
|
def force_https_check
|
|
|
|
return unless @opts[:check_force_https]
|
2018-11-08 00:59:42 +08:00
|
|
|
I18n.t('dashboard.force_https_warning', base_path: Discourse.base_path) unless SiteSetting.force_https
|
2018-01-11 14:46:10 +08:00
|
|
|
end
|
|
|
|
|
2019-08-01 01:33:49 +08:00
|
|
|
def watched_words_check
|
|
|
|
WatchedWord.actions.keys.each do |action|
|
|
|
|
begin
|
|
|
|
WordWatcher.word_matcher_regexp(action, raise_errors: true)
|
|
|
|
rescue RegexpError => e
|
|
|
|
return I18n.t('dashboard.watched_word_regexp_error', base_path: Discourse.base_path, action: action)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-08-03 07:53:48 +08:00
|
|
|
def out_of_date_themes
|
|
|
|
old_themes = RemoteTheme.out_of_date_themes
|
|
|
|
return unless old_themes.present?
|
|
|
|
|
2019-04-01 18:39:49 +08:00
|
|
|
themes_html_format(old_themes, 'dashboard.out_of_date_themes')
|
2018-09-08 21:24:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unreachable_themes
|
|
|
|
themes = RemoteTheme.unreachable_themes
|
|
|
|
return unless themes.present?
|
|
|
|
|
2019-04-01 18:39:49 +08:00
|
|
|
themes_html_format(themes, 'dashboard.unreachable_themes')
|
2018-09-08 21:24:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def themes_html_format(themes, i18n_key)
|
|
|
|
html = themes.map do |name, id|
|
2018-08-03 07:53:48 +08:00
|
|
|
"<li><a href=\"/admin/customize/themes/#{id}\">#{CGI.escapeHTML(name)}</a></li>"
|
|
|
|
end.join("\n")
|
|
|
|
|
2019-04-01 18:39:49 +08:00
|
|
|
"#{I18n.t(i18n_key)}<ul>#{html}</ul>"
|
2018-08-03 07:53:48 +08:00
|
|
|
end
|
2013-06-20 04:11:11 +08:00
|
|
|
end
|