From 57373c846a11711431465e3bce8d9ee219e7330b Mon Sep 17 00:00:00 2001 From: Xu Date: Thu, 28 Dec 2023 16:26:56 +0100 Subject: [PATCH] Metrics: Added not arrived users --- includes/pages/admin_user.php | 2 +- src/Controllers/Metrics/Controller.php | 15 +++++++---- src/Controllers/Metrics/Stats.php | 14 +++------- .../Controllers/Metrics/ControllerTest.php | 14 +++++----- tests/Unit/Controllers/Metrics/StatsTest.php | 27 +++++++------------ 5 files changed, 30 insertions(+), 42 deletions(-) diff --git a/includes/pages/admin_user.php b/includes/pages/admin_user.php index fbd681c2..8fded20b 100644 --- a/includes/pages/admin_user.php +++ b/includes/pages/admin_user.php @@ -289,7 +289,7 @@ function admin_user() $user_source->email = $request->postData('eemail'); } - $nick = trim((string)$request->get('eNick')); + $nick = trim((string) $request->get('eNick')); $nickValid = (new Username())->validate($nick); $changed_nick = false; diff --git a/src/Controllers/Metrics/Controller.php b/src/Controllers/Metrics/Controller.php index 85cc0151..7065f188 100644 --- a/src/Controllers/Metrics/Controller.php +++ b/src/Controllers/Metrics/Controller.php @@ -68,9 +68,14 @@ class Controller extends BaseController ], 'users' => [ 'type' => 'gauge', - ['labels' => ['state' => 'incoming'], 'value' => $this->stats->newUsers()], - ['labels' => ['state' => 'arrived', 'working' => 'no'], 'value' => $this->stats->arrivedUsers(false)], - ['labels' => ['state' => 'arrived', 'working' => 'yes'], 'value' => $this->stats->arrivedUsers(true)], + ['labels' => ['state' => 'incoming', 'working' => 'no'], 'value' + => $this->stats->usersState(false, false)], + ['labels' => ['state' => 'incoming', 'working' => 'yes'], 'value' + => $this->stats->usersState(true, false)], + ['labels' => ['state' => 'arrived', 'working' => 'no'], 'value' + => $this->stats->usersState(false)], + ['labels' => ['state' => 'arrived', 'working' => 'yes'], 'value' + => $this->stats->usersState(true)], ], 'users_force_active' => ['type' => 'gauge', $this->stats->forceActiveUsers()], 'users_pronouns' => ['type' => 'gauge', $this->stats->usersPronouns()], @@ -194,8 +199,8 @@ class Controller extends BaseController $this->checkAuth(true); $data = [ - 'user_count' => $this->stats->newUsers() + $this->stats->arrivedUsers(), - 'arrived_user_count' => $this->stats->arrivedUsers(), + 'user_count' => $this->stats->usersState() + $this->stats->usersState(null, false), + 'arrived_user_count' => $this->stats->usersState(), 'done_work_hours' => round($this->stats->workSeconds(true) / 60 / 60, 0), 'users_in_action' => $this->stats->currentlyWorkingUsers(), ]; diff --git a/src/Controllers/Metrics/Stats.php b/src/Controllers/Metrics/Stats.php index 2cad810d..1169e39b 100644 --- a/src/Controllers/Metrics/Stats.php +++ b/src/Controllers/Metrics/Stats.php @@ -36,13 +36,13 @@ class Stats } /** - * The number of not arrived users + * The number of users that arrived/not arrived and/or did some work * * @param bool|null $working */ - public function arrivedUsers(bool $working = null): int + public function usersState(bool $working = null, bool $arrived = true): int { - $query = State::whereArrived(true); + $query = State::whereArrived($arrived); if (!is_null($working)) { $query @@ -69,14 +69,6 @@ class Stats return $query->count('users_state.user_id'); } - /** - * The number of not arrived users - */ - public function newUsers(): int - { - return State::whereArrived(false)->count(); - } - public function forceActiveUsers(): int { return State::whereForceActive(true)->count(); diff --git a/tests/Unit/Controllers/Metrics/ControllerTest.php b/tests/Unit/Controllers/Metrics/ControllerTest.php index c9240371..a172e19d 100644 --- a/tests/Unit/Controllers/Metrics/ControllerTest.php +++ b/tests/Unit/Controllers/Metrics/ControllerTest.php @@ -90,10 +90,10 @@ class ControllerTest extends TestCase ->method('licenses') ->withConsecutive(['has_car'], ['forklift'], ['car'], ['3.5t'], ['7.5t'], ['12t'], ['ifsg_light'], ['ifsg']) ->willReturnOnConsecutiveCalls(6, 3, 15, 9, 7, 1, 5, 4); - $stats->expects($this->exactly(2)) - ->method('arrivedUsers') - ->withConsecutive([false], [true]) - ->willReturnOnConsecutiveCalls(7, 43); + $stats->expects($this->exactly(4)) + ->method('usersState') + ->withConsecutive([false, false], [true, false], [false], [true]) + ->willReturnOnConsecutiveCalls(7, 43, 42, 10); $stats->expects($this->exactly(2)) ->method('currentlyWorkingUsers') ->withConsecutive([false], [true]) @@ -123,7 +123,6 @@ class ControllerTest extends TestCase [LogLevel::DEBUG] ) ->willReturnOnConsecutiveCalls(0, 1, 0, 5, 999, 4, 55, 3); - $this->setExpects($stats, 'newUsers', null, 9); $this->setExpects($stats, 'worklogSeconds', null, 39 * 60 * 60); $this->setExpects($stats, 'vouchers', null, 17); $this->setExpects($stats, 'tshirts', null, 3); @@ -192,7 +191,7 @@ class ControllerTest extends TestCase $response->expects($this->once()) ->method('withContent') ->with(json_encode([ - 'user_count' => 13, + 'user_count' => 20, 'arrived_user_count' => 10, 'done_work_hours' => 99, 'users_in_action' => 5, @@ -210,8 +209,7 @@ class ControllerTest extends TestCase ->method('workSeconds') ->with(true) ->willReturn((int) (60 * 60 * 99.47)); - $this->setExpects($stats, 'newUsers', null, 3); - $this->setExpects($stats, 'arrivedUsers', null, 10, $this->exactly(2)); + $this->setExpects($stats, 'usersState', null, 10, $this->exactly(3)); $this->setExpects($stats, 'currentlyWorkingUsers', null, 5); $controller = new Controller($response, $engine, $config, $request, $stats, $version); diff --git a/tests/Unit/Controllers/Metrics/StatsTest.php b/tests/Unit/Controllers/Metrics/StatsTest.php index 66a3f03a..985b1e42 100644 --- a/tests/Unit/Controllers/Metrics/StatsTest.php +++ b/tests/Unit/Controllers/Metrics/StatsTest.php @@ -32,21 +32,10 @@ class StatsTest extends TestCase { use HasDatabase; - /** - * @covers \Engelsystem\Controllers\Metrics\Stats::__construct - * @covers \Engelsystem\Controllers\Metrics\Stats::newUsers - */ - public function testNewUsers(): void - { - $this->addUsers(); - - $stats = new Stats($this->database); - $this->assertEquals(2, $stats->newUsers()); - } - /** * @covers \Engelsystem\Controllers\Metrics\Stats::vouchers * @covers \Engelsystem\Controllers\Metrics\Stats::vouchersQuery + * @covers \Engelsystem\Controllers\Metrics\Stats::__construct */ public function testVouchers(): void { @@ -280,18 +269,22 @@ class StatsTest extends TestCase } /** - * @covers \Engelsystem\Controllers\Metrics\Stats::arrivedUsers + * @covers \Engelsystem\Controllers\Metrics\Stats::usersState */ - public function testArrivedUsers(): void + public function testUsersState(): void { $this->addUsers(); ShiftEntry::factory()->create(['user_id' => 3]); ShiftEntry::factory()->create(['user_id' => 4]); + ShiftEntry::factory()->create(['user_id' => 1]); $stats = new Stats($this->database); - $this->assertEquals(7, $stats->arrivedUsers()); - $this->assertEquals(5, $stats->arrivedUsers(false)); - $this->assertEquals(2, $stats->arrivedUsers(true)); + $this->assertEquals(7, $stats->usersState()); + $this->assertEquals(5, $stats->usersState(false)); + $this->assertEquals(2, $stats->usersState(true)); + $this->assertEquals(2, $stats->usersState(null, false)); + $this->assertEquals(1, $stats->usersState(true, false)); + $this->assertEquals(1, $stats->usersState(false, false)); } /**