From 8a657e6d23c3855a3240a7e2cf485497ff6ec63f Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 12 Sep 2022 09:55:00 +0100 Subject: [PATCH] MDL-74140 reportbuilder: debugging panel while editing reports. --- .../classes/table/base_report_table.php | 26 ++++++++---- .../classes/table/custom_report_table.php | 24 +++++++++++ .../templates/local/report/debug.mustache | 41 +++++++++++++++++++ 3 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 reportbuilder/templates/local/report/debug.mustache diff --git a/reportbuilder/classes/table/base_report_table.php b/reportbuilder/classes/table/base_report_table.php index 5f72f61bc44..47e1d344df6 100644 --- a/reportbuilder/classes/table/base_report_table.php +++ b/reportbuilder/classes/table/base_report_table.php @@ -151,14 +151,11 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl } /** - * Override parent method of the same, to make use of a recordset and avoid issues with duplicate values in the first column + * Generate suitable SQL for the table * - * @param int $pagesize - * @param bool $useinitialsbar + * @return string */ - public function query_db($pagesize, $useinitialsbar = true) { - global $DB; - + protected function get_table_sql(): string { $sql = "SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where} {$this->groupbysql}"; $sort = $this->get_sql_sort(); @@ -166,12 +163,25 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl $sql .= " ORDER BY {$sort}"; } + return $sql; + } + + /** + * Override parent method of the same, to make use of a recordset and avoid issues with duplicate values in the first column + * + * @param int $pagesize + * @param bool $useinitialsbar + */ + public function query_db($pagesize, $useinitialsbar = true): void { + global $DB; + if (!$this->is_downloading()) { $this->pagesize($pagesize, $DB->count_records_sql($this->countsql, $this->countparams)); - $this->rawdata = $DB->get_recordset_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size()); + $this->rawdata = $DB->get_recordset_sql($this->get_table_sql(), $this->sql->params, $this->get_page_start(), + $this->get_page_size()); } else { - $this->rawdata = $DB->get_recordset_sql($sql, $this->sql->params); + $this->rawdata = $DB->get_recordset_sql($this->get_table_sql(), $this->sql->params); } } diff --git a/reportbuilder/classes/table/custom_report_table.php b/reportbuilder/classes/table/custom_report_table.php index ac915fb5ecc..712ccdbec2d 100644 --- a/reportbuilder/classes/table/custom_report_table.php +++ b/reportbuilder/classes/table/custom_report_table.php @@ -44,6 +44,9 @@ class custom_report_table extends base_report_table { /** @var bool Whether report is being edited (we don't want user filters/sorting to be applied when editing) */ protected const REPORT_EDITING = true; + /** @var float $querytimestart Time we began executing table SQL */ + private $querytimestart = 0.0; + /** * Table constructor. Note that the passed unique ID value must match the pattern "custom-report-table-(\d+)" so that * dynamic updates continue to load the same report @@ -298,6 +301,26 @@ class custom_report_table extends base_report_table { echo $this->get_dynamic_table_html_end(); } + /** + * Provide additional table debugging during editing + */ + public function wrap_html_finish(): void { + global $OUTPUT; + + if ($this->editing && debugging('', DEBUG_DEVELOPER)) { + $params = array_map(static function(string $param, $value): array { + return ['param' => $param, 'value' => var_export($value, true)]; + }, array_keys($this->sql->params), $this->sql->params); + + echo $OUTPUT->render_from_template('core_reportbuilder/local/report/debug', [ + 'query' => $this->get_table_sql(), + 'params' => $params, + 'duration' => $this->querytimestart ? + format_time($this->querytimestart - microtime(true)) : null, + ]); + } + } + /** * Override get_row_cells_html to add an extra cell with the toggle button for card view. * @@ -338,6 +361,7 @@ class custom_report_table extends base_report_table { // If the live editing setting is disabled, we not need to fetch custom report data except in preview mode. if (!$this->editing || self::show_live_editing()) { + $this->querytimestart = microtime(true); $this->query_db($pagesize, $useinitialsbar); $this->build_table(); $this->close_recordset(); diff --git a/reportbuilder/templates/local/report/debug.mustache b/reportbuilder/templates/local/report/debug.mustache new file mode 100644 index 00000000000..da5facf21d2 --- /dev/null +++ b/reportbuilder/templates/local/report/debug.mustache @@ -0,0 +1,41 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template core_reportbuilder/local/report/debug + + Template for custom report debug + + Example context (json): + { + "query": "SELECT foo FROM {bar} WHERE baz = :rbparam1", + "params": [ + { + "param": "rbparam1", + "value": 42 + } + ], + "duration": "0.124 secs" + } +}} +
+ {{#str}} debuginfo, core_debug {{/str}} + {{query}} + {{#params}} + {{param}} => {{value}} + {{/params}} + {{#duration}}{{.}}{{/duration}} +