Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/Enums/StatKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
57 changes: 57 additions & 0 deletions app/Jobs/FetchVoteMonitorLiveDataJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Enums\StatKey;
use App\Models\Stat;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;

class FetchVoteMonitorLiveDataJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 1;

/**
* Execute the job.
*/
public function handle(): void
{
$data = Http::acceptJson()
->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'],
);
}
}
10 changes: 9 additions & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
|
*/

'name' => env('APP_NAME', 'Laravel'),
'name' => env('APP_NAME', 'WeVote4.EU'),

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
],

];
3 changes: 3 additions & 0 deletions lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down