Skip to content

A platform for community discussion. Free, open, simple.

DEV: Serialize dashboard sections for flamegraph profiling (#41897)

Alan Guo Xiang Tan4e264b4d4d62
diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rb
index 701edd764db..2bbea264ae0 100644
--- a/app/controllers/admin/dashboard_controller.rb
+++ b/app/controllers/admin/dashboard_controller.rb
@@ -163,6 +163,7 @@ class Admin::DashboardController < Admin::StaffController
current_user: current_user,
start_date: params[:start_date],
end_date: params[:end_date],
+ parallel: !mini_profiler_flamegraph_request?,
),
problems: serialized_problems,
}
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 1047565469b..dee67237eb6 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -709,10 +709,25 @@ class ApplicationController < ActionController::Base
false
end
+ def mini_profiler_flamegraph_request?
+ return false if !mini_profiler_enabled?
+
+ mini_profiler_matches_action?("flamegraph") ||
+ mini_profiler_matches_action?("async-flamegraph") ||
+ request.referer.to_s.match?(/pp=async-flamegraph/)
+ end
+
def authorize_mini_profiler
MINI_PROFILER_CLASS.authorize_request if mini_profiler_enabled?
end
+ def mini_profiler_matches_action?(action)
+ profile_parameter = Regexp.escape(MINI_PROFILER_CLASS.config.profile_parameter)
+
+ request.query_string.match?(/#{profile_parameter}=#{Regexp.escape(action)}/) ||
+ request.get_header("HTTP_X_RACK_MINI_PROFILER") == action
+ end
+
def check_xhr
# bypass xhr check on PUT / POST / DELETE provided api key is there, otherwise calling api is annoying
return if !request.get? && (is_api? || is_user_api?)
diff --git a/app/services/admin_dashboard_section_loader.rb b/app/services/admin_dashboard_section_loader.rb
index f2c68bb0f82..0dc05d7753d 100644
--- a/app/services/admin_dashboard_section_loader.rb
+++ b/app/services/admin_dashboard_section_loader.rb
@@ -1,13 +1,13 @@
# frozen_string_literal: true
class AdminDashboardSectionLoader
- def self.build(section_ids:, current_user:, start_date:, end_date:)
+ def self.build(section_ids:, current_user:, start_date:, end_date:, parallel: true)
new(
section_ids: section_ids,
current_user: current_user,
start_date: start_date,
end_date: end_date,
- ).build
+ ).build(parallel: parallel)
end
def self.pool_size
@@ -31,7 +31,19 @@ class AdminDashboardSectionLoader
@end_date = end_date
end
- def build
+ def build(parallel: true)
+ if parallel
+ build_in_parallel
+ else
+ section_ids.map { |id| build_section(id) }
+ end
+ end
+
+ private
+
+ attr_reader :section_ids, :current_user, :start_date, :end_date
+
+ def build_in_parallel
results = Queue.new
section_ids.each do |id|
@@ -49,16 +61,7 @@ class AdminDashboardSectionLoader
section_ids.size.times do
result = results.pop
- if result[:error]
- Discourse.warn_exception(
- result[:error],
- message: "Failed to build admin dashboard section",
- env: {
- section_id: result[:id],
- },
- )
- result = { id: result[:id], data: nil, error: true }
- end
+ result = failed_result(result) if result[:error]
results_by_id[result[:id]] = result
end
@@ -66,9 +69,23 @@ class AdminDashboardSectionLoader
section_ids.map { |id| results_by_id.fetch(id) }
end
- private
+ def build_section(id)
+ { id: id, data: section_data(id, current_user) }
+ rescue StandardError => e
+ failed_result(id: id, error: e)
+ end
- attr_reader :section_ids, :current_user, :start_date, :end_date
+ def failed_result(result)
+ Discourse.warn_exception(
+ result[:error],
+ message: "Failed to build admin dashboard section",
+ env: {
+ section_id: result[:id],
+ },
+ )
+
+ { id: result[:id], data: nil, error: true }
+ end
def section_data(id, user)
case id