From 79dbcce8b1d91edfab84dbbedff683eb8a455ad3 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Fri, 20 Mar 2020 21:42:55 +1100 Subject: [PATCH] MDL-68185 report_performance: Refactored into Check API --- lib/classes/check/manager.php | 31 +- lib/classes/check/performance/backups.php | 84 ++++++ lib/classes/check/performance/cachejs.php | 81 +++++ lib/classes/check/performance/debugging.php | 81 +++++ .../check/performance/designermode.php | 82 +++++ lib/classes/check/performance/stats.php | 81 +++++ report/performance/index.php | 60 +--- report/performance/locallib.php | 283 ------------------ report/performance/version.php | 2 +- 9 files changed, 449 insertions(+), 336 deletions(-) create mode 100644 lib/classes/check/performance/backups.php create mode 100644 lib/classes/check/performance/cachejs.php create mode 100644 lib/classes/check/performance/debugging.php create mode 100644 lib/classes/check/performance/designermode.php create mode 100644 lib/classes/check/performance/stats.php delete mode 100644 report/performance/locallib.php diff --git a/lib/classes/check/manager.php b/lib/classes/check/manager.php index cb99ff49200..62cc21cdb9d 100644 --- a/lib/classes/check/manager.php +++ b/lib/classes/check/manager.php @@ -40,7 +40,7 @@ class manager { /** * The list of valid check types */ - public const TYPES = ['status', 'security']; + public const TYPES = ['status', 'security', 'performance']; /** * Return all status checks @@ -57,6 +57,35 @@ class manager { return $checks; } + /** + * Return all performance checks + * + * @return array of check objects + */ + static public function get_performance_checks() : array { + $checks = [ + new performance\designermode(), + new performance\cachejs(), + new performance\debugging(), + new performance\backups(), + new performance\stats(), + ]; + + // Any plugin can add status checks to this report by implementing a callback + // _status_checks() which returns a check object. + $morechecks = get_plugins_with_function('performance_checks', 'lib.php'); + foreach ($morechecks as $plugintype => $plugins) { + foreach ($plugins as $plugin => $pluginfunction) { + $result = $pluginfunction(); + foreach ($result as $check) { + $check->component = $plugintype . '_' . $plugin; + $checks[] = $check; + } + } + } + return $checks; + } + /** * Return all status checks * diff --git a/lib/classes/check/performance/backups.php b/lib/classes/check/performance/backups.php new file mode 100644 index 00000000000..549e20d1e7f --- /dev/null +++ b/lib/classes/check/performance/backups.php @@ -0,0 +1,84 @@ +. + +/** + * Backups check + * + * @package core + * @category check + * @copyright 2020 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\check\performance; + +defined('MOODLE_INTERNAL') || die(); + +use core\check\check; +use core\check\result; + +/** + * Backups check + * + * @copyright 2020 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class backups extends check { + + /** + * Get the short check name + * + * @return string + */ + public function get_name(): string { + return get_string('check_backup', 'report_performance'); + } + + /** + * A link to a place to action this + * + * @return action_link|null + */ + public function get_action_link(): ?\action_link { + return new \action_link( + new \moodle_url('/admin/settings.php', ['section' => 'automated']), + get_string('automatedsetup', 'backup')); + } + + /** + * Return result + * @return result + */ + public function get_result(): result { + global $CFG; + + require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php'); + + $automatedbackupsenabled = get_config('backup', 'backup_auto_active'); + if ($automatedbackupsenabled == \backup_cron_automated_helper::AUTO_BACKUP_ENABLED) { + $status = result::WARNING; + $summary = get_string('check_backup_comment_enable', 'report_performance'); + } else { + $status = result::OK; + $summary = get_string('check_backup_comment_disable', 'report_performance'); + } + + $details = get_string('check_backup_details', 'report_performance'); + + return new result($status, $summary, $details); + } +} + diff --git a/lib/classes/check/performance/cachejs.php b/lib/classes/check/performance/cachejs.php new file mode 100644 index 00000000000..091c89afc95 --- /dev/null +++ b/lib/classes/check/performance/cachejs.php @@ -0,0 +1,81 @@ +. + +/** + * CacheJS check + * + * @package core + * @category check + * @copyright 2020 Brendan Heywood + * @copyright 2008 petr Skoda + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\check\performance; + +defined('MOODLE_INTERNAL') || die(); + +use core\check\check; +use core\check\result; + +/** + * CacheJS check + * + * @copyright 2020 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class cachejs extends check { + + /** + * Get the short check name + * + * @return string + */ + public function get_name(): string { + return get_string('cachejs', 'admin'); + } + + /** + * A link to a place to action this + * + * @return action_link|null + */ + public function get_action_link(): ?\action_link { + return new \action_link( + new \moodle_url('/admin/search.php', ['query' => 'cachejs']), + get_string('cachejs', 'admin')); + } + + /** + * Return result + * @return result + */ + public function get_result(): result { + global $CFG; + + if (empty($CFG->cachejs)) { + $status = result::CRITICAL; + $summary = get_string('check_cachejs_comment_disable', 'report_performance'); + } else { + $status = result::OK; + $summary = get_string('check_cachejs_comment_enable', 'report_performance'); + } + + $details = get_string('check_cachejs_details', 'report_performance'); + return new result($status, $summary, $details); + } +} + diff --git a/lib/classes/check/performance/debugging.php b/lib/classes/check/performance/debugging.php new file mode 100644 index 00000000000..14bc10a89b3 --- /dev/null +++ b/lib/classes/check/performance/debugging.php @@ -0,0 +1,81 @@ +. + +/** + * Debugging check + * + * @package core + * @copyright 2020 Brendan Heywood + * @copyright 2008 petr Skoda + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\check\performance; + +defined('MOODLE_INTERNAL') || die(); + +use core\check\check; +use core\check\result; + +/** + * Debugging check + * + * @copyright 2020 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class debugging extends check { + + /** + * Get the short check name + * + * @return string + */ + public function get_name(): string { + return get_string('debug', 'admin'); + } + + /** + * A link to a place to action this + * + * @return action_link|null + */ + public function get_action_link(): ?\action_link { + return new \action_link( + new \moodle_url('/admin/settings.php', ['section' => 'debugging']), + get_string('debug', 'admin')); + } + + /** + * Return result + * @return result + */ + public function get_result(): result { + global $CFG; + + if (!$CFG->debugdeveloper) { + $status = result::OK; + $summary = get_string('check_debugmsg_comment_nodeveloper', 'report_performance'); + } else { + $status = result::WARNING; + $summary = get_string('check_debugmsg_comment_developer', 'report_performance'); + } + + $details = get_string('check_debugmsg_details', 'report_performance'); + + return new result($status, $summary, $details); + } +} + diff --git a/lib/classes/check/performance/designermode.php b/lib/classes/check/performance/designermode.php new file mode 100644 index 00000000000..2703c11bfff --- /dev/null +++ b/lib/classes/check/performance/designermode.php @@ -0,0 +1,82 @@ +. + +/** + * Designer mode + * + * @package core + * @category check + * @copyright 2020 Brendan Heywood + * @copyright 2008 petr Skoda + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\check\performance; + +defined('MOODLE_INTERNAL') || die(); + +use core\check\check; +use core\check\result; + +/** + * Designer mode + * + * @copyright 2020 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class designermode extends check { + + /** + * Get the short check name + * + * @return string + */ + public function get_name(): string { + return get_string('themedesignermode', 'admin'); + } + + /** + * A link to a place to action this + * + * @return action_link|null + */ + public function get_action_link(): ?\action_link { + return new \action_link( + new \moodle_url('/admin/search.php', ['query' => 'themedesignermode']), + get_string('themedesignermode', 'admin')); + } + + /** + * Return result + * @return result + */ + public function get_result(): result { + global $DB, $CFG; + + if (empty($CFG->themedesignermode)) { + $status = result::OK; + $summary = get_string('check_themedesignermode_comment_disable', 'report_performance'); + } else { + $status = result::CRITICAL; + $summary = get_string('check_themedesignermode_comment_enable', 'report_performance'); + } + + $details = get_string('check_themedesignermode_details', 'report_performance'); + + return new result($status, $summary, $details); + } +} + diff --git a/lib/classes/check/performance/stats.php b/lib/classes/check/performance/stats.php new file mode 100644 index 00000000000..052b51cc5d4 --- /dev/null +++ b/lib/classes/check/performance/stats.php @@ -0,0 +1,81 @@ +. + +/** + * Stats check + * + * @package core + * @category check + * @copyright 2020 Brendan Heywood + * @copyright 2008 petr Skoda + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\check\performance; + +defined('MOODLE_INTERNAL') || die(); + +use core\check\check; +use core\check\result; + +/** + * Stats check + * + * @copyright 2020 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class stats extends check { + + /** + * Get the short check name + * + * @return string + */ + public function get_name(): string { + return get_string('check_riskadmin_name', 'report_security'); + } + + /** + * A link to a place to action this + * + * @return action_link|null + */ + public function get_action_link(): ?\action_link { + return new \action_link( + new \moodle_url('/admin/search.php', ['query' => 'enablestats']), + get_string('enablestats', 'admin')); + } + + /** + * Return result + * @return result + */ + public function get_result(): result { + global $CFG; + + if (!empty($CFG->enablestats)) { + $status = result::WARNING; + $summary = get_string('check_enablestats_comment_enable', 'report_performance'); + } else { + $status = result::OK; + $summary = get_string('check_enablestats_comment_disable', 'report_performance'); + } + + $details = get_string('check_enablestats_details', 'report_performance'); + return new result($status, $summary, $details); + } +} + diff --git a/report/performance/index.php b/report/performance/index.php index d3f12fd6a77..ed664f95cc1 100644 --- a/report/performance/index.php +++ b/report/performance/index.php @@ -25,64 +25,22 @@ define('NO_OUTPUT_BUFFERING', true); require('../../config.php'); -require_once($CFG->dirroot.'/report/performance/locallib.php'); require_once($CFG->libdir.'/adminlib.php'); +admin_externalpage_setup('reportperformance', '', null, '', array('pagelayout' => 'report')); -// Show detailed info about one issue only. -$issue = optional_param('issue', '', PARAM_ALPHANUMEXT); +$detail = optional_param('detail', '', PARAM_TEXT); // Show detailed info about one check only. -$reportperformance = new report_performance(); -$issues = $reportperformance->get_issue_list(); +$url = '/report/performance/index.php'; +$table = new core\check\table('performance', $url, $detail); -// Test if issue valid string. -if (array_search($issue, $issues, true) === false) { - $issue = ''; +if (!empty($table->detail)) { + $PAGE->set_docs_path(new moodle_url($url, ['detail' => $detail])); + $PAGE->navbar->add($table->detail->get_name()); } -// Print the header. -admin_externalpage_setup('reportperformance', '', null, '', array('pagelayout'=>'report')); echo $OUTPUT->header(); - echo $OUTPUT->heading(get_string('pluginname', 'report_performance')); - -$strissue = get_string('issue', 'report_performance'); -$strvalue = get_string('value', 'report_performance'); -$strcomments = get_string('comments', 'report_performance'); -$stredit = get_string('edit'); - -$table = new html_table(); -$table->head = array($strissue, $strvalue, $strcomments, $stredit); -$table->colclasses = array('mdl-left issue', 'mdl-left value', 'mdl-left comments', 'mdl-left config'); -$table->attributes = array('class' => 'admintable performancereport generaltable'); -$table->id = 'performanceissuereporttable'; -$table->data = array(); - -// Print details of one issue only. -if ($issue and ($issueresult = $reportperformance::$issue())) { - $reportperformance->add_issue_to_table($table, $issueresult, true); - - $PAGE->set_docs_path('report/security/' . $issue); - - echo html_writer::table($table); - - echo $OUTPUT->box($issueresult->details, 'generalbox boxwidthnormal boxaligncenter'); - - echo $OUTPUT->continue_button(new moodle_url('/report/performance/index.php')); -} else { - // Add Performance report description on main list page. - $morehelplink = $OUTPUT->doc_link('report/performance', get_string('morehelp', 'report_performance')); - echo $OUTPUT->box(get_string('performancereportdesc', 'report_performance', $morehelplink), 'generalbox mdl-align'); - - foreach ($issues as $issue) { - $issueresult = $reportperformance::$issue(); - if (!$issueresult) { - // Ignore this test. - continue; - } - $reportperformance->add_issue_to_table($table, $issueresult, false); - } - echo html_writer::table($table); -} - +echo $table->render($OUTPUT); echo $OUTPUT->footer(); + diff --git a/report/performance/locallib.php b/report/performance/locallib.php deleted file mode 100644 index ff11ee7b1e7..00000000000 --- a/report/performance/locallib.php +++ /dev/null @@ -1,283 +0,0 @@ -. - -/** - * This file contains classes for report_performance - * - * @package report_performance - * @copyright 2013 Rajesh Taneja - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die; - -/** - * Class defining issue result. - * - * @package report_performance - * @copyright 2013 Rajesh Taneja - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class report_performance_issue { - /** @var string issue identifier */ - public $issue; - /** @var string issue name */ - public $name; - /** @var string shown as status */ - public $statusstr; - /** @var string string defines issue status */ - public $status; - /** @var string shown as comment */ - public $comment; - /** @var string details aboout issue*/ - public $details; - /** @var string link pointing to configuration */ - public $configlink; -} - -/** - * This contains functions to get list of issues and there results. - * - * @package report_performance - * @copyright 2013 Rajesh Taneja - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class report_performance { - /** - * This is used when issue is ok and there is no impact on performance. - */ - const REPORT_PERFORMANCE_OK = 'ok'; - - /** - * This is used to notify that issue might impact performance. - */ - const REPORT_PERFORMANCE_WARNING = 'warning'; - - /** - * This is used to notify if issue is serious and will impact performance. - */ - const REPORT_PERFORMANCE_SERIOUS = 'serious'; - - /** - * This is used to notify if issue is critical and will significantly impact performance. - */ - const REPORT_PERFORMANCE_CRITICAL = 'critical'; - - /** - * Return list of performance check function list. - * - * @return array list of performance issues. - */ - public function get_issue_list() { - return array( - 'report_performance_check_themedesignermode', - 'report_performance_check_cachejs', - 'report_performance_check_debugmsg', - 'report_performance_check_automatic_backup', - 'report_performance_check_enablestats' - ); - } - - /** - * Returns document link for performance issue - * - * @param string $issue string describing issue - * @param string $name name of issue - * @return string issue link pointing to docs page. - */ - public function doc_link($issue, $name) { - global $CFG, $OUTPUT; - - if (empty($CFG->docroot)) { - return $name; - } - - return $OUTPUT->doc_link('report/performance/'.$issue, $name); - } - - /** - * Helper function to add issue details to table. - * - * @param html_table $table table in which issue details should be added - * @param report_performance_issues $issueresult issue result to be added - * @param bool $detail true if issue if displayed in detail. - */ - public function add_issue_to_table(&$table, $issueresult, $detailed = false) { - global $OUTPUT; - $statusarr = array(self::REPORT_PERFORMANCE_OK => 'badge badge-success', - self::REPORT_PERFORMANCE_WARNING => 'badge badge-warning', - self::REPORT_PERFORMANCE_SERIOUS => 'badge badge-danger', - self::REPORT_PERFORMANCE_CRITICAL => 'badge badge-danger'); - - $row = array(); - if ($detailed) { - $row[0] = $this->doc_link($issueresult->issue, $issueresult->name); - } else { - $url = new moodle_url('/report/performance/index.php', array('issue' => $issueresult->issue)); - $row[0] = html_writer::link($url, $issueresult->name); - } - $row[1] = html_writer::tag('span', $issueresult->statusstr, array('class' => $statusarr[$issueresult->status])); - $row[2] = $issueresult->comment; - if (!empty($issueresult->configlink)) { - $editicon = $OUTPUT->pix_icon('i/settings', $issueresult->issue); - $row[3] = $OUTPUT->action_link($issueresult->configlink, $editicon); - } else { - $row[3] = ''; - } - - $table->data[] = $row; - } - - /** - * Verifies if theme designer mode is enabled. - * - * @return report_performance_issue result of themedesigner issue. - */ - public static function report_performance_check_themedesignermode() { - global $CFG; - $issueresult = new report_performance_issue(); - $issueresult->issue = 'report_performance_check_themedesignermode'; - $issueresult->name = get_string('themedesignermode', 'admin'); - - if (empty($CFG->themedesignermode)) { - $issueresult->statusstr = get_string('disabled', 'report_performance'); - $issueresult->status = self::REPORT_PERFORMANCE_OK; - $issueresult->comment = get_string('check_themedesignermode_comment_disable', 'report_performance'); - } else { - $issueresult->statusstr = get_string('enabled', 'report_performance'); - $issueresult->status = self::REPORT_PERFORMANCE_CRITICAL; - $issueresult->comment = get_string('check_themedesignermode_comment_enable', 'report_performance'); - } - - $issueresult->details = get_string('check_themedesignermode_details', 'report_performance'); - $issueresult->configlink = new moodle_url('/admin/search.php', array('query' => 'themedesignermode')); - return $issueresult; - } - - /** - * Checks if javascript is cached. - * - * @return report_performance_issue result of cachejs issue. - */ - public static function report_performance_check_cachejs() { - global $CFG; - $issueresult = new report_performance_issue(); - $issueresult->issue = 'report_performance_check_cachejs'; - $issueresult->name = get_string('cachejs', 'admin'); - - if (empty($CFG->cachejs)) { - $issueresult->statusstr = get_string('disabled', 'report_performance'); - $issueresult->status = self::REPORT_PERFORMANCE_CRITICAL; - $issueresult->comment = get_string('check_cachejs_comment_disable', 'report_performance'); - } else { - $issueresult->statusstr = get_string('enabled', 'report_performance'); - $issueresult->status = self::REPORT_PERFORMANCE_OK; - $issueresult->comment = get_string('check_cachejs_comment_enable', 'report_performance'); - } - - $issueresult->details = get_string('check_cachejs_details', 'report_performance'); - $issueresult->configlink = new moodle_url('/admin/search.php', array('query' => 'cachejs')); - return $issueresult; - } - - /** - * Checks debug config. - * - * @return report_performance_issue result of debugmsg issue. - */ - public static function report_performance_check_debugmsg() { - global $CFG; - $issueresult = new report_performance_issue(); - $issueresult->issue = 'report_performance_check_debugmsg'; - $issueresult->name = get_string('debug', 'admin'); - $debugchoices = array(DEBUG_NONE => 'debugnone', - DEBUG_MINIMAL => 'debugminimal', - DEBUG_NORMAL => 'debugnormal', - DEBUG_ALL => 'debugall', - DEBUG_DEVELOPER => 'debugdeveloper'); - - $issueresult->statusstr = get_string($debugchoices[$CFG->debug], 'admin'); - if (!$CFG->debugdeveloper) { - $issueresult->status = self::REPORT_PERFORMANCE_OK; - $issueresult->comment = get_string('check_debugmsg_comment_nodeveloper', 'report_performance'); - } else { - $issueresult->status = self::REPORT_PERFORMANCE_WARNING; - $issueresult->comment = get_string('check_debugmsg_comment_developer', 'report_performance'); - } - - $issueresult->details = get_string('check_debugmsg_details', 'report_performance'); - - $issueresult->configlink = new moodle_url('/admin/settings.php', array('section' => 'debugging')); - return $issueresult; - } - - /** - * Checks automatic backup config. - * - * @return report_performance_issue result of automatic backup issue. - */ - public static function report_performance_check_automatic_backup() { - global $CFG; - require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php'); - - $issueresult = new report_performance_issue(); - $issueresult->issue = 'report_performance_check_automatic_backup'; - $issueresult->name = get_string('check_backup', 'report_performance'); - - $automatedbackupsenabled = get_config('backup', 'backup_auto_active'); - if ($automatedbackupsenabled == backup_cron_automated_helper::AUTO_BACKUP_ENABLED) { - $issueresult->statusstr = get_string('autoactiveenabled', 'backup'); - $issueresult->status = self::REPORT_PERFORMANCE_WARNING; - $issueresult->comment = get_string('check_backup_comment_enable', 'report_performance'); - } else { - if ($automatedbackupsenabled == backup_cron_automated_helper::AUTO_BACKUP_DISABLED) { - $issueresult->statusstr = get_string('autoactivedisabled', 'backup'); - } else { - $issueresult->statusstr = get_string('autoactivemanual', 'backup'); - } - $issueresult->status = self::REPORT_PERFORMANCE_OK; - $issueresult->comment = get_string('check_backup_comment_disable', 'report_performance'); - } - - $issueresult->details = get_string('check_backup_details', 'report_performance'); - $issueresult->configlink = new moodle_url('/admin/search.php', array('query' => 'backup_auto_active')); - return $issueresult; - } - - /** - * Checks if stats are enabled. - */ - public static function report_performance_check_enablestats() { - global $CFG; - $issueresult = new report_performance_issue(); - $issueresult->issue = 'report_performance_check_enablestats'; - $issueresult->name = get_string('enablestats', 'admin'); - - if (!empty($CFG->enablestats)) { - $issueresult->statusstr = get_string('enabled', 'report_performance'); - $issueresult->status = self::REPORT_PERFORMANCE_WARNING; - $issueresult->comment = get_string('check_enablestats_comment_enable', 'report_performance'); - } else { - $issueresult->statusstr = get_string('disabled', 'report_performance'); - $issueresult->status = self::REPORT_PERFORMANCE_OK; - $issueresult->comment = get_string('check_enablestats_comment_disable', 'report_performance'); - } - - $issueresult->details = get_string('check_enablestats_details', 'report_performance'); - $issueresult->configlink = new moodle_url('/admin/search.php', array('query' => 'enablestats')); - return $issueresult; - } -} diff --git a/report/performance/version.php b/report/performance/version.php index 859b3891eb0..4294d441605 100644 --- a/report/performance/version.php +++ b/report/performance/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die; -$plugin->version = 2019111800; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2019111801; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2019111200; // Requires this Moodle version. $plugin->component = 'report_performance'; // Full name of the plugin (used for diagnostics).