forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendDataToInfluxDbV2.php
More file actions
77 lines (63 loc) · 1.95 KB
/
SendDataToInfluxDbV2.php
File metadata and controls
77 lines (63 loc) · 1.95 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
<?php
namespace App\Jobs;
use App\Models\Result;
use App\Settings\InfluxDbSettings;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use InfluxDB2\Client;
class SendDataToInfluxDbV2 implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $result;
public $settings;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Result $result, InfluxDbSettings $settings)
{
$this->result = $result;
$this->settings = $settings;
}
/**
* Execute the job.
*/
public function handle(): void
{
$influxdb = [
'enabled' => $this->settings->v2_enabled,
'url' => $this->settings?->v2_url,
'org' => $this->settings?->v2_org,
'bucket' => $this->settings?->v2_bucket,
'token' => $this->settings?->v2_token,
'verifySSL' => $this->settings->v2_verify_ssl,
];
$client = new Client([
'url' => $influxdb['url'],
'token' => $influxdb['token'],
'bucket' => $influxdb['bucket'],
'org' => $influxdb['org'],
'verifySSL' => $influxdb['verifySSL'],
'precision' => \InfluxDB2\Model\WritePrecision::S,
]);
$writeApi = $client->createWriteApi();
$dataArray = [
'name' => 'speedtest',
'tags' => $this->result->formatTagsForInfluxDB2(),
'fields' => $this->result->formatForInfluxDB2(),
'time' => strtotime($this->result->created_at),
];
try {
$writeApi->write($dataArray);
} catch (\Exception $e) {
Log::info($e);
$this->fail();
}
$writeApi->close();
}
}