|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + */ |
| 6 | +namespace OC\Core\Command\TaskProcessing; |
| 7 | + |
| 8 | +use OC\Core\Command\Base; |
| 9 | +use OCP\TaskProcessing\IManager; |
| 10 | +use OCP\TaskProcessing\Task; |
| 11 | +use Symfony\Component\Console\Input\InputInterface; |
| 12 | +use Symfony\Component\Console\Input\InputOption; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | + |
| 15 | +class Statistics extends Base { |
| 16 | + public function __construct( |
| 17 | + protected IManager $taskProcessingManager, |
| 18 | + ) { |
| 19 | + parent::__construct(); |
| 20 | + } |
| 21 | + |
| 22 | + protected function configure() { |
| 23 | + $this |
| 24 | + ->setName('taskprocessing:task:stats') |
| 25 | + ->setDescription('get statistics for tasks') |
| 26 | + ->addOption( |
| 27 | + 'userIdFilter', |
| 28 | + 'u', |
| 29 | + InputOption::VALUE_OPTIONAL, |
| 30 | + 'only get the tasks for one user ID' |
| 31 | + ) |
| 32 | + ->addOption( |
| 33 | + 'type', |
| 34 | + 't', |
| 35 | + InputOption::VALUE_OPTIONAL, |
| 36 | + 'only get the tasks for one task type' |
| 37 | + ) |
| 38 | + ->addOption( |
| 39 | + 'appId', |
| 40 | + null, |
| 41 | + InputOption::VALUE_OPTIONAL, |
| 42 | + 'only get the tasks for one app ID' |
| 43 | + ) |
| 44 | + ->addOption( |
| 45 | + 'customId', |
| 46 | + null, |
| 47 | + InputOption::VALUE_OPTIONAL, |
| 48 | + 'only get the tasks for one custom ID' |
| 49 | + ) |
| 50 | + ->addOption( |
| 51 | + 'status', |
| 52 | + 's', |
| 53 | + InputOption::VALUE_OPTIONAL, |
| 54 | + 'only get the tasks that have a specific status (STATUS_UNKNOWN=0, STATUS_SCHEDULED=1, STATUS_RUNNING=2, STATUS_SUCCESSFUL=3, STATUS_FAILED=4, STATUS_CANCELLED=5)' |
| 55 | + ) |
| 56 | + ->addOption( |
| 57 | + 'scheduledAfter', |
| 58 | + null, |
| 59 | + InputOption::VALUE_OPTIONAL, |
| 60 | + 'only get the tasks that were scheduled after a specific date (Unix timestamp)' |
| 61 | + ) |
| 62 | + ->addOption( |
| 63 | + 'endedBefore', |
| 64 | + null, |
| 65 | + InputOption::VALUE_OPTIONAL, |
| 66 | + 'only get the tasks that ended before a specific date (Unix timestamp)' |
| 67 | + ); |
| 68 | + parent::configure(); |
| 69 | + } |
| 70 | + |
| 71 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 72 | + $userIdFilter = $input->getOption('userIdFilter'); |
| 73 | + if ($userIdFilter === null) { |
| 74 | + $userIdFilter = ''; |
| 75 | + } elseif ($userIdFilter === '') { |
| 76 | + $userIdFilter = null; |
| 77 | + } |
| 78 | + $type = $input->getOption('type'); |
| 79 | + $appId = $input->getOption('appId'); |
| 80 | + $customId = $input->getOption('customId'); |
| 81 | + $status = $input->getOption('status'); |
| 82 | + $scheduledAfter = $input->getOption('scheduledAfter'); |
| 83 | + $endedBefore = $input->getOption('endedBefore'); |
| 84 | + |
| 85 | + $tasks = $this->taskProcessingManager->getTasks($userIdFilter, $type, $appId, $customId, $status, $scheduledAfter, $endedBefore); |
| 86 | + |
| 87 | + $stats = ['Number of tasks' => count($tasks)]; |
| 88 | + |
| 89 | + $maxRunningTime = 0; |
| 90 | + $totalRunningTime = 0; |
| 91 | + $runningTimeCount = 0; |
| 92 | + |
| 93 | + $maxQueuingTime = 0; |
| 94 | + $totalQueuingTime = 0; |
| 95 | + $queuingTimeCount = 0; |
| 96 | + |
| 97 | + $maxUserWaitingTime = 0; |
| 98 | + $totalUserWaitingTime = 0; |
| 99 | + $userWaitingTimeCount = 0; |
| 100 | + |
| 101 | + $maxInputSize = 0; |
| 102 | + $maxOutputSize = 0; |
| 103 | + $inputCount = 0; |
| 104 | + $inputSum = 0; |
| 105 | + $outputCount = 0; |
| 106 | + $outputSum = 0; |
| 107 | + |
| 108 | + foreach ($tasks as $task) { |
| 109 | + // running time |
| 110 | + if ($task->getStartedAt() !== null && $task->getEndedAt() !== null) { |
| 111 | + $taskRunningTime = $task->getEndedAt() - $task->getStartedAt(); |
| 112 | + $totalRunningTime += $taskRunningTime; |
| 113 | + $runningTimeCount++; |
| 114 | + if ($taskRunningTime >= $maxRunningTime) { |
| 115 | + $maxRunningTime = $taskRunningTime; |
| 116 | + } |
| 117 | + } |
| 118 | + // queuing time |
| 119 | + if ($task->getScheduledAt() !== null && $task->getStartedAt() !== null) { |
| 120 | + $taskQueuingTime = $task->getStartedAt() - $task->getScheduledAt(); |
| 121 | + $totalQueuingTime += $taskQueuingTime; |
| 122 | + $queuingTimeCount++; |
| 123 | + if ($taskQueuingTime >= $maxQueuingTime) { |
| 124 | + $maxQueuingTime = $taskQueuingTime; |
| 125 | + } |
| 126 | + } |
| 127 | + // user waiting time |
| 128 | + if ($task->getScheduledAt() !== null && $task->getEndedAt() !== null) { |
| 129 | + $taskUserWaitingTime = $task->getEndedAt() - $task->getScheduledAt(); |
| 130 | + $totalUserWaitingTime += $taskUserWaitingTime; |
| 131 | + $userWaitingTimeCount++; |
| 132 | + if ($taskUserWaitingTime >= $maxUserWaitingTime) { |
| 133 | + $maxUserWaitingTime = $taskUserWaitingTime; |
| 134 | + } |
| 135 | + } |
| 136 | + // input/output sizes |
| 137 | + if ($task->getStatus() === Task::STATUS_SUCCESSFUL) { |
| 138 | + $outputString = json_encode($task->getOutput()); |
| 139 | + if ($outputString !== false) { |
| 140 | + $outputCount++; |
| 141 | + $outputLength = strlen($outputString); |
| 142 | + $outputSum += $outputLength; |
| 143 | + if ($outputLength > $maxOutputSize) { |
| 144 | + $maxOutputSize = $outputLength; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + $inputString = json_encode($task->getInput()); |
| 149 | + if ($inputString !== false) { |
| 150 | + $inputCount++; |
| 151 | + $inputLength = strlen($inputString); |
| 152 | + $inputSum += $inputLength; |
| 153 | + if ($inputLength > $maxInputSize) { |
| 154 | + $maxInputSize = $inputLength; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if ($runningTimeCount > 0) { |
| 160 | + $stats['Max running time'] = $maxRunningTime; |
| 161 | + $averageRunningTime = $totalRunningTime / $runningTimeCount; |
| 162 | + $stats['Average running time'] = (int)$averageRunningTime; |
| 163 | + $stats['Running time count'] = $runningTimeCount; |
| 164 | + } |
| 165 | + if ($queuingTimeCount > 0) { |
| 166 | + $stats['Max queuing time'] = $maxQueuingTime; |
| 167 | + $averageQueuingTime = $totalQueuingTime / $queuingTimeCount; |
| 168 | + $stats['Average queuing time'] = (int)$averageQueuingTime; |
| 169 | + $stats['Queuing time count'] = $queuingTimeCount; |
| 170 | + } |
| 171 | + if ($userWaitingTimeCount > 0) { |
| 172 | + $stats['Max user waiting time'] = $maxUserWaitingTime; |
| 173 | + $averageUserWaitingTime = $totalUserWaitingTime / $userWaitingTimeCount; |
| 174 | + $stats['Average user waiting time'] = (int)$averageUserWaitingTime; |
| 175 | + $stats['User waiting time count'] = $userWaitingTimeCount; |
| 176 | + } |
| 177 | + if ($outputCount > 0) { |
| 178 | + $stats['Max output size (bytes)'] = $maxOutputSize; |
| 179 | + $averageOutputSize = $outputSum / $outputCount; |
| 180 | + $stats['Average output size (bytes)'] = (int)$averageOutputSize; |
| 181 | + $stats['Number of tasks with output'] = $outputCount; |
| 182 | + } |
| 183 | + if ($inputCount > 0) { |
| 184 | + $stats['Max input size (bytes)'] = $maxInputSize; |
| 185 | + $averageInputSize = $inputSum / $inputCount; |
| 186 | + $stats['Average input size (bytes)'] = (int)$averageInputSize; |
| 187 | + $stats['Number of tasks with input'] = $inputCount; |
| 188 | + } |
| 189 | + |
| 190 | + $this->writeArrayInOutputFormat($input, $output, $stats); |
| 191 | + return 0; |
| 192 | + } |
| 193 | +} |
0 commit comments