Skip to content

Commit

Permalink
Merge pull request #521 from henrywhitaker3/alpha
Browse files Browse the repository at this point in the history
Bunch of updates
  • Loading branch information
henrywhitaker3 committed Apr 10, 2021
2 parents 99d80ed + b82b7c6 commit dd87160
Show file tree
Hide file tree
Showing 50 changed files with 28,318 additions and 1,952 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/laravel-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ jobs:
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit
run: php artisan test --parallel
2 changes: 1 addition & 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.10.4-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.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)

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 Down
48 changes: 48 additions & 0 deletions app/Actions/GetFailedSpeedtestData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Actions;

use App\Speedtest;
use Cache;
use Carbon\Carbon;
use DB;
use Henrywhitaker3\LaravelActions\Interfaces\ActionInterface;

class GetFailedSpeedtestData implements ActionInterface
{
/**
* Run the action.
*
* @return mixed
*/
public function run($days = 7)
{
$ttl = Carbon::now()->addDays(1);

return Cache::remember('failure-rate-' . $days, $ttl, function () use ($days) {
$range = [
Carbon::today()
];
for ($i = 0; $i < ($days - 1); $i++) {
$prev = end($range);
$new = $prev->copy()->subDays(1);
array_push($range, $new);
}

$rate = [];

foreach ($range as $day) {
$success = Speedtest::select(DB::raw('COUNT(id) as rate'))->whereDate('created_at', $day)->where('failed', false)->get()[0]['rate'];
$fail = Speedtest::select(DB::raw('COUNT(id) as rate'))->whereDate('created_at', $day)->where('failed', true)->get()[0]['rate'];

array_push($rate, [
'date' => $day->toDateString(),
'success' => $success,
'failure' => $fail,
]);
}

return array_reverse($rate);
});
}
}
52 changes: 52 additions & 0 deletions app/Actions/GetLatestSpeedtestData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Actions;

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

class GetLatestSpeedtestData implements ActionInterface
{
/**
* Run the action.
*
* @return mixed
*/
public function run()
{
$data = SpeedtestHelper::latest();

$response = [
'data' => $data,
];

if (SettingsHelper::get('show_average')) {
$avg = Speedtest::select(DB::raw('AVG(ping) as ping, AVG(download) as download, AVG(upload) as upload'))
->where('failed', false)
->first()
->toArray();
$response['average'] = $avg;
}

if (SettingsHelper::get('show_max')) {
$max = Speedtest::select(DB::raw('MAX(ping) as ping, MAX(download) as download, MAX(upload) as upload'))
->where('failed', false)
->first()
->toArray();
$response['maximum'] = $max;
}

if (SettingsHelper::get('show_min')) {
$min = Speedtest::select(DB::raw('MIN(ping) as ping, MIN(download) as download, MIN(upload) as upload'))
->where('failed', false)
->first()
->toArray();
$response['minimum'] = $min;
}

return $response;
}
}
37 changes: 37 additions & 0 deletions app/Actions/GetSpeedtestTimeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Actions;

use App\Helpers\SettingsHelper;
use App\Speedtest;
use Cache;
use Carbon\Carbon;
use Henrywhitaker3\LaravelActions\Interfaces\ActionInterface;

class GetSpeedtestTimeData implements ActionInterface
{
/**
* Run the action.
*
* @return mixed
*/
public function run($days = 7)
{
$ttl = Carbon::now()->addDays(1);

return Cache::remember('speedtest-days-' . $days, $ttl, function () use ($days) {
$showFailed = (bool)SettingsHelper::get('show_failed_tests_on_graph')->value;

if ($showFailed === true) {
return Speedtest::where('created_at', '>=', Carbon::now()->subDays($days))
->orderBy('created_at', 'asc')
->get();
}

return Speedtest::where('created_at', '>=', Carbon::now()->subDays($days))
->where('failed', false)
->orderBy('created_at', 'asc')
->get();
});
}
}
35 changes: 35 additions & 0 deletions app/Actions/QueueSpeedtest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Actions;

use App\Helpers\SettingsHelper;
use App\Interfaces\SpeedtestProvider;
use App\Jobs\SpeedtestJob;
use Henrywhitaker3\LaravelActions\Interfaces\ActionInterface;

class QueueSpeedtest implements ActionInterface
{
private SpeedtestProvider $speedtestProvider;

/**
* Create a new action instance.
*
* @return void
*/
public function __construct(SpeedtestProvider $speedtestProvider)
{
$this->speedtestProvider = $speedtestProvider;
}

/**
* Run the action.
*
* @return mixed
*/
public function run()
{
SettingsHelper::loadIntegrationConfig();

SpeedtestJob::dispatch(false, config('integrations'), $this->speedtestProvider);
}
}
52 changes: 52 additions & 0 deletions app/Casts/CommaSeparatedArrayCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class CommaSeparatedArrayCast implements CastsAttributes
{
/**
* Array of settings that should be cast
*/
private array $shouldCast = [
'visible_columns',
'hidden_columns',
];

/**
* Cast the given value.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
*/
public function get($model, $key, $value, $attributes)
{
if (!in_array($model->name, $this->shouldCast)) {
return $value;
}

return explode(',', $value);
}

/**
* Prepare the given value for storage.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
*/
public function set($model, $key, $value, $attributes)
{
if (!in_array($model->name, $this->shouldCast)) {
return $value;
}

return implode(',', $value);
}
}
3 changes: 2 additions & 1 deletion app/Console/Commands/AcceptEULACommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function __construct()
*/
public function handle()
{
shell_exec(config('speedtest.home') . ' && ' . app_path() . '/Bin/speedtest --accept-license --accept-gdpr');
$this->info('Acceping EULA');
shell_exec(config('speedtest.home') . ' && timeout 3s ' . app_path() . '/Bin/speedtest --accept-license --accept-gdpr');
}
}
13 changes: 9 additions & 4 deletions app/Console/Commands/SpeedtestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Helpers\SpeedtestHelper;
use App\Interfaces\SpeedtestProvider;
use Illuminate\Console\Command;

class SpeedtestCommand extends Command
Expand All @@ -21,13 +22,17 @@ class SpeedtestCommand extends Command
*/
protected $description = 'Performs a new speedtest';

private SpeedtestProvider $speedtestProvider;

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
public function __construct(SpeedtestProvider $speedtestProvider)
{
$this->speedtestProvider = $speedtestProvider;

parent::__construct();
}

Expand All @@ -40,14 +45,14 @@ public function handle()
{
$this->info('Running speedtest, this might take a while...');

$results = SpeedtestHelper::runSpeedtest(false, false);
$results = $this->speedtestProvider->run(false, false);

if(!is_object($results)) {
if (!is_object($results)) {
$this->error('Something went wrong running the speedtest.');
exit();
}

if(property_exists($results, 'ping') && property_exists($results, 'download') && property_exists($results, 'upload')) {
if (property_exists($results, 'ping') && property_exists($results, 'download') && property_exists($results, 'upload')) {
$this->error('Something went wrong running the speedtest.');
exit();
}
Expand Down
15 changes: 10 additions & 5 deletions app/Console/Commands/SpeedtestLatestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Helpers\SpeedtestHelper;
use App\Interfaces\SpeedtestProvider;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

Expand All @@ -22,13 +23,17 @@ class SpeedtestLatestCommand extends Command
*/
protected $description = 'Returns the latest speedtest result';

private SpeedtestProvider $speedtestProvider;

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
public function __construct(SpeedtestProvider $speedtestProvider)
{
$this->speedtestProvider = $speedtestProvider;

parent::__construct();
}

Expand All @@ -41,16 +46,16 @@ public function handle()
{
$latest = SpeedtestHelper::latest();

if($latest) {
if($latest->scheduled) {
if ($latest) {
if ($latest->scheduled) {
$extra = '(scheduled)';
} else {
$extra = '(manual)';
}

$this->info('Last speedtest run at: ' . $latest->created_at . ' ' . $extra);

if($latest->failed) {
if ($latest->failed) {
$this->error('Speedtest failed');
} else {
$this->info('Ping: ' . $latest->ping . ' ms');
Expand All @@ -62,7 +67,7 @@ public function handle()

$this->info('Running speedtest, this might take a while...');

$results = SpeedtestHelper::runSpeedtest();
$results = $this->speedtestProvider->run();

$this->info('Ping: ' . $results->ping . ' ms');
$this->info('Download: ' . $results->download . ' Mbit/s');
Expand Down
13 changes: 11 additions & 2 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Events\SpeedtestOverviewEvent;
use App\Helpers\SettingsHelper;
use App\Helpers\SpeedtestHelper;
use App\Interfaces\SpeedtestProvider;
use App\Jobs\SpeedtestJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
Expand All @@ -29,9 +30,17 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule)
{
if ((bool)SettingsHelper::get('schedule_enabled')->value) {
$schedule->job(new SpeedtestJob(true, config('integrations')))->cron(SettingsHelper::get('schedule')['value']);
$schedule->job(
new SpeedtestJob(
true,
config('integrations'),
app()->make(SpeedtestProvider::class)
)
)
->cron(SettingsHelper::get('schedule')['value'])
->timezone(env('TZ', 'UTC'));
}
$schedule->command('speedtest:overview')->cron('0 ' . SettingsHelper::get('speedtest_overview_time')->value . ' * * *');
$schedule->command('speedtest:overview')->cron('0 ' . SettingsHelper::get('speedtest_overview_time')->value . ' * * *')->timezone(env('TZ', 'UTC'));
$schedule->command('speedtest:clear-sessions')->everyMinute();
}

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

namespace App\Exceptions;

use Exception;

class SpeedtestFailureException extends Exception
{
//
}
Loading

0 comments on commit dd87160

Please sign in to comment.