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 new file mode 100644 index 0000000..c854069 --- /dev/null +++ b/app/Jobs/FetchVoteMonitorLiveDataJob.php @@ -0,0 +1,57 @@ +withUserAgent(config('app.name')) + ->withHeader('x-vote-monitor-api-key', config('services.votemonitor.apikey')) + ->get(config('services.votemonitor.url')) + ->throw() + ->json(); + + Stat::upsert( + collect(StatKey::values()) + ->map(fn (string $key) => [ + 'key' => $key, + 'value' => data_get($data, $key), + 'updated_at' => now(), + ]) + ->reject(fn (array $item) => blank($item['value'])) + ->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/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'), /* |-------------------------------------------------------------------------- diff --git a/config/services.php b/config/services.php index 11e6ef1..db70e80 100644 --- a/config/services.php +++ b/config/services.php @@ -39,4 +39,10 @@ 'google_analytics_id' => env('GOOGLE_ANALYTICS_ID'), + 'votemonitor' => [ + 'enabled' => env('VOTEMONITOR_ENABLED', false), + 'url' => env('VOTEMONITOR_URL'), + 'apikey' => env('VOTEMONITOR_APIKEY'), + ], + ]; 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' => [