From 6e76843db460804cf61afe815ab78f4468e3d6af Mon Sep 17 00:00:00 2001 From: Xu Date: Tue, 26 Mar 2024 21:02:05 +0100 Subject: [PATCH] add ifsg and drive confirmed stats to metrics --- src/Controllers/Metrics/Controller.php | 41 ++++++++++++------- src/Controllers/Metrics/Stats.php | 23 ++++++----- .../Controllers/Metrics/ControllerTest.php | 22 ++++++++-- tests/Unit/Controllers/Metrics/StatsTest.php | 23 ++++++++--- 4 files changed, 76 insertions(+), 33 deletions(-) diff --git a/src/Controllers/Metrics/Controller.php b/src/Controllers/Metrics/Controller.php index c5cbf736..ef620fb1 100644 --- a/src/Controllers/Metrics/Controller.php +++ b/src/Controllers/Metrics/Controller.php @@ -68,14 +68,14 @@ class Controller extends BaseController ], 'users' => [ 'type' => 'gauge', - ['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)], + ['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_info' => ['type' => 'gauge', $this->stats->usersInfo()], 'users_force_active' => ['type' => 'gauge', $this->stats->forceActiveUsers()], @@ -84,13 +84,24 @@ class Controller extends BaseController 'type' => 'gauge', 'help' => 'The total number of licenses', ['labels' => ['type' => 'has_car'], 'value' => $this->stats->licenses('has_car')], - ['labels' => ['type' => 'forklift'], 'value' => $this->stats->licenses('forklift')], - ['labels' => ['type' => 'car'], 'value' => $this->stats->licenses('car')], - ['labels' => ['type' => '3.5t'], 'value' => $this->stats->licenses('3.5t')], - ['labels' => ['type' => '7.5t'], 'value' => $this->stats->licenses('7.5t')], - ['labels' => ['type' => '12t'], 'value' => $this->stats->licenses('12t')], - ['labels' => ['type' => 'ifsg_light'], 'value' => $this->stats->licenses('ifsg_light')], - ['labels' => ['type' => 'ifsg'], 'value' => $this->stats->licenses('ifsg')], + ['labels' => ['type' => 'forklift', 'confirmed' => 'no'], + 'value' => $this->stats->licenses('forklift')], + ['labels' => ['type' => 'forklift', 'confirmed' => 'yes'], + 'value' => $this->stats->licenses('forklift', true)], + ['labels' => ['type' => 'car', 'confirmed' => 'no'], 'value' => $this->stats->licenses('car')], + ['labels' => ['type' => 'car', 'confirmed' => 'yes'], 'value' => $this->stats->licenses('car', true)], + ['labels' => ['type' => '3.5t', 'confirmed' => 'no'], 'value' => $this->stats->licenses('3.5t')], + ['labels' => ['type' => '3.5t', 'confirmed' => 'yes'], 'value' => $this->stats->licenses('3.5t', true)], + ['labels' => ['type' => '7.5t', 'confirmed' => 'no'], 'value' => $this->stats->licenses('7.5t')], + ['labels' => ['type' => '7.5t', 'confirmed' => 'yes'], 'value' => $this->stats->licenses('7.5t', true)], + ['labels' => ['type' => '12t', 'confirmed' => 'no'], 'value' => $this->stats->licenses('12t')], + ['labels' => ['type' => '12t', 'confirmed' => 'yes'], 'value' => $this->stats->licenses('12t', true)], + ['labels' => ['type' => 'ifsg_light', 'confirmed' => 'no'], + 'value' => $this->stats->licenses('ifsg_light')], + ['labels' => ['type' => 'ifsg_light', 'confirmed' => 'yes'], + 'value' => $this->stats->licenses('ifsg_light', true)], + ['labels' => ['type' => 'ifsg', 'confirmed' => 'no'], 'value' => $this->stats->licenses('ifsg')], + ['labels' => ['type' => 'ifsg', 'confirmed' => 'yes'], 'value' => $this->stats->licenses('ifsg', true)], ], 'users_email' => [ 'type' => 'gauge', diff --git a/src/Controllers/Metrics/Stats.php b/src/Controllers/Metrics/Stats.php index 1f4f9936..dfc144dc 100644 --- a/src/Controllers/Metrics/Stats.php +++ b/src/Controllers/Metrics/Stats.php @@ -176,22 +176,25 @@ class Stats ->get(); } - public function licenses(string $license): int + public function licenses(string $license, bool $confirmed = false): int { $mapping = [ - 'has_car' => 'has_car', - 'forklift' => 'drive_forklift', - 'car' => 'drive_car', - '3.5t' => 'drive_3_5t', - '7.5t' => 'drive_7_5t', - '12t' => 'drive_12t', - 'ifsg_light' => 'ifsg_certificate_light', - 'ifsg' => 'ifsg_certificate', + 'has_car' => ['has_car', null], + 'forklift' => ['drive_forklift', 'drive_confirmed'], + 'car' => ['drive_car', 'drive_confirmed'], + '3.5t' => ['drive_3_5t', 'drive_confirmed'], + '7.5t' => ['drive_7_5t', 'drive_confirmed'], + '12t' => ['drive_12t', 'drive_confirmed'], + 'ifsg_light' => ['ifsg_certificate_light', 'ifsg_confirmed'], + 'ifsg' => ['ifsg_certificate', 'ifsg_confirmed'], ]; $query = (new License()) ->getQuery() - ->where($mapping[$license], true); + ->where($mapping[$license][0], true); + if (!is_null($mapping[$license][1])) { + $query->where($mapping[$license][1], $confirmed); + } return $query->count(); } diff --git a/tests/Unit/Controllers/Metrics/ControllerTest.php b/tests/Unit/Controllers/Metrics/ControllerTest.php index a172e19d..c9d26d2e 100644 --- a/tests/Unit/Controllers/Metrics/ControllerTest.php +++ b/tests/Unit/Controllers/Metrics/ControllerTest.php @@ -86,10 +86,26 @@ class ControllerTest extends TestCase ->with('metrics return') ->willReturn($response); - $stats->expects($this->exactly(8)) + $stats->expects($this->exactly(15)) ->method('licenses') - ->withConsecutive(['has_car'], ['forklift'], ['car'], ['3.5t'], ['7.5t'], ['12t'], ['ifsg_light'], ['ifsg']) - ->willReturnOnConsecutiveCalls(6, 3, 15, 9, 7, 1, 5, 4); + ->withConsecutive( + ['has_car'], + ['forklift'], + ['forklift'], + ['car'], + ['car', true], + ['3.5t'], + ['3.5t', true], + ['7.5t'], + ['7.5t', true], + ['12t'], + ['12t', true], + ['ifsg_light'], + ['ifsg_light', true], + ['ifsg'], + ['ifsg', true], + ) + ->willReturnOnConsecutiveCalls(6, 3, 15, 9, 7, 1, 5, 4, 3, 5, 9, 2, 1, 7, 8); $stats->expects($this->exactly(4)) ->method('usersState') ->withConsecutive([false, false], [true, false], [false], [true]) diff --git a/tests/Unit/Controllers/Metrics/StatsTest.php b/tests/Unit/Controllers/Metrics/StatsTest.php index fcfbcc08..2e171aa3 100644 --- a/tests/Unit/Controllers/Metrics/StatsTest.php +++ b/tests/Unit/Controllers/Metrics/StatsTest.php @@ -129,12 +129,19 @@ class StatsTest extends TestCase $stats = new Stats($this->database); $this->assertEquals(1, $stats->licenses('has_car')); $this->assertEquals(1, $stats->licenses('forklift')); - $this->assertEquals(2, $stats->licenses('car')); + $this->assertEquals(1, $stats->licenses('car')); $this->assertEquals(0, $stats->licenses('3.5t')); $this->assertEquals(0, $stats->licenses('7.5t')); - $this->assertEquals(1, $stats->licenses('12t')); - $this->assertEquals(0, $stats->licenses('ifsg_light')); + $this->assertEquals(0, $stats->licenses('12t')); + $this->assertEquals(1, $stats->licenses('ifsg_light')); $this->assertEquals(0, $stats->licenses('ifsg')); + $this->assertEquals(0, $stats->licenses('forklift', true)); + $this->assertEquals(1, $stats->licenses('car', true)); + $this->assertEquals(0, $stats->licenses('3.5t', true)); + $this->assertEquals(0, $stats->licenses('7.5t', true)); + $this->assertEquals(1, $stats->licenses('12t', true)); + $this->assertEquals(0, $stats->licenses('ifsg_light', true)); + $this->assertEquals(1, $stats->licenses('ifsg', true)); } /** @@ -518,13 +525,19 @@ class StatsTest extends TestCase ['arrived' => 1, 'got_voucher' => 9, 'force_active' => true, 'user_info' => 'Info'], [], ['theme' => 1], - ['drive_car' => true, 'drive_12t' => true] + ['drive_car' => true, 'drive_12t' => true, 'drive_confirmed' => true, 'ifsg_certificate_light' => true] ); $this->addUser( ['arrived' => 1, 'got_voucher' => 3], ['pronoun' => 'per'], ['theme' => 1, 'email_human' => true], - ['has_car' => true, 'drive_forklift' => true, 'drive_car' => true] + [ + 'has_car' => true, + 'drive_forklift' => true, + 'drive_car' => true, + 'ifsg_certificate' => true, + 'ifsg_confirmed' => true, + ] ); $this->addUser(['arrived' => 1, 'active' => 1, 'got_shirt' => true, 'force_active' => true]); $this->addUser(['arrived' => 1, 'active' => 1, 'got_shirt' => true], ['shirt_size' => 'L'], ['theme' => 4]);