|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Settings\SetupChecks; |
| 11 | + |
| 12 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 13 | +use OCP\IL10N; |
| 14 | +use OCP\SetupCheck\ISetupCheck; |
| 15 | +use OCP\SetupCheck\SetupResult; |
| 16 | +use OCP\TaskProcessing\IManager; |
| 17 | +use OCP\TaskProcessing\Task; |
| 18 | + |
| 19 | +class TaskProcessingSuccessRate implements ISetupCheck { |
| 20 | + public const MAX_FAILURE_PERCENTAGE = 0.2; |
| 21 | + |
| 22 | + public const MAX_DAYS = 14; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private IL10N $l10n, |
| 26 | + private IManager $taskProcessingManager, |
| 27 | + private ITimeFactory $timeFactory, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function getCategory(): string { |
| 32 | + return 'ai'; |
| 33 | + } |
| 34 | + |
| 35 | + public function getName(): string { |
| 36 | + return $this->l10n->t('Task Processing pickup speed'); |
| 37 | + } |
| 38 | + |
| 39 | + public function run(): SetupResult { |
| 40 | + $taskCount = 0; |
| 41 | + $lastNDays = 0; |
| 42 | + while ($taskCount === 0 && $lastNDays < self::MAX_DAYS) { |
| 43 | + $lastNDays++; |
| 44 | + // userId: '' means no filter, whereas null would mean guest |
| 45 | + $tasks = $this->taskProcessingManager->getTasks(userId: '', scheduleAfter: $this->timeFactory->now()->getTimestamp() - (60 * 60 * 24 * $lastNDays)); |
| 46 | + $taskCount = count($tasks); |
| 47 | + } |
| 48 | + if ($taskCount === 0) { |
| 49 | + return SetupResult::success( |
| 50 | + $this->l10n->n( |
| 51 | + 'No scheduled tasks in the last day.', |
| 52 | + 'No scheduled tasks in the last %n days.', |
| 53 | + $lastNDays |
| 54 | + ) |
| 55 | + ); |
| 56 | + } |
| 57 | + $failedCount = 0; |
| 58 | + foreach ($tasks as $task) { |
| 59 | + if ($task->getEndedAt() === null) { |
| 60 | + continue; // task was not picked up yet |
| 61 | + } |
| 62 | + $status = $task->getStatus(); |
| 63 | + if ($status === Task::STATUS_FAILED) { |
| 64 | + $failedCount++; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (($failedCount / $taskCount) < self::MAX_FAILURE_PERCENTAGE) { |
| 69 | + return SetupResult::success( |
| 70 | + $this->l10n->n( |
| 71 | + 'Most tasks were successful in the last day.', |
| 72 | + 'Most tasks were successful in the last %n days.', |
| 73 | + $lastNDays |
| 74 | + ) |
| 75 | + ); |
| 76 | + } else { |
| 77 | + return SetupResult::warning( |
| 78 | + $this->l10n->n( |
| 79 | + 'A lot of tasks failed in the last day. Consider checking the nextcloud log for errors and investigating whether the AI provider apps have been set up correctly.', |
| 80 | + 'A lot of tasks failed in the last %n days. Consider checking the nextcloud log for errors and investigating whether the AI provider apps have been set up correctly.', |
| 81 | + $lastNDays |
| 82 | + ), |
| 83 | + 'https://docs.nextcloud.com/server/latest/admin_manual/ai/insight_and_debugging.html' |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments