Class: AdminDashboardData

Inherits:
Object
  • Object
show all
Includes:
StatsCacheable
Defined in:
app/models/admin_dashboard_data.rb

Defined Under Namespace

Classes: Problem

Constant Summary collapse

PROBLEM_MESSAGE_PREFIX =
"admin-problem:"
SCHEDULED_PROBLEM_STORAGE_KEY =
"admin-found-scheduled-problems"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ AdminDashboardData

Returns a new instance of AdminDashboardData.



40
41
42
# File 'app/models/admin_dashboard_data.rb', line 40

def initialize(opts = {})
  @opts = opts
end

Class Method Details

.add_found_scheduled_check_problem(problem) ⇒ Object



87
88
89
90
91
92
93
94
# File 'app/models/admin_dashboard_data.rb', line 87

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
  problems << problem
  set_found_scheduled_check_problems(problems)
end

.add_problem_check(*syms, &blk) ⇒ Object



78
79
80
81
# File 'app/models/admin_dashboard_data.rb', line 78

def self.add_problem_check(*syms, &blk)
  @@problem_syms.push(*syms) if syms
  @@problem_blocks << blk if blk
end

.add_problem_message(i18n_key, expire_seconds = nil) ⇒ Object

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.



266
267
268
269
270
271
272
# File 'app/models/admin_dashboard_data.rb', line 266

def self.add_problem_message(i18n_key, expire_seconds = nil)
  if expire_seconds.to_i > 0
    Discourse.redis.setex problem_message_key(i18n_key), expire_seconds.to_i, 1
  else
    Discourse.redis.set problem_message_key(i18n_key), 1
  end
end

.add_scheduled_problem_check(check_identifier, &blk) ⇒ Object



83
84
85
# File 'app/models/admin_dashboard_data.rb', line 83

def self.add_scheduled_problem_check(check_identifier, &blk)
  @@problem_scheduled_check_blocks[check_identifier] = blk
end

.clear_found_problem(identifier) ⇒ Object



104
105
106
107
108
# File 'app/models/admin_dashboard_data.rb', line 104

def self.clear_found_problem(identifier)
  problems = load_found_scheduled_check_problems
  problems.reject! { |p| p.identifier == identifier }
  set_found_scheduled_check_problems(problems)
end

.clear_found_scheduled_check_problemsObject



100
101
102
# File 'app/models/admin_dashboard_data.rb', line 100

def self.clear_found_scheduled_check_problems
  Discourse.redis.del(SCHEDULED_PROBLEM_STORAGE_KEY)
end

.clear_problem_message(i18n_key) ⇒ Object



274
275
276
# File 'app/models/admin_dashboard_data.rb', line 274

def self.clear_problem_message(i18n_key)
  Discourse.redis.del problem_message_key(i18n_key)
end

.clear_problems_startedObject



240
241
242
# File 'app/models/admin_dashboard_data.rb', line 240

def self.clear_problems_started
  Discourse.redis.del problems_started_key
end

.execute_scheduled_checksObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/models/admin_dashboard_data.rb', line 147

def self.execute_scheduled_checks
  found_problems = []
  problem_scheduled_check_blocks.each do |check_identifier, blk|
    problems = nil

    begin
      problems = instance_exec(&blk)
    rescue StandardError => err
      Discourse.warn_exception(
        err,
        message: "A scheduled admin dashboard problem check (#{check_identifier}) errored.",
      )
      # we don't want to hold up other checks because this one errored
      next
    end

    found_problems += Array.wrap(problems)
  end

  found_problems.compact.each do |problem|
    next if !problem.is_a?(Problem)
    add_found_scheduled_check_problem(problem)
  end
end

.fetch_problems(opts = {}) ⇒ Object



249
250
251
# File 'app/models/admin_dashboard_data.rb', line 249

def self.fetch_problems(opts = {})
  new(opts).problems
end

.fetch_statsObject



219
220
221
# File 'app/models/admin_dashboard_data.rb', line 219

def self.fetch_stats
  new.as_json
end

.load_found_scheduled_check_problemsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/admin_dashboard_data.rb', line 110

def self.load_found_scheduled_check_problems
  found_problems_json = Discourse.redis.get(SCHEDULED_PROBLEM_STORAGE_KEY)
  return [] if found_problems_json.blank?
  begin
    JSON.parse(found_problems_json).map { |problem| Problem.from_h(problem) }
  rescue JSON::ParserError => err
    Discourse.warn_exception(
      err,
      message: "Error parsing found problem JSON in admin dashboard: #{found_problems_json}",
    )
    []
  end
end

.problem_message_check(i18n_key) ⇒ Object



253
254
255
256
257
258
259
# File 'app/models/admin_dashboard_data.rb', line 253

def self.problem_message_check(i18n_key)
  if Discourse.redis.get(problem_message_key(i18n_key))
    I18n.t(i18n_key, base_path: Discourse.base_path)
  else
    nil
  end
end

.problem_message_key(i18n_key) ⇒ Object



278
279
280
# File 'app/models/admin_dashboard_data.rb', line 278

def self.problem_message_key(i18n_key)
  "#{PROBLEM_MESSAGE_PREFIX}#{i18n_key}"
end

.problems_started_atObject



244
245
246
247
# File 'app/models/admin_dashboard_data.rb', line 244

def self.problems_started_at
  s = Discourse.redis.get(problems_started_key)
  s ? Time.zone.parse(s) : nil
end

.problems_started_keyObject



231
232
233
# File 'app/models/admin_dashboard_data.rb', line 231

def self.problems_started_key
  "dash-problems-started-at"
end

.register_default_scheduled_problem_checksObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/admin_dashboard_data.rb', line 124

def self.register_default_scheduled_problem_checks
  add_scheduled_problem_check(:group_smtp_credentials) do
    problems = GroupEmailCredentialsCheck.run
    problems.map do |p|
      problem_message =
        I18n.t(
          "dashboard.group_email_credentials_warning",
          {
            base_path: Discourse.base_path,
            group_name: p[:group_name],
            group_full_name: p[:group_full_name],
            error: p[:message],
          },
        )
      Problem.new(
        problem_message,
        priority: "high",
        identifier: "group_#{p[:group_id]}_email_credentials",
      )
    end
  end
end

.reports(source) ⇒ Object



223
224
225
# File 'app/models/admin_dashboard_data.rb', line 223

def self.reports(source)
  source.map { |type| Report.find(type).as_json }
end

.reset_problem_checksObject

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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/models/admin_dashboard_data.rb', line 181

def self.reset_problem_checks
  @@problem_syms = []
  @@problem_blocks = []
  @@problem_scheduled_check_blocks = {}

  @@problem_messages = %w[
    dashboard.bad_favicon_url
    dashboard.poll_pop3_timeout
    dashboard.poll_pop3_auth_error
  ]

  add_problem_check :rails_env_check,
                    :host_names_check,
                    :force_https_check,
                    :ram_check,
                    :google_oauth2_config_check,
                    :facebook_config_check,
                    :twitter_config_check,
                    :github_config_check,
                    :s3_config_check,
                    :s3_cdn_check,
                    :image_magick_check,
                    :failing_emails_check,
                    :subfolder_ends_in_slash_check,
                    :email_polling_errored_recently,
                    :out_of_date_themes,
                    :unreachable_themes,
                    :watched_words_check,
                    :google_analytics_version_check,
                    :translation_overrides_check,
                    :legacy_navigation_menu_check

  register_default_scheduled_problem_checks

  add_problem_check { sidekiq_check || queue_size_check }
end

.set_found_scheduled_check_problems(problems) ⇒ Object



96
97
98
# File 'app/models/admin_dashboard_data.rb', line 96

def self.set_found_scheduled_check_problems(problems)
  Discourse.redis.setex(SCHEDULED_PROBLEM_STORAGE_KEY, 300, JSON.dump(problems.map(&:to_h)))
end

.set_problems_startedObject



235
236
237
238
# File 'app/models/admin_dashboard_data.rb', line 235

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

.stats_cache_keyObject



227
228
229
# File 'app/models/admin_dashboard_data.rb', line 227

def self.stats_cache_key
  "dashboard-data-#{Report::SCHEMA_VERSION}"
end

Instance Method Details

#as_json(_options = nil) ⇒ Object



48
49
50
# File 'app/models/admin_dashboard_data.rb', line 48

def as_json(_options = nil)
  @json ||= get_json
end

#email_polling_errored_recentlyObject



402
403
404
405
406
407
408
409
410
411
# File 'app/models/admin_dashboard_data.rb', line 402

def email_polling_errored_recently
  errors = Jobs::PollMailbox.errors_in_past_24_hours
  if errors > 0
    I18n.t(
      "dashboard.email_polling_errored_recently",
      count: errors,
      base_path: Discourse.base_path,
    )
  end
end

#facebook_config_checkObject



318
319
320
321
322
323
# File 'app/models/admin_dashboard_data.rb', line 318

def facebook_config_check
  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
end

#failing_emails_checkObject



383
384
385
386
387
388
389
390
391
392
# File 'app/models/admin_dashboard_data.rb', line 383

def failing_emails_check
  num_failed_jobs = Jobs.num_email_retry_jobs
  if num_failed_jobs > 0
    I18n.t(
      "dashboard.failing_emails_warning",
      num_failed_jobs: num_failed_jobs,
      base_path: Discourse.base_path,
    )
  end
end

#force_https_checkObject



420
421
422
423
424
425
# File 'app/models/admin_dashboard_data.rb', line 420

def force_https_check
  return unless @opts[:check_force_https]
  unless SiteSetting.force_https
    I18n.t("dashboard.force_https_warning", base_path: Discourse.base_path)
  end
end

#get_jsonObject



44
45
46
# File 'app/models/admin_dashboard_data.rb', line 44

def get_json
  {}
end

#github_config_checkObject



332
333
334
335
336
337
# File 'app/models/admin_dashboard_data.rb', line 332

def github_config_check
  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
end

#google_analytics_version_checkObject



398
399
400
# File 'app/models/admin_dashboard_data.rb', line 398

def google_analytics_version_check
  I18n.t("dashboard.v3_analytics_deprecated") if SiteSetting.ga_version == "v3_analytics"
end

#google_oauth2_config_checkObject



308
309
310
311
312
313
314
315
316
# File 'app/models/admin_dashboard_data.rb', line 308

def google_oauth2_config_check
  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
end

#host_names_checkObject



286
287
288
289
290
# File 'app/models/admin_dashboard_data.rb', line 286

def host_names_check
  if %w[localhost production.localhost].include?(Discourse.current_hostname)
    I18n.t("dashboard.host_names_warning")
  end
end

#image_magick_checkObject



377
378
379
380
381
# File 'app/models/admin_dashboard_data.rb', line 377

def image_magick_check
  if SiteSetting.create_thumbnails && !system("command -v convert >/dev/null;")
    I18n.t("dashboard.image_magick_warning")
  end
end

#legacy_navigation_menu_checkObject



371
372
373
374
375
# File 'app/models/admin_dashboard_data.rb', line 371

def legacy_navigation_menu_check
  if SiteSetting.navigation_menu == "legacy"
    I18n.t("dashboard.legacy_navigation_menu_deprecated", base_path: Discourse.base_path)
  end
end

#missing_mailgun_api_keyObject



413
414
415
416
417
418
# File 'app/models/admin_dashboard_data.rb', line 413

def missing_mailgun_api_key
  return unless SiteSetting.reply_by_email_enabled
  return unless ActionMailer::Base.smtp_settings[:address]["smtp.mailgun.org"]
  return unless SiteSetting.mailgun_api_key.blank?
  I18n.t("dashboard.missing_mailgun_api_key")
end

#out_of_date_themesObject



443
444
445
446
447
448
# File 'app/models/admin_dashboard_data.rb', line 443

def out_of_date_themes
  old_themes = RemoteTheme.out_of_date_themes
  return unless old_themes.present?

  themes_html_format(old_themes, "dashboard.out_of_date_themes")
end

#problemsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/admin_dashboard_data.rb', line 52

def problems
  problems = []
  self.class.problem_syms.each do |sym|
    message = public_send(sym)
    problems << Problem.new(message) if message.present?
  end
  self.class.problem_blocks.each do |blk|
    message = instance_exec(&blk)
    problems << Problem.new(message) if message.present?
  end
  self.class.problem_messages.each do |i18n_key|
    message = self.class.problem_message_check(i18n_key)
    problems << Problem.new(message) if message.present?
  end
  problems += self.class.load_found_scheduled_check_problems
  problems.compact!

  if problems.empty?
    self.class.clear_problems_started
  else
    self.class.set_problems_started
  end

  problems
end

#queue_size_checkObject



299
300
301
302
# File 'app/models/admin_dashboard_data.rb', line 299

def queue_size_check
  queue_size = Jobs.queued
  I18n.t("dashboard.queue_size_warning", queue_size: queue_size) if queue_size >= 100_000
end

#rails_env_checkObject



282
283
284
# File 'app/models/admin_dashboard_data.rb', line 282

def rails_env_check
  I18n.t("dashboard.rails_env_warning", env: Rails.env) unless Rails.env.production?
end

#ram_checkObject



304
305
306
# File 'app/models/admin_dashboard_data.rb', line 304

def ram_check
  I18n.t("dashboard.memory_warning") if MemInfo.new.mem_total && MemInfo.new.mem_total < 950_000
end

#s3_cdn_checkObject



358
359
360
361
362
363
# File 'app/models/admin_dashboard_data.rb', line 358

def s3_cdn_check
  if (GlobalSetting.use_s3? || SiteSetting.enable_s3_uploads) &&
       SiteSetting.Upload.s3_cdn_url.blank?
    I18n.t("dashboard.s3_cdn_warning")
  end
end

#s3_config_checkObject



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'app/models/admin_dashboard_data.rb', line 339

def s3_config_check
  # 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

    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
  end
  nil
end

#sidekiq_checkObject



292
293
294
295
296
297
# File 'app/models/admin_dashboard_data.rb', line 292

def sidekiq_check
  last_job_performed_at = Jobs.last_job_performed_at
  if Jobs.queued > 0 && (last_job_performed_at.nil? || last_job_performed_at < 2.minutes.ago)
    I18n.t("dashboard.sidekiq_warning")
  end
end

#subfolder_ends_in_slash_checkObject



394
395
396
# File 'app/models/admin_dashboard_data.rb', line 394

def subfolder_ends_in_slash_check
  I18n.t("dashboard.subfolder_ends_in_slash") if Discourse.base_path =~ %r{/\z}
end

#translation_overrides_checkObject



365
366
367
368
369
# File 'app/models/admin_dashboard_data.rb', line 365

def translation_overrides_check
  if TranslationOverride.exists?(status: %i[outdated invalid_interpolation_keys])
    I18n.t("dashboard.outdated_translations_warning", base_path: Discourse.base_path)
  end
end

#twitter_config_checkObject



325
326
327
328
329
330
# File 'app/models/admin_dashboard_data.rb', line 325

def twitter_config_check
  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
end

#unreachable_themesObject



450
451
452
453
454
455
# File 'app/models/admin_dashboard_data.rb', line 450

def unreachable_themes
  themes = RemoteTheme.unreachable_themes
  return unless themes.present?

  themes_html_format(themes, "dashboard.unreachable_themes")
end

#watched_words_checkObject



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'app/models/admin_dashboard_data.rb', line 427

def watched_words_check
  WatchedWord.actions.keys.each do |action|
    begin
      WordWatcher.word_matcher_regexp_list(action, raise_errors: true)
    rescue RegexpError => e
      translated_action = I18n.t("admin_js.admin.watched_words.actions.#{action}")
      I18n.t(
        "dashboard.watched_word_regexp_error",
        base_path: Discourse.base_path,
        action: translated_action,
      )
    end
  end
  nil
end