MDL-74140 reportbuilder: debugging panel while editing reports.

This commit is contained in:
Paul Holden 2022-09-12 09:55:00 +01:00
parent c6211a68b4
commit 8a657e6d23
3 changed files with 83 additions and 8 deletions

View File

@ -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);
}
}

View File

@ -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();

View File

@ -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 <http://www.gnu.org/licenses/>.
}}
{{!
@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"
}
}}
<details>
<summary>{{#str}} debuginfo, core_debug {{/str}}</summary>
<code class="d-block mb-2">{{query}}</code>
{{#params}}
<code class="d-block mb-2">{{param}} => {{value}}</code>
{{/params}}
{{#duration}}<code class="d-block">{{.}}</code>{{/duration}}
</details>