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
53 changes: 53 additions & 0 deletions app/Console/Commands/TestInfluxDB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Console\Commands;

use App\Models\Result;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use InfluxDB2\Client;
use Symfony\Component\Yaml\Yaml;

class TestInfluxDB extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:test-influxdb';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Write a test log to InfluxDB to make sure the config works.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
if (File::exists(base_path().'/config.yml')) {
$config = Yaml::parseFile(
base_path().'/config.yml'
);
}

if (File::exists('/app/config.yml')) {
$config = Yaml::parseFile('/app/config.yml');
}

$influxdb = $config['influxdb'];

if ($influxdb['enabled'] == true) {
$result = Result::factory()->create();
}

return 0;
}
}
17 changes: 17 additions & 0 deletions app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,21 @@ class Result extends Model
'scheduled' => 'boolean',
'created_at' => 'datetime',
];

/**
* The attributes to be passed to influxdb
*/
public function formatForInfluxDB2()
{
return [
'id' => (int) $this->id,
'ping' => (float) $this->ping,
'download' => (int) $this->download,
'upload' => (int) $this->upload,
'server_id' => (int) $this->server_id,
'server_host' => $this->server_host,
'server_name' => $this->server_name,
'scheduled' => $this->scheduled,
];
}
}
111 changes: 111 additions & 0 deletions app/Observers/ResultObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace App\Observers;

use App\Models\Result;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use InfluxDB2\Client;
use Symfony\Component\Yaml\Yaml;

class ResultObserver
{
/**
* Handle events after all transactions are committed.
*
* @var bool
*/
public $afterCommit = true;

/**
* Handle the Result "created" event.
*
* @param \App\Models\Result $result
* @return void
*/
public function created(Result $result)
{
if (File::exists(base_path().'/config.yml')) {
$config = Yaml::parseFile(
base_path().'/config.yml'
);
}

if (File::exists('/app/config.yml')) {
$config = Yaml::parseFile('/app/config.yml');
}

$influxdb = $config['influxdb'];

if ($influxdb['enabled'] == true) {
$client = new Client([
'url' => $influxdb['url'],
'token' => $influxdb['token'],
'bucket' => $influxdb['bucket'],
'org' => $influxdb['org'],
'precision' => \InfluxDB2\Model\WritePrecision::S
]);

$writeApi = $client->createWriteApi();

$dataArray = [
'name' => 'speedtest',
'tags' => null,
'fields' => $result->formatForInfluxDB2(),
'time' => strtotime($result->created_at),
];

try {
$writeApi->write($dataArray);
} catch (\Exception $e) {
Log::error($e);
}

$writeApi->close();
}
}

/**
* Handle the Result "updated" event.
*
* @param \App\Models\Result $result
* @return void
*/
public function updated(Result $result)
{
//
}

/**
* Handle the Result "deleted" event.
*
* @param \App\Models\Result $result
* @return void
*/
public function deleted(Result $result)
{
//
}

/**
* Handle the Result "restored" event.
*
* @param \App\Models\Result $result
* @return void
*/
public function restored(Result $result)
{
//
}

/**
* Handle the Result "force deleted" event.
*
* @param \App\Models\Result $result
* @return void
*/
public function forceDeleted(Result $result)
{
//
}
}
4 changes: 3 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Models\Result;
use App\Observers\ResultObserver;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -27,7 +29,7 @@ class EventServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Result::observe(ResultObserver::class);
}

/**
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"php": "^8.0.2",
"dragonmantank/cron-expression": "^3.3",
"guzzlehttp/guzzle": "^7.2",
"influxdata/influxdb-client-php": "^2.9",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
Expand Down
49 changes: 48 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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