2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
21 * @subpackage profiling
22 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // TODO: Move all the DB stuff to profiling_db_xxxx() function in xhprof_moodle.php
28 // TODO: it is wrong when core lib references ANY plugin lang strings, maybe more login could be moved here (skodak)
30 require_once(dirname(__FILE__) . '/../../../config.php');
31 require_once($CFG->libdir.'/adminlib.php');
32 require_once($CFG->libdir . '/xhprof/xhprof_moodle.php');
34 define('PROFILING_RUNSPERPAGE', 50);
37 $script = optional_param('script', null, PARAM_PATH);
38 $runid = optional_param('runid', null, PARAM_ALPHANUM);
39 $runid2 = optional_param('runid2', null, PARAM_ALPHANUM);
40 $listurl = optional_param('listurl', null, PARAM_PATH);
41 $runreference= optional_param('runreference', 0, PARAM_INT);
42 $runcomment = optional_param('runcomment', null, PARAM_TEXT);
44 $dbfields = 'runid, url, totalexecutiontime, totalcputime, ' .
45 'totalcalls, totalmemory, runreference, runcomment, timecreated';
47 admin_externalpage_setup('toolprofiling');
49 // Always add listurl if available
51 $listurlnav = new moodle_url('/admin/tool/profiling/index.php', array('listurl' => $listurl));
52 $PAGE->navbar->add($listurl, $listurlnav);
56 echo $OUTPUT->header();
58 // We have requested the last available run for one script
60 // Get the last available run for the given script
61 $run = $DB->get_record_sql("SELECT $dbfields
64 AND id = (SELECT MAX(id)
67 array($script, $script), IGNORE_MISSING);
69 // No run found for script, warn and exit
71 notice(get_string('cannotfindanyrunforurl', 'tool_profiling', $script), 'index.php');
74 // Check if there is any previous run marked as reference one
75 $prevreferences = $DB->get_records_select('profiling',
76 'url = ? AND runreference = 1 AND timecreated < ?',
77 array($run->url, $run->timecreated),
78 'timecreated DESC', 'runid', 0, 1);
79 $prevrunid = $prevreferences ? reset($prevreferences)->runid : false;
80 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
81 $header = get_string('lastrunof', 'tool_profiling', $script);
82 echo $OUTPUT->heading($header);
83 $table = profiling_print_run($run, $prevrunid);
85 echo $OUTPUT->box_end();
88 // We have requested the diff between 2 runs
89 } else if (isset($runid) && isset($runid2)) {
90 $run1 = $DB->get_record('profiling', array('runid'=>$runid), $dbfields, MUST_EXIST);
91 $run2 = $DB->get_record('profiling', array('runid'=>$runid2), $dbfields, MUST_EXIST);
92 if ($run1->url == $run2->url && $run1->runid != $run2->runid) {
93 if ($run2->timecreated < $run1->timecreated) {
98 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
99 $header = get_string('differencesbetween2runsof', 'tool_profiling', $run1->url);
100 echo $OUTPUT->heading($header);
101 $table = profiling_print_rundiff($run1, $run2);
103 echo $OUTPUT->box_end();
107 // We have requested one run, invoke it
108 } else if (isset($runid)) {
109 // Check if we are trying to update the runreference/runcomment for the run
110 if (isset($runcomment) && confirm_sesskey()) {
111 $id = $DB->get_field('profiling', 'id', array('runid' => $runid), MUST_EXIST);
112 $rec = new stdClass();
114 $rec->runreference = (bool)$runreference;
115 $rec->runcomment = $runcomment;
116 $DB->update_record('profiling', $rec);
118 // Get the requested runid
119 $run = $DB->get_record('profiling', array('runid'=>$runid), $dbfields, IGNORE_MISSING);
121 // No run found for runid, warn and exit
123 notice(get_string('cannotfindanyrunforrunid', 'tool_profiling', $runid), 'index.php');
126 // Check if there is any previous run marked as reference one
127 $prevreferences = $DB->get_records_select('profiling',
128 'url = ? AND runreference = 1 AND timecreated < ?',
129 array($run->url, $run->timecreated),
130 'timecreated DESC', 'runid', 0, 1);
131 $prevrunid = $prevreferences ? reset($prevreferences)->runid : false;
132 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
133 $header = get_string('summaryof', 'tool_profiling', $run->url);
134 echo $OUTPUT->heading($header);
135 $table = profiling_print_run($run, $prevrunid);
137 echo $OUTPUT->box_end();
140 // Default: List one page of runs
143 // The flexitable that will root listings
144 $table = new xhprof_table_sql('profiling-list-table');
145 $baseurl = $CFG->wwwroot . '/admin/tool/profiling/index.php';
147 // Check if we are listing all or some URL ones
149 $sqlparams = array();
150 if (!isset($listurl)) {
151 $header = get_string('pluginname', 'tool_profiling');
152 $sqlconditions = '1 = 1';
153 $table->set_listurlmode(false);
155 $header = get_string('profilingrunsfor', 'tool_profiling', $listurl);
156 $sqlconditions = 'url = :url';
157 $sqlparams['url'] = $listurl;
158 $table->set_listurlmode(true);
159 $baseurl .= '?listurl=' . urlencode($listurl);
162 echo $OUTPUT->heading($header);
164 // TODO: Fix flexitable to validate tsort/thide/tshow/tifirs/tilast/page
165 // TODO: Fix table_sql to allow it to work without WHERE clause
166 // add silly condition (1 = 1) because of table_sql bug
167 $table->set_sql($dbfields, '{profiling}', $sqlconditions, $sqlparams);
168 $table->set_count_sql("SELECT COUNT(*) FROM {profiling} WHERE $sqlconditions", $sqlparams);
170 'url', 'timecreated', 'totalexecutiontime', 'totalcputime',
171 'totalcalls', 'totalmemory', 'runcomment');
173 get_string('url'), get_string('date'), get_string('executiontime', 'tool_profiling'),
174 get_string('cputime', 'tool_profiling'), get_string('calls', 'tool_profiling'),
175 get_string('memory', 'tool_profiling'), get_string('comment', 'tool_profiling'));
176 $table->define_columns($columns);
177 $table->define_headers($headers);
178 $table->sortable(true, 'timecreated', SORT_DESC);
179 $table->define_baseurl($baseurl);
180 $table->column_suppress('url');
181 $table->out(PROFILING_RUNSPERPAGE, true);
183 // Print the controller block with different options
184 echo profiling_list_controls($listurl);
188 echo $OUTPUT->footer();