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.rbindex 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::StaffControllercurrent_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.rbindex 1047565469b..dee67237eb6 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -709,10 +709,25 @@ class ApplicationController < ActionController::Basefalseend+ 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_profilerMINI_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 annoyingreturn 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.rbindex 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: trueclass 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)enddef self.pool_size@@ -31,7 +31,19 @@ class AdminDashboardSectionLoader@end_date = end_dateend- 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_parallelresults = Queue.newsection_ids.each do |id|@@ -49,16 +61,7 @@ class AdminDashboardSectionLoadersection_ids.size.times doresult = 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]] = resultend@@ -66,9 +69,23 @@ class AdminDashboardSectionLoadersection_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 }+ enddef section_data(id, user)case id