From c0c1dab711f018ea750adc1f500eb50487728f7e Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 12 Sep 2020 22:51:32 +0200 Subject: [PATCH] Metrics: Added user enabled email targets and comments counts --- src/Controllers/Metrics/Controller.php | 6 ++ src/Controllers/Metrics/Stats.php | 31 +++++++++++ tests/Unit/Controllers/Metrics/StatsTest.php | 58 ++++++++++++++++---- 3 files changed, 85 insertions(+), 10 deletions(-) diff --git a/src/Controllers/Metrics/Controller.php b/src/Controllers/Metrics/Controller.php index 138a5b29..913b23ba 100644 --- a/src/Controllers/Metrics/Controller.php +++ b/src/Controllers/Metrics/Controller.php @@ -107,6 +107,11 @@ class Controller extends BaseController ['labels' => ['type' => '7.5t'], 'value' => $this->stats->licenses('7.5t')], ['labels' => ['type' => '12.5t'], 'value' => $this->stats->licenses('12.5t')], ], + 'users_email' => [ + 'type' => 'gauge', + ['labels' => ['type' => 'system'], 'value' => $this->stats->email('system')], + ['labels' => ['type' => 'humans'], 'value' => $this->stats->email('humans')], + ], 'users_working' => [ 'type' => 'gauge', ['labels' => ['freeloader' => false], $this->stats->currentlyWorkingUsers(false)], @@ -152,6 +157,7 @@ class Controller extends BaseController ['labels' => ['type' => 'news'], 'value' => $this->stats->announcements(false)], ['labels' => ['type' => 'meeting'], 'value' => $this->stats->announcements(true)], ], + 'comments' => ['type' => 'gauge', $this->stats->comments()], 'questions' => [ 'type' => 'gauge', ['labels' => ['state' => 'answered'], 'value' => $this->stats->questions(true)], diff --git a/src/Controllers/Metrics/Stats.php b/src/Controllers/Metrics/Stats.php index e8fe1a39..6305670b 100644 --- a/src/Controllers/Metrics/Stats.php +++ b/src/Controllers/Metrics/Stats.php @@ -10,6 +10,7 @@ use Engelsystem\Models\EventConfig; use Engelsystem\Models\LogEntry; use Engelsystem\Models\Message; use Engelsystem\Models\News; +use Engelsystem\Models\NewsComment; use Engelsystem\Models\Question; use Engelsystem\Models\User\PasswordReset; use Engelsystem\Models\User\PersonalData; @@ -88,6 +89,27 @@ class Stats return State::whereForceActive(true)->count(); } + /** + * @param string $type + * + * @return int + */ + public function email(string $type): int + { + switch ($type) { + case 'system': + $query = Settings::whereEmailShiftinfo(true); + break; + case 'humans': + $query = Settings::whereEmailHuman(true); + break; + default: + return 0; + } + + return $query->count(); + } + /** * The number of currently working users * @@ -364,6 +386,15 @@ class Stats return $query->count(); } + /** + * @return int + */ + public function comments(): int + { + return NewsComment::query() + ->count(); + } + /** * @param bool|null $answered * @return int diff --git a/tests/Unit/Controllers/Metrics/StatsTest.php b/tests/Unit/Controllers/Metrics/StatsTest.php index f1a7d526..ce1786c3 100644 --- a/tests/Unit/Controllers/Metrics/StatsTest.php +++ b/tests/Unit/Controllers/Metrics/StatsTest.php @@ -7,6 +7,7 @@ use Engelsystem\Controllers\Metrics\Stats; use Engelsystem\Models\LogEntry; use Engelsystem\Models\Message; use Engelsystem\Models\News; +use Engelsystem\Models\NewsComment; use Engelsystem\Models\Question; use Engelsystem\Models\User\PasswordReset; use Engelsystem\Models\User\PersonalData; @@ -112,13 +113,12 @@ class StatsTest extends TestCase $themes = $stats->themes(); $this->assertCount(3, $themes); $this->assertEquals([ - ['theme' => 0, 'count' => 7], - ['theme' => 1, 'count' => 1], + ['theme' => 0, 'count' => 6], + ['theme' => 1, 'count' => 2], ['theme' => 4, 'count' => 1], ], $themes->toArray()); } - /** * @covers \Engelsystem\Controllers\Metrics\Stats::announcements */ @@ -137,6 +137,27 @@ class StatsTest extends TestCase $this->assertEquals(1, $stats->announcements(true)); } + /** + * @covers \Engelsystem\Controllers\Metrics\Stats::comments + */ + public function testComments() + { + $user = $this->addUser(); + + $news = new News(['title' => 'Test', 'text' => 'Foo Bar', 'user_id' => $user->id]); + $news->save(); + + foreach (['Test', 'Another text!'] as $text) { + $comment = new NewsComment(['text' => $text]); + $comment->news()->associate($news); + $comment->user()->associate($user); + $comment->save(); + } + + $stats = new Stats($this->database); + $this->assertEquals(2, $stats->comments()); + } + /** * @covers \Engelsystem\Controllers\Metrics\Stats::questions */ @@ -177,6 +198,19 @@ class StatsTest extends TestCase $this->assertEquals(2, $stats->forceActiveUsers()); } + /** + * @covers \Engelsystem\Controllers\Metrics\Stats::email + */ + public function testEmail() + { + $this->addUsers(); + + $stats = new Stats($this->database); + $this->assertEquals(0, $stats->email('not-available-option')); + $this->assertEquals(2, $stats->email('system')); + $this->assertEquals(3, $stats->email('humans')); + } + /** * @covers \Engelsystem\Controllers\Metrics\Stats::messages */ @@ -267,12 +301,12 @@ class StatsTest extends TestCase protected function addUsers() { $this->addUser(); - $this->addUser([], ['shirt_size' => 'L']); - $this->addUser(['arrived' => 1]); - $this->addUser(['arrived' => 1], [], ['language' => 'lo_RM']); + $this->addUser([], ['shirt_size' => 'L'], ['email_human' => true, 'email_shiftinfo' => true]); + $this->addUser(['arrived' => 1], [], ['email_human' => true]); + $this->addUser(['arrived' => 1], [], ['language' => 'lo_RM', 'email_shiftinfo' => true]); $this->addUser(['arrived' => 1, 'got_voucher' => 2], ['shirt_size' => 'XXL'], ['language' => 'lo_RM']); $this->addUser(['arrived' => 1, 'got_voucher' => 9, 'force_active' => true], [], ['theme' => 1]); - $this->addUser(['arrived' => 1, 'got_voucher' => 3], ['theme' => 10]); + $this->addUser(['arrived' => 1, 'got_voucher' => 3], [], ['theme' => 1, 'email_human' => 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]); } @@ -281,8 +315,10 @@ class StatsTest extends TestCase * @param array $state * @param array $personalData * @param array $settings + * + * @return User */ - protected function addUser(array $state = [], $personalData = [], $settings = []) + protected function addUser(array $state = [], $personalData = [], $settings = []): User { $name = 'user_' . Str::random(5); @@ -307,12 +343,14 @@ class StatsTest extends TestCase $settings = new Settings(array_merge([ 'language' => 'te_ST', 'theme' => 0, - 'email_human' => '', - 'email_shiftinfo' => '', + 'email_human' => false, + 'email_shiftinfo' => false, ], $settings)); $settings->user() ->associate($user) ->save(); + + return $user; } /**