Files
Joas Schilling 29e49cec08 perf(setupchecks): Don't load all tasks to calculate stats
Assisted-by: ClaudeCode:claude-opus-5
Signed-off-by: Joas Schilling <coding@schilljs.com>
2026-09-20 23:32:47 +02:00

80 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Settings\SetupChecks;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IL10N;
use OCP\SetupCheck\ISetupCheck;
use OCP\SetupCheck\SetupResult;
use OCP\TaskProcessing\IManager;
use OCP\TaskProcessing\Task;
class TaskProcessingSuccessRate implements ISetupCheck {
public const MAX_FAILURE_PERCENTAGE = 0.1;
public const MAX_DAYS = 14;
public function __construct(
private IL10N $l10n,
private IManager $taskProcessingManager,
private ITimeFactory $timeFactory,
) {
}
#[\Override]
public function getCategory(): string {
return 'ai';
}
#[\Override]
public function getName(): string {
return $this->l10n->t('Task Processing success rate');
}
#[\Override]
public function run(): SetupResult {
$lastNDays = 0;
do {
$lastNDays++;
$scheduleAfter = $this->timeFactory->now()->getTimestamp() - (60 * 60 * 24 * $lastNDays);
$taskCount = $this->taskProcessingManager->countTasks(scheduleAfter: $scheduleAfter);
} while ($taskCount === 0 && $lastNDays < self::MAX_DAYS);
if ($taskCount === 0) {
return SetupResult::success(
$this->l10n->n(
'No scheduled tasks in the last %n day.',
'No scheduled tasks in the last %n days.',
$lastNDays
)
);
}
$failedCount = $this->taskProcessingManager->countTasks(status: Task::STATUS_FAILED, scheduleAfter: $scheduleAfter);
if (($failedCount / $taskCount) < self::MAX_FAILURE_PERCENTAGE) {
return SetupResult::success(
$this->l10n->n(
'Most tasks were successful in the last %n day.',
'Most tasks were successful in the last %n days.',
$lastNDays
)
);
} else {
return SetupResult::warning(
$this->l10n->n(
'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.',
'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.',
$lastNDays
),
'https://docs.nextcloud.com/server/latest/admin_manual/ai/insight_and_debugging.html'
);
}
}
}