3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * @subpackage profiling
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 // TODO: Move all the DB stuff to profiling_db_xxxx() function in xhprof_moodle.php
27 require_once(dirname(__FILE__).'/../../../config.php');
28 require_once($CFG->libdir.'/adminlib.php');
29 require_once($CFG->libdir . '/xhprof/xhprof_moodle.php');
31 define('PROFILING_RUNSPERPAGE', 50);
34 $script = optional_param('script', null, PARAM_PATH);
35 $runid = optional_param('runid', null, PARAM_ALPHANUM);
36 $runid2 = optional_param('runid2', null, PARAM_ALPHANUM);
37 $listurl = optional_param('listurl', null, PARAM_PATH);
38 $runreference= optional_param('runreference', 0, PARAM_INT);
39 $runcomment = optional_param('runcomment', null, PARAM_TEXT);
41 $dbfields = 'runid, url, totalexecutiontime, totalcputime, ' .
42 'totalcalls, totalmemory, runreference, runcomment, timecreated';
44 admin_externalpage_setup('reportprofiling');
46 // Always add listurl if available
48 $listurlnav = new moodle_url('/admin/report/profiling/index.php', array('listurl' => $listurl));
49 $PAGE->navbar->add($listurl, $listurlnav);
53 echo $OUTPUT->header();
55 // We have requested the last available run for one script
57 // Get the last available run for the given script
58 $run = $DB->get_record_sql("SELECT $dbfields
61 AND id = (SELECT MAX(id)
64 array($script, $script), IGNORE_MISSING);
66 // No run found for script, warn and exit
68 notice(get_string('cannotfindanyrunforurl', 'report_profiling', $script), 'index.php');
71 // Check if there is any previous run marked as reference one
72 $prevreferences = $DB->get_records_select('profiling',
73 'url = ? AND runreference = 1 AND timecreated < ?',
74 array($run->url, $run->timecreated),
75 'timecreated DESC', 'runid', 0, 1);
76 $prevrunid = $prevreferences ? reset($prevreferences)->runid : false;
77 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
78 $header = get_string('lastrunof', 'report_profiling', $script);
79 echo $OUTPUT->heading($header);
80 $table = profiling_print_run($run, $prevrunid);
82 echo $OUTPUT->box_end();
85 // We have requested the diff between 2 runs
86 } else if (isset($runid) && isset($runid2)) {
87 $run1 = $DB->get_record('profiling', array('runid'=>$runid), $dbfields, MUST_EXIST);
88 $run2 = $DB->get_record('profiling', array('runid'=>$runid2), $dbfields, MUST_EXIST);
89 if ($run1->url == $run2->url && $run1->runid != $run2->runid) {
90 if ($run2->timecreated < $run1->timecreated) {
95 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
96 $header = get_string('differencesbetween2runsof', 'report_profiling', $run1->url);
97 echo $OUTPUT->heading($header);
98 $table = profiling_print_rundiff($run1, $run2);
100 echo $OUTPUT->box_end();
104 // We have requested one run, invoke it
105 } else if (isset($runid)) {
106 // Check if we are trying to update the runreference/runcomment for the run
107 if (isset($runcomment) && confirm_sesskey()) {
108 $id = $DB->get_field('profiling', 'id', array('runid' => $runid), MUST_EXIST);
109 $rec = new stdClass();
111 $rec->runreference = (bool)$runreference;
112 $rec->runcomment = $runcomment;
113 $DB->update_record('profiling', $rec);
115 // Get the requested runid
116 $run = $DB->get_record('profiling', array('runid'=>$runid), $dbfields, IGNORE_MISSING);
118 // No run found for runid, warn and exit
120 notice(get_string('cannotfindanyrunforrunid', 'report_profiling', $runid), 'index.php');
123 // Check if there is any previous run marked as reference one
124 $prevreferences = $DB->get_records_select('profiling',
125 'url = ? AND runreference = 1 AND timecreated < ?',
126 array($run->url, $run->timecreated),
127 'timecreated DESC', 'runid', 0, 1);
128 $prevrunid = $prevreferences ? reset($prevreferences)->runid : false;
129 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
130 $header = get_string('summaryof', 'report_profiling', $run->url);
131 echo $OUTPUT->heading($header);
132 $table = profiling_print_run($run, $prevrunid);
134 echo $OUTPUT->box_end();
137 // Default: List one page of runs
140 // The flexitable that will root listings
141 $table = new xhprof_table_sql('profiling-list-table');
142 $baseurl = $CFG->wwwroot . '/admin/report/profiling/index.php';
144 // Check if we are listing all or some URL ones
146 $sqlparams = array();
147 if (!isset($listurl)) {
148 $header = get_string('pluginname', 'report_profiling');
149 $sqlconditions = '1 = 1';
150 $table->set_listurlmode(false);
152 $header = get_string('profilingrunsfor', 'report_profiling', $listurl);
153 $sqlconditions = 'url = :url';
154 $sqlparams['url'] = $listurl;
155 $table->set_listurlmode(true);
156 $baseurl .= '?listurl=' . urlencode($listurl);
159 echo $OUTPUT->heading($header);
161 // TODO: Fix flexitable to validate tsort/thide/tshow/tifirs/tilast/page
162 // TODO: Fix table_sql to allow it to work without WHERE clause
163 // add silly condition (1 = 1) because of table_sql bug
164 $table->set_sql($dbfields, '{profiling}', $sqlconditions, $sqlparams);
165 $table->set_count_sql("SELECT COUNT(*) FROM {profiling} WHERE $sqlconditions", $sqlparams);
167 'url', 'timecreated', 'totalexecutiontime', 'totalcputime',
168 'totalcalls', 'totalmemory', 'runcomment');
170 get_string('url'), get_string('date'), get_string('executiontime', 'report_profiling'),
171 get_string('cputime', 'report_profiling'), get_string('calls', 'report_profiling'),
172 get_string('memory', 'report_profiling'), get_string('comment', 'report_profiling'));
173 $table->define_columns($columns);
174 $table->define_headers($headers);
175 $table->sortable(true, 'timecreated', SORT_DESC);
176 $table->define_baseurl($baseurl);
177 $table->column_suppress('url');
178 $table->out(PROFILING_RUNSPERPAGE, true);
180 // Print the controller block with different options
181 echo profiling_list_controls($listurl);
185 echo $OUTPUT->footer();