diff --git a/README.md b/README.md index 1abcee48..c1ce12fc 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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 diff --git a/app/Helpers/SpeedtestHelper.php b/app/Helpers/SpeedtestHelper.php index a5b9c1d6..016da06e 100644 --- a/app/Helpers/SpeedtestHelper.php +++ b/app/Helpers/SpeedtestHelper.php @@ -2,6 +2,7 @@ namespace App\Helpers; +use App\Interfaces\SpeedtestProvider; use App\Models\Speedtest; use App\Utils\OoklaTester; use Carbon\Carbon; @@ -25,7 +26,7 @@ class SpeedtestHelper */ public static function runSpeedtest() { - $tester = new OoklaTester(); + $tester = app()->make(SpeedtestProvider::class); return $tester->run(); } diff --git a/app/Utils/InfluxDB/InfluxDBVersion2Wrapper.php b/app/Utils/InfluxDB/InfluxDBVersion2Wrapper.php new file mode 100644 index 00000000..f7d24946 --- /dev/null +++ b/app/Utils/InfluxDB/InfluxDBVersion2Wrapper.php @@ -0,0 +1,37 @@ +client = $client; + } + + public function testConnection(): bool + { + return $this->client->health()->getStatus() !== 'pass'; + } + + public function doesDatabaseExist(string $database): bool + { + return true; + } + + public function createDatabase(string $database): bool + { + return true; + } + + public function store(Speedtest $speedtest): bool + { + return true; + } +} diff --git a/changelog.json b/changelog.json index 7302b2c9..89c3ab38 100644 --- a/changelog.json +++ b/changelog.json @@ -1,4 +1,10 @@ { + "1.12.0": [ + { + "description": "Added InfluxDB intergation.", + "link": "" + } + ], "1.11.1": [ { "description": "Add option to show/hide columns in the all tests table.", diff --git a/config/speedtest.php b/config/speedtest.php index 1db30b80..d39eaf89 100644 --- a/config/speedtest.php +++ b/config/speedtest.php @@ -7,7 +7,7 @@ return [ |-------------------------------------------------------------------------- */ - 'version' => '1.11.1', + 'version' => '1.12.0', /* |-------------------------------------------------------------------------- diff --git a/database/factories/SpeedtestFactory.php b/database/factories/SpeedtestFactory.php index 4cc2efc3..c09c6798 100644 --- a/database/factories/SpeedtestFactory.php +++ b/database/factories/SpeedtestFactory.php @@ -26,6 +26,7 @@ class SpeedtestFactory extends Factory 'upload' => rand(15, 900), 'ping' => rand(1, 25), 'scheduled' => (bool) rand(0, 1), + 'failed' => false, ]; } } diff --git a/database/speed.db b/database/speed.db index f2560a3d..b84a194a 100644 Binary files a/database/speed.db and b/database/speed.db differ diff --git a/tests/Feature/SpeedtestTest.php b/tests/Feature/SpeedtestTest.php index 4efc6e17..e3560a20 100644 --- a/tests/Feature/SpeedtestTest.php +++ b/tests/Feature/SpeedtestTest.php @@ -3,14 +3,28 @@ namespace Tests\Feature; use App\Helpers\SpeedtestHelper; +use App\Interfaces\SpeedtestProvider; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; +use Tests\Mocks\OoklaTesterMocker; use Tests\TestCase; class SpeedtestTest extends TestCase { use RefreshDatabase; + public function setUp(): void + { + parent::setUp(); + + $this->app->singleton( + SpeedtestProvider::class, + function () { + return new OoklaTesterMocker(); + } + ); + } + /** * Runs a speedtest * diff --git a/tests/Feature/Utils/InfluxDB/InfluxDBWrapperWrapperTest.php b/tests/Feature/Utils/InfluxDB/InfluxDBWrapperWrapperTest.php new file mode 100644 index 00000000..2ccdc5e8 --- /dev/null +++ b/tests/Feature/Utils/InfluxDB/InfluxDBWrapperWrapperTest.php @@ -0,0 +1,26 @@ +expectException(InfluxDBNotEnabledException::class); + + InfluxDB::connect(); + } +} diff --git a/tests/Mocks/OoklaTesterMocker.php b/tests/Mocks/OoklaTesterMocker.php new file mode 100644 index 00000000..035696c3 --- /dev/null +++ b/tests/Mocks/OoklaTesterMocker.php @@ -0,0 +1,59 @@ +passes = $passes; + } + + public function run($output = null): Speedtest + { + $output = $output ?? $this->output(); + + try { + $output = json_decode($output, true, 512, JSON_THROW_ON_ERROR); + } catch (Exception $e) { + throw new SpeedtestFailureException(); + } + + return $this->passes + ? Speedtest::factory()->create() + : Speedtest::factory()->create([ + 'download' => 0, + 'upload' => 0, + 'ping' => 0, + 'failed' => true, + ]); + } + + public function output() + { + return !$this->passes + ? null + : json_encode([ + 'type' => 'result', + 'download' => ['bandwidth' => '50'], + 'upload' => ['bandwidth' => '50'], + 'ping' => ['latency' => '50'], + 'server' => [ + 'id' => '1', + 'name' => 'PHPUnit', + 'host' => 'phpunit', + 'port' => '443', + ], + 'result' => [ + 'url' => 'some-url', + ] + ]); + } +} diff --git a/tests/Unit/Helpers/SpeedtestHelper/CheckOutputTest.php b/tests/Unit/Helpers/SpeedtestHelper/CheckOutputTest.php index d017cb7b..febf818f 100644 --- a/tests/Unit/Helpers/SpeedtestHelper/CheckOutputTest.php +++ b/tests/Unit/Helpers/SpeedtestHelper/CheckOutputTest.php @@ -4,6 +4,7 @@ namespace Tests\Unit\Helpers\SpeedtestHelper; use App\Utils\OoklaTester; use PHPUnit\Framework\TestCase; +use Tests\Mocks\OoklaTesterMocker; class CheckOutputTest extends TestCase { @@ -12,6 +13,7 @@ class CheckOutputTest extends TestCase public function setUp(): void { $this->speedtestProvider = new OoklaTester(); + $this->mocker = new OoklaTesterMocker(); } /** diff --git a/tests/Unit/Helpers/SpeedtestHelper/SpeedtestTest.php b/tests/Unit/Helpers/SpeedtestHelper/SpeedtestTest.php index 9c5db28f..7e4670bb 100644 --- a/tests/Unit/Helpers/SpeedtestHelper/SpeedtestTest.php +++ b/tests/Unit/Helpers/SpeedtestHelper/SpeedtestTest.php @@ -6,6 +6,7 @@ use App\Exceptions\SpeedtestFailureException; use App\Helpers\SpeedtestHelper; use App\Utils\OoklaTester; use Illuminate\Foundation\Testing\RefreshDatabase; +use Tests\Mocks\OoklaTesterMocker; use Tests\TestCase; class SpeedtestTest extends TestCase @@ -22,17 +23,7 @@ class SpeedtestTest extends TestCase $this->speedtestProvider = new OoklaTester(); - $this->output = $this->speedtestProvider->output(); - } - - /** - * A basic unit test example. - * - * @return void - */ - public function testOutputFunction() - { - $this->assertJson($this->output); + $this->output = (new OoklaTesterMocker())->output(); } /**