2013-03-22 15:47:25 -04:00
|
|
|
require_dependency 'mem_info'
|
|
|
|
|
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
|
|
|
|
2015-06-24 20:42:08 -04:00
|
|
|
GLOBAL_REPORTS ||= [
|
2013-04-16 16:56:18 -04:00
|
|
|
'visits',
|
|
|
|
'signups',
|
2015-09-14 13:30:06 -04:00
|
|
|
'profile_views',
|
2013-04-16 16:56:18 -04:00
|
|
|
'topics',
|
|
|
|
'posts',
|
2015-06-22 13:46:51 -04:00
|
|
|
'time_to_first_response',
|
|
|
|
'topics_with_no_response',
|
2013-04-16 16:56:18 -04:00
|
|
|
'likes',
|
2015-06-24 20:42:08 -04:00
|
|
|
'flags',
|
2013-04-18 14:27:22 -04:00
|
|
|
'bookmarks',
|
2013-04-16 16:56:18 -04:00
|
|
|
'emails',
|
2015-06-24 20:42:08 -04:00
|
|
|
]
|
|
|
|
|
2015-07-07 12:31:07 -04:00
|
|
|
PAGE_VIEW_REPORTS ||= ['page_view_total_reqs'] + ApplicationRequest.req_types.keys.select { |r| r =~ /^page_view_/ && r !~ /mobile/ }.map { |r| r + "_reqs" }
|
2015-06-24 20:42:08 -04:00
|
|
|
|
|
|
|
PRIVATE_MESSAGE_REPORTS ||= [
|
2013-04-16 16:56:18 -04:00
|
|
|
'user_to_user_private_messages',
|
|
|
|
'system_private_messages',
|
|
|
|
'notify_moderators_private_messages',
|
2015-02-05 22:39:04 -05:00
|
|
|
'notify_user_private_messages',
|
2015-06-24 20:42:08 -04:00
|
|
|
'moderator_warning_private_messages',
|
|
|
|
]
|
|
|
|
|
|
|
|
HTTP_REPORTS ||= ApplicationRequest.req_types.keys.select { |r| r =~ /^http_/ }.map { |r| r + "_reqs" }.sort
|
|
|
|
|
|
|
|
USER_REPORTS ||= ['users_by_trust_level']
|
|
|
|
|
2015-07-07 12:31:07 -04:00
|
|
|
MOBILE_REPORTS ||= ['mobile_visits'] + ApplicationRequest.req_types.keys.select {|r| r =~ /mobile/}.map { |r| r + "_reqs" }
|
2013-03-19 23:18:00 -04:00
|
|
|
|
2015-08-25 20:07:40 -04:00
|
|
|
def self.add_problem_check(*syms, &blk)
|
|
|
|
@problem_syms.push(*syms) if syms
|
|
|
|
@problem_blocks << blk if blk
|
|
|
|
end
|
2016-04-05 14:42:24 -04:00
|
|
|
class << self; attr_reader :problem_syms, :problem_blocks, :problem_messages; end
|
2015-08-25 20:07:40 -04:00
|
|
|
|
2013-04-25 17:53:31 -04:00
|
|
|
def problems
|
2015-08-25 20:07:40 -04:00
|
|
|
problems = []
|
|
|
|
AdminDashboardData.problem_syms.each do |sym|
|
|
|
|
problems << send(sym)
|
|
|
|
end
|
|
|
|
AdminDashboardData.problem_blocks.each do |blk|
|
|
|
|
problems << instance_exec(&blk)
|
|
|
|
end
|
2016-04-05 14:42:24 -04:00
|
|
|
AdminDashboardData.problem_messages.each do |i18n_key|
|
|
|
|
problems << AdminDashboardData.problem_message_check(i18n_key)
|
|
|
|
end
|
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
|
|
|
|
|
|
|
|
def self.problems_started_key
|
|
|
|
"dash-problems-started-at"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_problems_started
|
|
|
|
existing_time = $redis.get(problems_started_key)
|
|
|
|
$redis.setex(problems_started_key, 14.days.to_i, existing_time || Time.zone.now.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_problems_started
|
|
|
|
$redis.del problems_started_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.problems_started_at
|
|
|
|
s = $redis.get(problems_started_key)
|
|
|
|
s ? Time.zone.parse(s) : nil
|
2015-08-25 20:07:40 -04:00
|
|
|
end
|
|
|
|
|
2015-09-01 16:32:35 -04:00
|
|
|
# used for testing
|
|
|
|
def self.reset_problem_checks
|
|
|
|
@problem_syms = []
|
|
|
|
@problem_blocks = []
|
2016-04-08 17:33:47 -04:00
|
|
|
|
|
|
|
@problem_messages = [
|
|
|
|
'dashboard.bad_favicon_url',
|
|
|
|
'dashboard.poll_pop3_timeout',
|
|
|
|
'dashboard.poll_pop3_auth_error'
|
|
|
|
]
|
2015-09-01 16:32:35 -04:00
|
|
|
|
|
|
|
add_problem_check :rails_env_check, :ruby_version_check, :host_names_check,
|
2016-05-25 07:08:48 -04:00
|
|
|
:ram_check, :google_oauth2_config_check,
|
2015-09-01 16:32:35 -04:00
|
|
|
:facebook_config_check, :twitter_config_check,
|
|
|
|
:github_config_check, :s3_config_check, :image_magick_check,
|
|
|
|
:failing_emails_check, :default_logo_check, :contact_email_check,
|
|
|
|
:send_consumer_email_check, :title_check,
|
|
|
|
:site_description_check, :site_contact_username_check,
|
2016-02-17 05:25:49 -05:00
|
|
|
:notification_email_check, :subfolder_ends_in_slash_check,
|
2016-03-16 16:17:48 -04:00
|
|
|
:pop3_polling_configuration, :email_polling_errored_recently
|
2015-09-01 16:32:35 -04:00
|
|
|
|
|
|
|
add_problem_check do
|
|
|
|
sidekiq_check || queue_size_check
|
|
|
|
end
|
2013-04-25 17:53:31 -04:00
|
|
|
end
|
2015-09-01 16:32:35 -04:00
|
|
|
reset_problem_checks
|
2014-05-21 18:19:40 -04:00
|
|
|
|
2013-08-02 18:31:25 -04:00
|
|
|
def self.fetch_stats
|
2015-07-07 00:52:19 -04:00
|
|
|
AdminDashboardData.new.as_json
|
2013-08-02 18:31:25 -04:00
|
|
|
end
|
2015-06-24 20:42:08 -04:00
|
|
|
|
2013-08-02 18:31:25 -04:00
|
|
|
def self.stats_cache_key
|
|
|
|
'dash-stats'
|
|
|
|
end
|
2013-03-19 23:18:00 -04:00
|
|
|
|
2013-03-29 15:48:26 -04:00
|
|
|
def self.fetch_problems
|
|
|
|
AdminDashboardData.new.problems
|
|
|
|
end
|
|
|
|
|
2016-04-05 14:42:24 -04:00
|
|
|
def self.problem_message_check(i18n_key)
|
|
|
|
$redis.get(problem_message_key(i18n_key)) ? I18n.t(i18n_key) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_problem_message(i18n_key, expire_seconds=nil)
|
|
|
|
if expire_seconds.to_i > 0
|
|
|
|
$redis.setex problem_message_key(i18n_key), expire_seconds.to_i, 1
|
|
|
|
else
|
|
|
|
$redis.set problem_message_key(i18n_key), 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_problem_message(i18n_key)
|
|
|
|
$redis.del problem_message_key(i18n_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.problem_message_key(i18n_key)
|
|
|
|
"admin-problem:#{i18n_key}"
|
|
|
|
end
|
|
|
|
|
2014-08-20 14:14:19 -04:00
|
|
|
def as_json(_options = nil)
|
2013-03-19 23:18:00 -04:00
|
|
|
@json ||= {
|
2015-06-24 20:42:08 -04:00
|
|
|
global_reports: AdminDashboardData.reports(GLOBAL_REPORTS),
|
|
|
|
page_view_reports: AdminDashboardData.reports(PAGE_VIEW_REPORTS),
|
|
|
|
private_message_reports: AdminDashboardData.reports(PRIVATE_MESSAGE_REPORTS),
|
|
|
|
http_reports: AdminDashboardData.reports(HTTP_REPORTS),
|
|
|
|
user_reports: AdminDashboardData.reports(USER_REPORTS),
|
2015-07-07 12:31:07 -04:00
|
|
|
mobile_reports: AdminDashboardData.reports(MOBILE_REPORTS),
|
2013-03-29 02:29:58 -04:00
|
|
|
admins: User.admins.count,
|
2013-05-01 18:12:02 -04:00
|
|
|
moderators: User.moderators.count,
|
2013-11-07 13:53:32 -05:00
|
|
|
suspended: User.suspended.count,
|
2013-06-04 11:53:19 -04:00
|
|
|
blocked: User.blocked.count,
|
2013-05-14 16:17:17 -04:00
|
|
|
top_referrers: IncomingLinksReport.find('top_referrers').as_json,
|
|
|
|
top_traffic_sources: IncomingLinksReport.find('top_traffic_sources').as_json,
|
2013-08-02 18:31:25 -04:00
|
|
|
top_referred_topics: IncomingLinksReport.find('top_referred_topics').as_json,
|
|
|
|
updated_at: Time.zone.now.as_json
|
2013-07-30 12:11:51 -04:00
|
|
|
}
|
2013-03-19 23:18:00 -04:00
|
|
|
end
|
|
|
|
|
2015-06-24 20:42:08 -04:00
|
|
|
def self.reports(source)
|
|
|
|
source.map { |type| Report.find(type).as_json }
|
|
|
|
end
|
|
|
|
|
2013-03-19 23:18:00 -04:00
|
|
|
def rails_env_check
|
2014-08-18 02:42:48 -04:00
|
|
|
I18n.t("dashboard.rails_env_warning", env: Rails.env) unless Rails.env.production?
|
2013-03-19 23:18:00 -04:00
|
|
|
end
|
2013-03-20 15:38:28 -04:00
|
|
|
|
|
|
|
def host_names_check
|
|
|
|
I18n.t("dashboard.host_names_warning") if ['localhost', 'production.localhost'].include?(Discourse.current_hostname)
|
|
|
|
end
|
2013-03-20 16:16:23 -04:00
|
|
|
|
2013-03-22 11:35:32 -04:00
|
|
|
def sidekiq_check
|
|
|
|
last_job_performed_at = Jobs.last_job_performed_at
|
|
|
|
I18n.t('dashboard.sidekiq_warning') if Jobs.queued > 0 and (last_job_performed_at.nil? or last_job_performed_at < 2.minutes.ago)
|
|
|
|
end
|
|
|
|
|
2015-08-06 16:46:49 -04: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-22 15:47:25 -04:00
|
|
|
def ram_check
|
|
|
|
I18n.t('dashboard.memory_warning') if MemInfo.new.mem_total and MemInfo.new.mem_total < 1_000_000
|
|
|
|
end
|
2013-03-29 13:31:00 -04:00
|
|
|
|
2014-05-21 18:19:40 -04:00
|
|
|
def google_oauth2_config_check
|
2014-05-21 19:11:02 -04:00
|
|
|
I18n.t('dashboard.google_oauth2_config_warning') if SiteSetting.enable_google_oauth2_logins && (SiteSetting.google_oauth2_client_id.blank? || SiteSetting.google_oauth2_client_secret.blank?)
|
2014-05-21 18:19:40 -04:00
|
|
|
end
|
|
|
|
|
2013-03-29 13:31:00 -04:00
|
|
|
def facebook_config_check
|
2014-05-21 18:19:40 -04:00
|
|
|
I18n.t('dashboard.facebook_config_warning') if SiteSetting.enable_facebook_logins && (SiteSetting.facebook_app_id.blank? || SiteSetting.facebook_app_secret.blank?)
|
2013-03-29 13:31:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def twitter_config_check
|
2013-06-19 16:11:11 -04:00
|
|
|
I18n.t('dashboard.twitter_config_warning') if SiteSetting.enable_twitter_logins and (SiteSetting.twitter_consumer_key.blank? or SiteSetting.twitter_consumer_secret.blank?)
|
2013-03-29 13:31:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def github_config_check
|
2013-06-19 16:11:11 -04:00
|
|
|
I18n.t('dashboard.github_config_warning') if SiteSetting.enable_github_logins and (SiteSetting.github_client_id.blank? or SiteSetting.github_client_secret.blank?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def s3_config_check
|
2014-07-05 18:16:13 -04:00
|
|
|
bad_keys = (SiteSetting.s3_access_key_id.blank? or SiteSetting.s3_secret_access_key.blank?) and !SiteSetting.s3_use_iam_profile
|
|
|
|
|
|
|
|
return I18n.t('dashboard.s3_config_warning') if SiteSetting.enable_s3_uploads and (bad_keys or SiteSetting.s3_upload_bucket.blank?)
|
|
|
|
return I18n.t('dashboard.s3_backup_config_warning') if SiteSetting.enable_s3_backups and (bad_keys or SiteSetting.s3_backup_bucket.blank?)
|
2014-03-17 15:56:59 -04:00
|
|
|
nil
|
2013-03-29 13:31:00 -04:00
|
|
|
end
|
2013-04-22 13:37:16 -04:00
|
|
|
|
2013-06-19 16:36:56 -04:00
|
|
|
def image_magick_check
|
2013-06-20 21:42:18 -04:00
|
|
|
I18n.t('dashboard.image_magick_warning') if SiteSetting.create_thumbnails and !system("command -v convert >/dev/null;")
|
2013-06-19 16:36:56 -04:00
|
|
|
end
|
|
|
|
|
2013-04-22 13:37:16 -04:00
|
|
|
def failing_emails_check
|
|
|
|
num_failed_jobs = Jobs.num_email_retry_jobs
|
|
|
|
I18n.t('dashboard.failing_emails_warning', num_failed_jobs: num_failed_jobs) if num_failed_jobs > 0
|
|
|
|
end
|
2013-04-22 15:38:48 -04:00
|
|
|
|
|
|
|
def default_logo_check
|
2013-06-05 16:59:19 -04:00
|
|
|
if SiteSetting.logo_url =~ /#{SiteSetting.defaults[:logo_url].split('/').last}/ or
|
|
|
|
SiteSetting.logo_small_url =~ /#{SiteSetting.defaults[:logo_small_url].split('/').last}/ or
|
|
|
|
SiteSetting.favicon_url =~ /#{SiteSetting.defaults[:favicon_url].split('/').last}/
|
2013-04-22 15:38:48 -04:00
|
|
|
I18n.t('dashboard.default_logo_warning')
|
|
|
|
end
|
|
|
|
end
|
2013-04-24 11:15:37 -04:00
|
|
|
|
|
|
|
def contact_email_check
|
|
|
|
return I18n.t('dashboard.contact_email_missing') if !SiteSetting.contact_email.present?
|
|
|
|
return I18n.t('dashboard.contact_email_invalid') if !(SiteSetting.contact_email =~ User::EMAIL)
|
|
|
|
end
|
2013-04-25 17:53:31 -04:00
|
|
|
|
|
|
|
def title_check
|
|
|
|
I18n.t('dashboard.title_nag') if SiteSetting.title == SiteSetting.defaults[:title]
|
|
|
|
end
|
2013-05-01 18:12:02 -04:00
|
|
|
|
2013-09-11 16:32:49 -04:00
|
|
|
def site_description_check
|
2013-11-15 10:46:41 -05:00
|
|
|
I18n.t('dashboard.site_description_missing') if !SiteSetting.site_description.present?
|
2013-09-11 16:32:49 -04:00
|
|
|
end
|
|
|
|
|
2013-06-10 03:08:06 -04:00
|
|
|
def send_consumer_email_check
|
2014-08-18 02:42:48 -04:00
|
|
|
I18n.t('dashboard.consumer_email_warning') if Rails.env.production? and ActionMailer::Base.smtp_settings[:address] =~ /gmail\.com|live\.com|yahoo\.com/
|
2013-05-29 13:54:49 -04:00
|
|
|
end
|
|
|
|
|
2013-09-06 03:28:37 -04:00
|
|
|
def site_contact_username_check
|
2015-01-05 11:55:03 -05:00
|
|
|
I18n.t('dashboard.site_contact_username_warning') if !SiteSetting.site_contact_username.present? || SiteSetting.site_contact_username == SiteSetting.defaults[:site_contact_username]
|
2013-06-26 10:57:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def notification_email_check
|
2015-01-05 11:55:03 -05:00
|
|
|
I18n.t('dashboard.notification_email_warning') if !SiteSetting.notification_email.present? || SiteSetting.notification_email == SiteSetting.defaults[:notification_email]
|
2013-06-26 10:57:04 -04:00
|
|
|
end
|
|
|
|
|
2013-10-24 18:22:47 -04:00
|
|
|
def ruby_version_check
|
|
|
|
I18n.t('dashboard.ruby_version_warning') if RUBY_VERSION == '2.0.0' and RUBY_PATCHLEVEL < 247
|
|
|
|
end
|
|
|
|
|
2015-09-06 23:20:59 -04:00
|
|
|
def subfolder_ends_in_slash_check
|
|
|
|
I18n.t('dashboard.subfolder_ends_in_slash') if Discourse.base_uri =~ /\/$/
|
|
|
|
end
|
|
|
|
|
2016-02-17 05:25:49 -05:00
|
|
|
def pop3_polling_configuration
|
|
|
|
POP3PollingEnabledSettingValidator.new.error_message if SiteSetting.pop3_polling_enabled
|
|
|
|
end
|
|
|
|
|
2016-03-16 16:17:48 -04:00
|
|
|
def email_polling_errored_recently
|
|
|
|
errors = Jobs::PollMailbox.errors_in_past_24_hours
|
|
|
|
I18n.t('dashboard.email_polling_errored_recently', count: errors) if errors > 0
|
|
|
|
end
|
|
|
|
|
2013-06-19 16:11:11 -04:00
|
|
|
end
|