Skip to content

Commit cc12e4c

Browse files
authored
Influxdb support (alexjustesen#13)
1 parent 015b08b commit cc12e4c

File tree

7 files changed

+240
-6
lines changed

7 files changed

+240
-6
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\Result;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\File;
8+
use Illuminate\Support\Facades\Log;
9+
use InfluxDB2\Client;
10+
use Symfony\Component\Yaml\Yaml;
11+
12+
class TestInfluxDB extends Command
13+
{
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'app:test-influxdb';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Write a test log to InfluxDB to make sure the config works.';
27+
28+
/**
29+
* Execute the console command.
30+
*
31+
* @return int
32+
*/
33+
public function handle()
34+
{
35+
if (File::exists(base_path().'/config.yml')) {
36+
$config = Yaml::parseFile(
37+
base_path().'/config.yml'
38+
);
39+
}
40+
41+
if (File::exists('/app/config.yml')) {
42+
$config = Yaml::parseFile('/app/config.yml');
43+
}
44+
45+
$influxdb = $config['influxdb'];
46+
47+
if ($influxdb['enabled'] == true) {
48+
$result = Result::factory()->create();
49+
}
50+
51+
return 0;
52+
}
53+
}

app/Models/Result.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ class Result extends Model
4242
'scheduled' => 'boolean',
4343
'created_at' => 'datetime',
4444
];
45+
46+
/**
47+
* The attributes to be passed to influxdb
48+
*/
49+
public function formatForInfluxDB2()
50+
{
51+
return [
52+
'id' => (int) $this->id,
53+
'ping' => (float) $this->ping,
54+
'download' => (int) $this->download,
55+
'upload' => (int) $this->upload,
56+
'server_id' => (int) $this->server_id,
57+
'server_host' => $this->server_host,
58+
'server_name' => $this->server_name,
59+
'scheduled' => $this->scheduled,
60+
];
61+
}
4562
}

app/Observers/ResultObserver.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace App\Observers;
4+
5+
use App\Models\Result;
6+
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Facades\Log;
8+
use InfluxDB2\Client;
9+
use Symfony\Component\Yaml\Yaml;
10+
11+
class ResultObserver
12+
{
13+
/**
14+
* Handle events after all transactions are committed.
15+
*
16+
* @var bool
17+
*/
18+
public $afterCommit = true;
19+
20+
/**
21+
* Handle the Result "created" event.
22+
*
23+
* @param \App\Models\Result $result
24+
* @return void
25+
*/
26+
public function created(Result $result)
27+
{
28+
if (File::exists(base_path().'/config.yml')) {
29+
$config = Yaml::parseFile(
30+
base_path().'/config.yml'
31+
);
32+
}
33+
34+
if (File::exists('/app/config.yml')) {
35+
$config = Yaml::parseFile('/app/config.yml');
36+
}
37+
38+
$influxdb = $config['influxdb'];
39+
40+
if ($influxdb['enabled'] == true) {
41+
$client = new Client([
42+
'url' => $influxdb['url'],
43+
'token' => $influxdb['token'],
44+
'bucket' => $influxdb['bucket'],
45+
'org' => $influxdb['org'],
46+
'precision' => \InfluxDB2\Model\WritePrecision::S
47+
]);
48+
49+
$writeApi = $client->createWriteApi();
50+
51+
$dataArray = [
52+
'name' => 'speedtest',
53+
'tags' => null,
54+
'fields' => $result->formatForInfluxDB2(),
55+
'time' => strtotime($result->created_at),
56+
];
57+
58+
try {
59+
$writeApi->write($dataArray);
60+
} catch (\Exception $e) {
61+
Log::error($e);
62+
}
63+
64+
$writeApi->close();
65+
}
66+
}
67+
68+
/**
69+
* Handle the Result "updated" event.
70+
*
71+
* @param \App\Models\Result $result
72+
* @return void
73+
*/
74+
public function updated(Result $result)
75+
{
76+
//
77+
}
78+
79+
/**
80+
* Handle the Result "deleted" event.
81+
*
82+
* @param \App\Models\Result $result
83+
* @return void
84+
*/
85+
public function deleted(Result $result)
86+
{
87+
//
88+
}
89+
90+
/**
91+
* Handle the Result "restored" event.
92+
*
93+
* @param \App\Models\Result $result
94+
* @return void
95+
*/
96+
public function restored(Result $result)
97+
{
98+
//
99+
}
100+
101+
/**
102+
* Handle the Result "force deleted" event.
103+
*
104+
* @param \App\Models\Result $result
105+
* @return void
106+
*/
107+
public function forceDeleted(Result $result)
108+
{
109+
//
110+
}
111+
}

app/Providers/EventServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Providers;
44

5+
use App\Models\Result;
6+
use App\Observers\ResultObserver;
57
use Illuminate\Auth\Events\Registered;
68
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
79
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@@ -27,7 +29,7 @@ class EventServiceProvider extends ServiceProvider
2729
*/
2830
public function boot()
2931
{
30-
//
32+
Result::observe(ResultObserver::class);
3133
}
3234

3335
/**

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"php": "^8.0.2",
99
"dragonmantank/cron-expression": "^3.3",
1010
"guzzlehttp/guzzle": "^7.2",
11+
"influxdata/influxdb-client-php": "^2.9",
1112
"laravel/framework": "^9.19",
1213
"laravel/sanctum": "^3.0",
1314
"laravel/tinker": "^2.7",

composer.lock

Lines changed: 48 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config.example.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
---
2-
authentication: true # Set to false to disable authentication
2+
authentication: true
33
influxdb:
4-
enabled: true
5-
host_tag: 'speedtest-tracker'
6-
retention: '30d'
4+
enabled: false
5+
version: 2 # Only InfluxDB2 is supported right now
6+
url: 'http://your-influxdb-instance' # Full url to your InfluxDB instance, sometimes this includes a port number
7+
org: '' # Organization name, usually created when you installed InfluxDB
8+
bucket: 'speedtest-tracker' # A name location to store the speedtest data in
9+
token: '' # This is your API token
710
notifications:
811
channels:
912
discord:

0 commit comments

Comments
 (0)