Metrics: Added not arrived users

This commit is contained in:
Xu 2023-12-28 16:26:56 +01:00 committed by Igor Scheller
parent 1d3509ba3c
commit 57373c846a
5 changed files with 30 additions and 42 deletions

View File

@ -68,9 +68,14 @@ class Controller extends BaseController
], ],
'users' => [ 'users' => [
'type' => 'gauge', 'type' => 'gauge',
['labels' => ['state' => 'incoming'], 'value' => $this->stats->newUsers()], ['labels' => ['state' => 'incoming', 'working' => 'no'], 'value'
['labels' => ['state' => 'arrived', 'working' => 'no'], 'value' => $this->stats->arrivedUsers(false)], => $this->stats->usersState(false, false)],
['labels' => ['state' => 'arrived', 'working' => 'yes'], 'value' => $this->stats->arrivedUsers(true)], ['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_force_active' => ['type' => 'gauge', $this->stats->forceActiveUsers()],
'users_pronouns' => ['type' => 'gauge', $this->stats->usersPronouns()], 'users_pronouns' => ['type' => 'gauge', $this->stats->usersPronouns()],
@ -194,8 +199,8 @@ class Controller extends BaseController
$this->checkAuth(true); $this->checkAuth(true);
$data = [ $data = [
'user_count' => $this->stats->newUsers() + $this->stats->arrivedUsers(), 'user_count' => $this->stats->usersState() + $this->stats->usersState(null, false),
'arrived_user_count' => $this->stats->arrivedUsers(), 'arrived_user_count' => $this->stats->usersState(),
'done_work_hours' => round($this->stats->workSeconds(true) / 60 / 60, 0), 'done_work_hours' => round($this->stats->workSeconds(true) / 60 / 60, 0),
'users_in_action' => $this->stats->currentlyWorkingUsers(), 'users_in_action' => $this->stats->currentlyWorkingUsers(),
]; ];

View File

@ -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 * @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)) { if (!is_null($working)) {
$query $query
@ -69,14 +69,6 @@ class Stats
return $query->count('users_state.user_id'); 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 public function forceActiveUsers(): int
{ {
return State::whereForceActive(true)->count(); return State::whereForceActive(true)->count();

View File

@ -90,10 +90,10 @@ class ControllerTest extends TestCase
->method('licenses') ->method('licenses')
->withConsecutive(['has_car'], ['forklift'], ['car'], ['3.5t'], ['7.5t'], ['12t'], ['ifsg_light'], ['ifsg']) ->withConsecutive(['has_car'], ['forklift'], ['car'], ['3.5t'], ['7.5t'], ['12t'], ['ifsg_light'], ['ifsg'])
->willReturnOnConsecutiveCalls(6, 3, 15, 9, 7, 1, 5, 4); ->willReturnOnConsecutiveCalls(6, 3, 15, 9, 7, 1, 5, 4);
$stats->expects($this->exactly(2)) $stats->expects($this->exactly(4))
->method('arrivedUsers') ->method('usersState')
->withConsecutive([false], [true]) ->withConsecutive([false, false], [true, false], [false], [true])
->willReturnOnConsecutiveCalls(7, 43); ->willReturnOnConsecutiveCalls(7, 43, 42, 10);
$stats->expects($this->exactly(2)) $stats->expects($this->exactly(2))
->method('currentlyWorkingUsers') ->method('currentlyWorkingUsers')
->withConsecutive([false], [true]) ->withConsecutive([false], [true])
@ -123,7 +123,6 @@ class ControllerTest extends TestCase
[LogLevel::DEBUG] [LogLevel::DEBUG]
) )
->willReturnOnConsecutiveCalls(0, 1, 0, 5, 999, 4, 55, 3); ->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, 'worklogSeconds', null, 39 * 60 * 60);
$this->setExpects($stats, 'vouchers', null, 17); $this->setExpects($stats, 'vouchers', null, 17);
$this->setExpects($stats, 'tshirts', null, 3); $this->setExpects($stats, 'tshirts', null, 3);
@ -192,7 +191,7 @@ class ControllerTest extends TestCase
$response->expects($this->once()) $response->expects($this->once())
->method('withContent') ->method('withContent')
->with(json_encode([ ->with(json_encode([
'user_count' => 13, 'user_count' => 20,
'arrived_user_count' => 10, 'arrived_user_count' => 10,
'done_work_hours' => 99, 'done_work_hours' => 99,
'users_in_action' => 5, 'users_in_action' => 5,
@ -210,8 +209,7 @@ class ControllerTest extends TestCase
->method('workSeconds') ->method('workSeconds')
->with(true) ->with(true)
->willReturn((int) (60 * 60 * 99.47)); ->willReturn((int) (60 * 60 * 99.47));
$this->setExpects($stats, 'newUsers', null, 3); $this->setExpects($stats, 'usersState', null, 10, $this->exactly(3));
$this->setExpects($stats, 'arrivedUsers', null, 10, $this->exactly(2));
$this->setExpects($stats, 'currentlyWorkingUsers', null, 5); $this->setExpects($stats, 'currentlyWorkingUsers', null, 5);
$controller = new Controller($response, $engine, $config, $request, $stats, $version); $controller = new Controller($response, $engine, $config, $request, $stats, $version);

View File

@ -32,21 +32,10 @@ class StatsTest extends TestCase
{ {
use HasDatabase; 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::vouchers
* @covers \Engelsystem\Controllers\Metrics\Stats::vouchersQuery * @covers \Engelsystem\Controllers\Metrics\Stats::vouchersQuery
* @covers \Engelsystem\Controllers\Metrics\Stats::__construct
*/ */
public function testVouchers(): void 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(); $this->addUsers();
ShiftEntry::factory()->create(['user_id' => 3]); ShiftEntry::factory()->create(['user_id' => 3]);
ShiftEntry::factory()->create(['user_id' => 4]); ShiftEntry::factory()->create(['user_id' => 4]);
ShiftEntry::factory()->create(['user_id' => 1]);
$stats = new Stats($this->database); $stats = new Stats($this->database);
$this->assertEquals(7, $stats->arrivedUsers()); $this->assertEquals(7, $stats->usersState());
$this->assertEquals(5, $stats->arrivedUsers(false)); $this->assertEquals(5, $stats->usersState(false));
$this->assertEquals(2, $stats->arrivedUsers(true)); $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));
} }
/** /**