Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InfluxDB integration #563

Merged
merged 14 commits into from
Apr 11, 2021
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Speedtest Tracker

[![Docker pulls](https://img.shields.io/docker/pulls/henrywhitaker3/speedtest-tracker?style=flat-square)](https://hub.docker.com/r/henrywhitaker3/speedtest-tracker) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/henrywhitaker3/Speedtest-Tracker/Stable?label=master&logo=github&style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/henrywhitaker3/Speedtest-Tracker/Dev?label=dev&logo=github&style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [![last_commit](https://img.shields.io/github/last-commit/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) [![issues](https://img.shields.io/github/issues/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/issues) [![commit_freq](https://img.shields.io/github/commit-activity/m/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) ![version](https://img.shields.io/badge/version-v1.11.1-success?style=flat-square) [![license](https://img.shields.io/github/license/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/blob/master/LICENSE)
[![Docker pulls](https://img.shields.io/docker/pulls/henrywhitaker3/speedtest-tracker?style=flat-square)](https://hub.docker.com/r/henrywhitaker3/speedtest-tracker) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/henrywhitaker3/Speedtest-Tracker/Stable?label=master&logo=github&style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/henrywhitaker3/Speedtest-Tracker/Dev?label=dev&logo=github&style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [![last_commit](https://img.shields.io/github/last-commit/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) [![issues](https://img.shields.io/github/issues/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/issues) [![commit_freq](https://img.shields.io/github/commit-activity/m/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) ![version](https://img.shields.io/badge/version-v1.12.0-success?style=flat-square) [![license](https://img.shields.io/github/license/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/blob/master/LICENSE)

This program runs a speedtest check every hour and graphs the results. The back-end is written in [Laravel](https://laravel.com/) and the front-end uses [React](https://reactjs.org/). It uses the [Ookla's speedtest cli](https://www.speedtest.net/apps/cli) package to get the data and uses [Chart.js](https://www.chartjs.org/) to plot the results.

Expand All @@ -18,6 +18,7 @@ Disclaimer: You will need to accept Ookla's EULA and privacy agreements in order
- Slack/Discord/Telegram notifications
- [healthchecks.io](https://healthchecks.io) integration
- Organizr integration
- InfluxDB integration (currently v1 only, v2 is a WIP)

## Installation & Setup

Expand Down Expand Up @@ -84,6 +85,8 @@ Container images are configured using parameters passed at runtime (such as thos
| `-e PUID` | Optional. Supply a local user ID for volume permissions |
| `-e PGID` | Optional. Supply a local group ID for volume permissions |
| `-e AUTH` | Optional. Set to 'true' to enable authentication for the app |
| `-e INFLUXDB_RETENTION`| Optional. Sets the InfluxDB retention period, defaults to `30d` |
| `-e INFLUXDB_HOST_TAG | Optional. Sets the InfluxDB host tag value, defaults to `speedtest` |

### Authentication

Expand All @@ -101,3 +104,7 @@ After enabling, you should change the password through the web UI.
### Manual Install

For manual installations, please follow the instructions [here](https://github.com/henrywhitaker3/Speedtest-Tracker/wiki/Manual-Installation).

### Kubernetes

There is a 3rd party helm chart available [here](https://github.com/sOblivionsCall/charts).
2 changes: 1 addition & 1 deletion app/Actions/GetFailedSpeedtestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Actions;

use App\Speedtest;
use App\Models\Speedtest;
use Cache;
use Carbon\Carbon;
use DB;
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/GetLatestSpeedtestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Helpers\SettingsHelper;
use App\Helpers\SpeedtestHelper;
use App\Speedtest;
use App\Models\Speedtest;
use DB;
use Henrywhitaker3\LaravelActions\Interfaces\ActionInterface;

Expand Down
2 changes: 1 addition & 1 deletion app/Actions/GetSpeedtestTimeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Actions;

use App\Helpers\SettingsHelper;
use App\Speedtest;
use App\Models\Speedtest;
use Cache;
use Carbon\Carbon;
use Henrywhitaker3\LaravelActions\Interfaces\ActionInterface;
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/ClearOldSessionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Console\Commands;

use App\Auth\LoginSession;
use App\Models\Auth\LoginSession;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Log;
Expand Down
10 changes: 10 additions & 0 deletions app/Exceptions/InfluxDBConnectionErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Exceptions;

use Exception;

class InfluxDBConnectionErrorException extends Exception
{
//
}
10 changes: 10 additions & 0 deletions app/Exceptions/InfluxDBNotEnabledException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Exceptions;

use Exception;

class InfluxDBNotEnabledException extends Exception
{
//
}
2 changes: 1 addition & 1 deletion app/Helpers/BackupHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Helpers;

use App\Speedtest;
use App\Models\Speedtest;
use Cache;
use DateTime;
use Exception;
Expand Down
4 changes: 2 additions & 2 deletions app/Helpers/EmailVerificationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Helpers;

use App\Auth\EmailVerification;
use App\User;
use App\Models\Auth\EmailVerification;
use App\Models\User;

class EmailVerificationHelper {
public static function checkVerificationAttempt($userID, $token)
Expand Down
2 changes: 1 addition & 1 deletion app/Helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Helpers;

use App\Events\TestNotificationEvent;
use App\Setting;
use App\Models\Setting;
use Cache;
use Carbon\Carbon;

Expand Down
5 changes: 3 additions & 2 deletions app/Helpers/SpeedtestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace App\Helpers;

use App\Speedtest;
use App\Interfaces\SpeedtestProvider;
use App\Models\Speedtest;
use App\Utils\OoklaTester;
use Carbon\Carbon;
use Exception;
Expand All @@ -25,7 +26,7 @@ class SpeedtestHelper
*/
public static function runSpeedtest()
{
$tester = new OoklaTester();
$tester = app()->make(SpeedtestProvider::class);
return $tester->run();
}

Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace App\Http\Controllers;

use App\Auth\EmailVerification;
use App\Auth\LoginSession as AuthLoginSession;
use App\Models\Auth\EmailVerification;
use App\Models\Auth\LoginSession as AuthLoginSession;
use App\Helpers\EmailVerificationHelper;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\LoginSession;
use App\Rules\CurrentPasswordMatches;
use App\User;
use App\Models\User;
use DateTime;
use Hash;
use Illuminate\Support\Facades\Request as RequestFacade;
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Helpers\SettingsHelper;
use App\Rules\Cron;
use App\Setting;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/SpeedtestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Helpers\SettingsHelper;
use App\Helpers\SpeedtestHelper;
use App\Jobs\SpeedtestJob;
use App\Speedtest;
use App\Models\Speedtest;
use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request;
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/CheckActiveSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Middleware;

use App\Auth\LoginSession;
use App\Models\Auth\LoginSession;
use Closure;
use Exception;

Expand Down
13 changes: 13 additions & 0 deletions app/Interfaces/InfluxDBWrapperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Interfaces;

use App\Models\Speedtest;

interface InfluxDBWrapperInterface
{
public function testConnection(): bool;
public function doesDatabaseExist(string $database): bool;
public function createDatabase(string $database): bool;
public function store(Speedtest $speedtest): bool;
}
2 changes: 1 addition & 1 deletion app/Interfaces/SpeedtestProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Interfaces;

use App\Speedtest;
use App\Models\Speedtest;

interface SpeedtestProvider
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Auth;
namespace App\Models\Auth;

use Illuminate\Database\Eloquent\Model;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Auth;
namespace App\Models\Auth;

use Illuminate\Database\Eloquent\Model;

Expand Down
2 changes: 1 addition & 1 deletion app/Setting.php → app/Models/Setting.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App;
namespace App\Models;

use App\Casts\CommaSeparatedArrayCast;
use App\Helpers\SettingsHelper;
Expand Down
45 changes: 45 additions & 0 deletions app/Models/Speedtest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Speedtest extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'ping',
'download',
'upload',
'created_at',
'server_id',
'server_name',
'server_host',
'url',
'scheduled',
'failed',
];

protected $table = 'speedtests';

public function formatForInfluxDB()
{
return [
'id' => (int) $this->id,
'download' => (float) $this->download,
'upload' => (float) $this->upload,
'ping' => (float) $this->ping,
'server_id' => (int) $this->server_id,
'server_host' => $this->server_host,
'server_name' => $this->server_name,
'scheduled' => (bool) $this->scheduled,
];
}
}
4 changes: 2 additions & 2 deletions app/User.php → app/Models/User.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App;
namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand Down Expand Up @@ -107,7 +107,7 @@ public function getJWTCustomClaims()

public function setPasswordAttribute($password)
{
if ( !empty($password) ) {
if (!empty($password)) {
$this->attributes['password'] = Hash::make($password);
}
}
Expand Down
30 changes: 30 additions & 0 deletions app/Observers/SpeedtestObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Observers;

use App\Exceptions\InfluxDBNotEnabledException;
use App\Models\Speedtest;
use App\Utils\InfluxDB\InfluxDB;
use Exception;
use Log;

class SpeedtestObserver
{
/**
* Handle the Speedtest "created" event.
*
* @param \App\Speedtest $speedtest
* @return void
*/
public function created(Speedtest $speedtest)
{
try {
InfluxDB::connect()
->store($speedtest);
} catch (InfluxDBNotEnabledException $e) {
// /
} catch (Exception $e) {
Log::error($e);
}
}
}
4 changes: 3 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use App\Listeners\SpeedtestFailedListener;
use App\Listeners\SpeedtestOverviewListener;
use App\Listeners\TestNotificationListener;
use App\Observers\SpeedtestObserver;
use App\Models\Speedtest;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand Down Expand Up @@ -49,6 +51,6 @@ public function boot()
{
parent::boot();

//
Speedtest::observe(SpeedtestObserver::class);
}
}
28 changes: 0 additions & 28 deletions app/Speedtest.php

This file was deleted.

Loading