Completed
Push — master ( 3aae74...954b14 )
by Marcel
27:03
created
apps/settings/lib/SetupChecks/TaskProcessingSuccessRate.php 1 patch
Indentation   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -17,71 +17,71 @@
 block discarded – undo
17 17
 use OCP\TaskProcessing\Task;
18 18
 
19 19
 class TaskProcessingSuccessRate implements ISetupCheck {
20
-	public const MAX_FAILURE_PERCENTAGE = 0.2;
20
+    public const MAX_FAILURE_PERCENTAGE = 0.2;
21 21
 
22
-	public const MAX_DAYS = 14;
22
+    public const MAX_DAYS = 14;
23 23
 
24
-	public function __construct(
25
-		private IL10N $l10n,
26
-		private IManager $taskProcessingManager,
27
-		private ITimeFactory $timeFactory,
28
-	) {
29
-	}
24
+    public function __construct(
25
+        private IL10N $l10n,
26
+        private IManager $taskProcessingManager,
27
+        private ITimeFactory $timeFactory,
28
+    ) {
29
+    }
30 30
 
31
-	public function getCategory(): string {
32
-		return 'ai';
33
-	}
31
+    public function getCategory(): string {
32
+        return 'ai';
33
+    }
34 34
 
35
-	public function getName(): string {
36
-		return $this->l10n->t('Task Processing pickup speed');
37
-	}
35
+    public function getName(): string {
36
+        return $this->l10n->t('Task Processing pickup speed');
37
+    }
38 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
-		}
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 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
-	}
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 87
 }
Please login to merge, or discard this patch.
apps/settings/tests/SetupChecks/TaskProcessingSuccessRateTest.php 1 patch
Indentation   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -17,59 +17,59 @@
 block discarded – undo
17 17
 use Test\TestCase;
18 18
 
19 19
 class TaskProcessingSuccessRateTest extends TestCase {
20
-	private IL10N $l10n;
21
-	private ITimeFactory $timeFactory;
22
-	private IManager $taskProcessingManager;
20
+    private IL10N $l10n;
21
+    private ITimeFactory $timeFactory;
22
+    private IManager $taskProcessingManager;
23 23
 
24
-	private TaskProcessingSuccessRate $check;
24
+    private TaskProcessingSuccessRate $check;
25 25
 
26
-	protected function setUp(): void {
27
-		parent::setUp();
26
+    protected function setUp(): void {
27
+        parent::setUp();
28 28
 
29
-		$this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
30
-		$this->timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock();
31
-		$this->taskProcessingManager = $this->getMockBuilder(IManager::class)->getMock();
29
+        $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
30
+        $this->timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock();
31
+        $this->taskProcessingManager = $this->getMockBuilder(IManager::class)->getMock();
32 32
 
33
-		$this->check = new TaskProcessingSuccessRate(
34
-			$this->l10n,
35
-			$this->taskProcessingManager,
36
-			$this->timeFactory,
37
-		);
38
-	}
33
+        $this->check = new TaskProcessingSuccessRate(
34
+            $this->l10n,
35
+            $this->taskProcessingManager,
36
+            $this->timeFactory,
37
+        );
38
+    }
39 39
 
40
-	public function testPass(): void {
41
-		$tasks = [];
42
-		for ($i = 0; $i < 100; $i++) {
43
-			$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
44
-			$task->setStartedAt(0);
45
-			$task->setEndedAt(1);
46
-			if ($i < 15) {
47
-				$task->setStatus(Task::STATUS_FAILED); // 15% get status FAILED
48
-			} else {
49
-				$task->setStatus(Task::STATUS_SUCCESSFUL);
50
-			}
51
-			$tasks[] = $task;
52
-		}
53
-		$this->taskProcessingManager->method('getTasks')->willReturn($tasks);
40
+    public function testPass(): void {
41
+        $tasks = [];
42
+        for ($i = 0; $i < 100; $i++) {
43
+            $task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
44
+            $task->setStartedAt(0);
45
+            $task->setEndedAt(1);
46
+            if ($i < 15) {
47
+                $task->setStatus(Task::STATUS_FAILED); // 15% get status FAILED
48
+            } else {
49
+                $task->setStatus(Task::STATUS_SUCCESSFUL);
50
+            }
51
+            $tasks[] = $task;
52
+        }
53
+        $this->taskProcessingManager->method('getTasks')->willReturn($tasks);
54 54
 
55
-		$this->assertEquals(SetupResult::SUCCESS, $this->check->run()->getSeverity());
56
-	}
55
+        $this->assertEquals(SetupResult::SUCCESS, $this->check->run()->getSeverity());
56
+    }
57 57
 
58
-	public function testFail(): void {
59
-		$tasks = [];
60
-		for ($i = 0; $i < 100; $i++) {
61
-			$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
62
-			$task->setStartedAt(0);
63
-			$task->setEndedAt(1);
64
-			if ($i < 30) {
65
-				$task->setStatus(Task::STATUS_FAILED); // 30% get status FAILED
66
-			} else {
67
-				$task->setStatus(Task::STATUS_SUCCESSFUL);
68
-			}
69
-			$tasks[] = $task;
70
-		}
71
-		$this->taskProcessingManager->method('getTasks')->willReturn($tasks);
58
+    public function testFail(): void {
59
+        $tasks = [];
60
+        for ($i = 0; $i < 100; $i++) {
61
+            $task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
62
+            $task->setStartedAt(0);
63
+            $task->setEndedAt(1);
64
+            if ($i < 30) {
65
+                $task->setStatus(Task::STATUS_FAILED); // 30% get status FAILED
66
+            } else {
67
+                $task->setStatus(Task::STATUS_SUCCESSFUL);
68
+            }
69
+            $tasks[] = $task;
70
+        }
71
+        $this->taskProcessingManager->method('getTasks')->willReturn($tasks);
72 72
 
73
-		$this->assertEquals(SetupResult::WARNING, $this->check->run()->getSeverity());
74
-	}
73
+        $this->assertEquals(SetupResult::WARNING, $this->check->run()->getSeverity());
74
+    }
75 75
 }
Please login to merge, or discard this patch.