forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBulkWriteResults.php
More file actions
81 lines (65 loc) · 2.11 KB
/
BulkWriteResults.php
File metadata and controls
81 lines (65 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace App\Jobs\Influxdb\v2;
use App\Actions\Influxdb\v2\BuildPointData;
use App\Actions\Influxdb\v2\CreateClient;
use App\Enums\ResultStatus;
use App\Models\Result;
use App\Models\User;
use Filament\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
class BulkWriteResults implements ShouldQueue
{
use Queueable;
/**
* The number of seconds the job can run before timing out.
*
* @var int
*/
public $timeout = 120;
/**
* Create a new job instance.
*/
public function __construct(
public User $user,
) {}
/**
* Execute the job.
*/
public function handle(): void
{
$client = CreateClient::run();
$writeApi = $client->createWriteApi();
Result::query()
->where('status', '=', ResultStatus::Completed)
->chunkById(100, function (Collection $results) use ($writeApi) {
$points = [];
foreach ($results as $result) {
$points[] = BuildPointData::run($result);
}
try {
$writeApi->write($points);
} catch (\Exception $e) {
Log::error('Failed to bulk write to InfluxDB.', [
'error' => $e->getMessage(),
]);
Notification::make()
->title('Failed to build write to Influxdb.')
->body('Check the logs for more details.')
->danger()
->sendToDatabase($this->user);
$this->fail($e);
$writeApi->close();
return;
}
});
$writeApi->close();
Notification::make()
->title('Finished bulk data load to Influxdb.')
->body('Data has been sent to InfluxDB, check if the data was received.')
->success()
->sendToDatabase($this->user);
}
}