From 84a9d768d5a06c047ce1437d700630a19a187d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Thu, 6 Jun 2024 12:10:18 +0100 Subject: [PATCH 1/3] feat: add votemonitor integration --- app/Jobs/FetchVoteMonitorLiveDataJob.php | 54 ++++++++++++++++++++++++ bootstrap/app.php | 10 ++++- config/services.php | 7 +++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 app/Jobs/FetchVoteMonitorLiveDataJob.php diff --git a/app/Jobs/FetchVoteMonitorLiveDataJob.php b/app/Jobs/FetchVoteMonitorLiveDataJob.php new file mode 100644 index 0000000..aa32385 --- /dev/null +++ b/app/Jobs/FetchVoteMonitorLiveDataJob.php @@ -0,0 +1,54 @@ +withToken(config('services.votemonitor.apikey')) + ->get(config('services.votemonitor.url')) + ->throw() + ->json(); + + Stat::upsert( + collect($data) + ->map(fn ($value, $key) => [ + 'key' => $key, + 'value' => $value, + 'updated_at' => now(), + ]) + ->values() + ->all(), + uniqueBy: ['key'], + update: ['value', 'updated_at'], + ); + } +} diff --git a/bootstrap/app.php b/bootstrap/app.php index ac3b607..4af4a7e 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use App\Jobs\FetchVoteMonitorLiveDataJob; +use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; @@ -18,4 +20,10 @@ }) ->withExceptions(function (Exceptions $exceptions) { Integration::handles($exceptions); - })->create(); + }) + ->withSchedule(function (Schedule $schedule) { + $schedule->job(FetchVoteMonitorLiveDataJob::class) + ->everyFiveMinutes() + ->when(fn () => config('services.votemonitor.enabled')); + }) + ->create(); diff --git a/config/services.php b/config/services.php index 11e6ef1..c94f1ad 100644 --- a/config/services.php +++ b/config/services.php @@ -39,4 +39,11 @@ 'google_analytics_id' => env('GOOGLE_ANALYTICS_ID'), + 'votemonitor' => [ + 'enabled' => env('VOTEMONITOR_ENABLED', false), + 'url' => env('VOTEMONITOR_URL'), + 'apikey' => env('VOTEMONITOR_APIKEY'), + 'election_round_id' => env('VOTEMONITOR_ELECTION_ROUND_ID'), + ], + ]; From aa90d20e4d22aaff087b6a962b4482d501788850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Thu, 6 Jun 2024 12:54:01 +0100 Subject: [PATCH 2/3] add user agent --- app/Jobs/FetchVoteMonitorLiveDataJob.php | 1 + config/app.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Jobs/FetchVoteMonitorLiveDataJob.php b/app/Jobs/FetchVoteMonitorLiveDataJob.php index aa32385..5165363 100644 --- a/app/Jobs/FetchVoteMonitorLiveDataJob.php +++ b/app/Jobs/FetchVoteMonitorLiveDataJob.php @@ -33,6 +33,7 @@ class FetchVoteMonitorLiveDataJob implements ShouldQueue, ShouldBeUnique public function handle(): void { $data = Http::acceptJson() + ->withUserAgent(config('app.name')) ->withToken(config('services.votemonitor.apikey')) ->get(config('services.votemonitor.url')) ->throw() diff --git a/config/app.php b/config/app.php index 706439d..86ebec1 100644 --- a/config/app.php +++ b/config/app.php @@ -15,7 +15,7 @@ | */ - 'name' => env('APP_NAME', 'Laravel'), + 'name' => env('APP_NAME', 'WeVote4.EU'), /* |-------------------------------------------------------------------------- From fb51c7f4c6ed6336a2ef2dfe805c68b36c6bace5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Thu, 6 Jun 2024 16:02:46 +0100 Subject: [PATCH 3/3] wip --- app/Enums/StatKey.php | 4 ++++ app/Jobs/FetchVoteMonitorLiveDataJob.php | 10 ++++++---- config/services.php | 1 - lang/en/app.php | 3 +++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/Enums/StatKey.php b/app/Enums/StatKey.php index a2456b5..64fe284 100644 --- a/app/Enums/StatKey.php +++ b/app/Enums/StatKey.php @@ -15,11 +15,15 @@ enum StatKey: string use HasLabel; case VOTES = 'votes'; + case OBSERVERS = 'observers'; case POLLING_STATIONS = 'polling_stations'; + case VISITED_POLLING_STATIONS = 'visited_polling_stations'; case STARTED_FORMS = 'started_forms'; case QUESTIONS_ANSWERED = 'questions_answered'; case FLAGGED_ANSWERS = 'flagged_answers'; + case MINUTES_MONITORING = 'minutes_monitoring'; + case NGOS = 'ngos'; protected function labelKeyPrefix(): ?string { diff --git a/app/Jobs/FetchVoteMonitorLiveDataJob.php b/app/Jobs/FetchVoteMonitorLiveDataJob.php index 5165363..c854069 100644 --- a/app/Jobs/FetchVoteMonitorLiveDataJob.php +++ b/app/Jobs/FetchVoteMonitorLiveDataJob.php @@ -4,6 +4,7 @@ namespace App\Jobs; +use App\Enums\StatKey; use App\Models\Stat; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; @@ -34,18 +35,19 @@ public function handle(): void { $data = Http::acceptJson() ->withUserAgent(config('app.name')) - ->withToken(config('services.votemonitor.apikey')) + ->withHeader('x-vote-monitor-api-key', config('services.votemonitor.apikey')) ->get(config('services.votemonitor.url')) ->throw() ->json(); Stat::upsert( - collect($data) - ->map(fn ($value, $key) => [ + collect(StatKey::values()) + ->map(fn (string $key) => [ 'key' => $key, - 'value' => $value, + 'value' => data_get($data, $key), 'updated_at' => now(), ]) + ->reject(fn (array $item) => blank($item['value'])) ->values() ->all(), uniqueBy: ['key'], diff --git a/config/services.php b/config/services.php index c94f1ad..db70e80 100644 --- a/config/services.php +++ b/config/services.php @@ -43,7 +43,6 @@ 'enabled' => env('VOTEMONITOR_ENABLED', false), 'url' => env('VOTEMONITOR_URL'), 'apikey' => env('VOTEMONITOR_APIKEY'), - 'election_round_id' => env('VOTEMONITOR_ELECTION_ROUND_ID'), ], ]; diff --git a/lang/en/app.php b/lang/en/app.php index bc7d1ee..7c31f41 100644 --- a/lang/en/app.php +++ b/lang/en/app.php @@ -28,9 +28,12 @@ 'votes' => 'Votes', 'observers' => 'Observers on field', 'polling_stations' => 'Polling stations', + 'visited_polling_stations' => 'Visited polling stations', 'started_forms' => 'Started forms', 'questions_answered' => 'Questions answered', 'flagged_answers' => 'Flagged answers', + 'minutes_monitoring' => 'Minutes monitoring', + 'ngos' => 'NGOs', ], 'votemonitor' => [