|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 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. |
| 8 | +// |
| 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. |
| 13 | +// |
| 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/>. |
| 16 | + |
| 17 | +namespace tool_cloudmetrics\local\task_load; |
| 18 | + |
| 19 | +use core\task\manager as taskmanager; |
| 20 | + |
| 21 | +/** |
| 22 | + * Base class for task load estimators. |
| 23 | + * |
| 24 | + * @package tool_cloudmetrics |
| 25 | + * @author Jason den Dulk <jasondendulk@catalyst-au.net> |
| 26 | + * @copyright 2026 Catalyst IT |
| 27 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 28 | + */ |
| 29 | +abstract class task_load_estimator { |
| 30 | + /** @var float Default value for the average time, in seconds. */ |
| 31 | + public const DEFAULT_AVG_TIME = 0.5; |
| 32 | + |
| 33 | + /** |
| 34 | + * Returns the object to calculate the task load estimates. |
| 35 | + * Which implementation is used depends on the availability of task statistics feature (MDL-85173). |
| 36 | + * |
| 37 | + * @return task_load_estimator |
| 38 | + */ |
| 39 | + public static function create(): task_load_estimator { |
| 40 | + if (class_exists('\core\task\stat_processor')) { |
| 41 | + return new task_load_from_stats(); |
| 42 | + } else { |
| 43 | + return new task_load_from_log(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Generate task load estimates for the near future. The load is taken from the current |
| 49 | + * time determined with a DI clock. |
| 50 | + * |
| 51 | + * @param int $window The number of seconds into the future to look. |
| 52 | + * @return array Returns an array of task load estimates (in seconds), indexed by classname. |
| 53 | + */ |
| 54 | + abstract public function generate_load_estimates(int $window): array; |
| 55 | + |
| 56 | + /** |
| 57 | + * Main function for generating the load estimates array. |
| 58 | + * |
| 59 | + * @param array $scheduled |
| 60 | + * @param array $adhoc |
| 61 | + * @param int $start |
| 62 | + * @param int $finish |
| 63 | + * @return array |
| 64 | + */ |
| 65 | + protected function generate_load_estimates_inner(array $scheduled, array $adhoc, int $start, int $finish): array { |
| 66 | + // Loads will contain the estimates. |
| 67 | + $loads = []; |
| 68 | + |
| 69 | + foreach ($scheduled as $record) { |
| 70 | + $task = taskmanager::scheduled_task_from_record($record); |
| 71 | + |
| 72 | + // If the task has already started, then we add the remaining time. |
| 73 | + if ($record->timestarted > 0) { |
| 74 | + $started = (int) $record->timestarted; |
| 75 | + $elapsed = max(0, $start - $started); |
| 76 | + $avgduration = max(0.0, $record->mean - $elapsed); |
| 77 | + } else if ($record->nextruntime + $record->mean > $finish) { |
| 78 | + $avgduration = ($finish - $record->nextruntime); |
| 79 | + } else { |
| 80 | + $avgduration = $record->mean; |
| 81 | + } |
| 82 | + $classname = taskmanager::get_canonical_class_name($task::class); |
| 83 | + $loads[$classname] = [ |
| 84 | + 'type' => 'scheduled', |
| 85 | + 'classname' => $classname, |
| 86 | + 'totalduration' => $avgduration, |
| 87 | + 'avgduration' => $avgduration, |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + foreach ($adhoc as $record) { |
| 92 | + $classname = taskmanager::get_canonical_class_name($record->classname); |
| 93 | + if (!isset($loads[$classname])) { |
| 94 | + $loads[$classname] = [ |
| 95 | + 'type' => 'adhoc', |
| 96 | + 'classname' => $classname, |
| 97 | + 'count' => 0, |
| 98 | + 'totalduration' => 0, |
| 99 | + 'avgduration' => 0, |
| 100 | + ]; |
| 101 | + } |
| 102 | + if ($record->timestarted > 0) { |
| 103 | + // Task has already started. Add remaining time. |
| 104 | + $started = (int) $record->timestarted; |
| 105 | + $elapsed = max(0, $start - $started); |
| 106 | + $remaining = max(0.0, $record->mean - $elapsed); |
| 107 | + $loads[$classname]['totalduration'] += $remaining; |
| 108 | + } else if ($record->nextruntime + $record->mean > $finish) { |
| 109 | + // Task will finish after window ends. Add time left in window. |
| 110 | + $loads[$classname]['totalduration'] += ($finish - $record->nextruntime); |
| 111 | + } else { |
| 112 | + // Task will run wholly within the window, so add the full mean. |
| 113 | + $loads[$classname]['totalduration'] += $record->mean; |
| 114 | + } |
| 115 | + ++$loads[$classname]['count']; |
| 116 | + $loads[$classname]['avgduration'] = $loads[$classname]['totalduration'] / $loads[$classname]['count']; |
| 117 | + } |
| 118 | + |
| 119 | + // Order by type then by estimated_seconds descending. |
| 120 | + usort($loads, static function ($a, $b) { |
| 121 | + $q = strcmp($a['type'], $b['type']); |
| 122 | + if ($q !== 0) { |
| 123 | + return $q; |
| 124 | + } |
| 125 | + return $b['totalduration'] <=> $a['totalduration']; |
| 126 | + }); |
| 127 | + |
| 128 | + return $loads; |
| 129 | + } |
| 130 | +} |
0 commit comments