Skip to content

Commit

Permalink
Merge pull request #413 from henrywhitaker3/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
henrywhitaker3 authored Dec 13, 2020
2 parents 0b8c633 + 342e87b commit 17b72b9
Show file tree
Hide file tree
Showing 20 changed files with 2,266 additions and 2,250 deletions.
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.9.6-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.10.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 Down
48 changes: 27 additions & 21 deletions app/Helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use Cache;
use Carbon\Carbon;

class SettingsHelper {
class SettingsHelper
{

/**
* Get a Setting object by name
Expand All @@ -19,9 +20,9 @@ public static function get(String $name)
{
$name = Setting::where('name', $name)->get();

if(sizeof($name) == 0) {
if (sizeof($name) == 0) {
return false;
} else if(sizeof($name) == 1) {
} else if (sizeof($name) == 1) {
return $name[0];
} else {
$name = $name->keyBy('name');
Expand All @@ -40,11 +41,11 @@ public static function set(String $name, $value)
{
$setting = SettingsHelper::get($name);

if($value === false) {
if ($value === false) {
$value = "0";
}

if($setting !== false) {
if ($setting !== false) {
$setting->value = $value;
$setting->save();
} else {
Expand All @@ -54,7 +55,7 @@ public static function set(String $name, $value)
]);
}

if($name == 'show_failed_tests_on_graph') {
if ($name == 'show_failed_tests_on_graph') {
Cache::flush();
}

Expand All @@ -69,13 +70,13 @@ public static function set(String $name, $value)
public static function getBase()
{
$base = env('BASE_PATH', '/');
if($base == '') {
if ($base == '') {
$base = '/';
} else {
if($base[0] != '/') {
if ($base[0] != '/') {
$base = '/' . $base;
}
if($base[-1] != '/') {
if ($base[-1] != '/') {
$base = $base . '/';
}
}
Expand All @@ -95,7 +96,7 @@ public static function settingIsEditable(string $key)
// Try exact key
$val = exec('echo $' . $key);

if($val == "") {
if ($val == "") {
array_push($results, true);
} else {
array_push($results, false);
Expand All @@ -104,25 +105,25 @@ public static function settingIsEditable(string $key)
// Try key all caps
$val = exec('echo $' . strtoupper($key));

if($val == "") {
if ($val == "") {
array_push($results, true);
} else {
array_push($results, false);
}

if(env($key, false) == false) {
if (env($key, false) == false) {
array_push($results, true);
} else {
array_push($results, false);
}

if(env(strtoupper($key), false) == false) {
if (env(strtoupper($key), false) == false) {
array_push($results, true);
} else {
array_push($results, false);
}

if(in_array(false, $results)) {
if (in_array(false, $results)) {
return false;
}

Expand All @@ -138,6 +139,11 @@ public static function getConfig()
{
return [
'base' => SettingsHelper::getBase(),
'widgets' => [
'show_average' => (bool)SettingsHelper::get('show_average')->value,
'show_max' => (bool)SettingsHelper::get('show_max')->value,
'show_min' => (bool)SettingsHelper::get('show_min')->value,
],
'graphs' => [
'download_upload_graph_enabled' => SettingsHelper::get('download_upload_graph_enabled'),
'download_upload_graph_width' => SettingsHelper::get('download_upload_graph_width'),
Expand All @@ -163,15 +169,15 @@ public static function getConfig()
*/
public static function testNotification($agent = true)
{
$agents = [ 'slack', 'telegram' ];
$agents = ['slack', 'telegram'];

if($agent === true) {
if ($agent === true) {
event(new TestNotificationEvent($agents));
return true;
}

if(in_array($agent, $agents)) {
event(new TestNotificationEvent([ $agent ]));
if (in_array($agent, $agents)) {
event(new TestNotificationEvent([$agent]));
return true;
}

Expand All @@ -188,14 +194,14 @@ public static function loadIntegrationConfig()
'telegram_chat_id' => SettingsHelper::get('telegram_chat_id')->value,
];

foreach($settings as $key => $value) {
foreach ($settings as $key => $value) {
$key = 'integrations.' . $key;

if($value === "") {
if ($value === "") {
$value = null;
}

config()->set([ $key => $value ]);
config()->set([$key => $value]);
}
}
}
80 changes: 49 additions & 31 deletions app/Http/Controllers/SpeedtestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class SpeedtestController extends Controller
{
public function __construct()
{
if((bool)SettingsHelper::get('auth')->value === true) {
if ((bool)SettingsHelper::get('auth')->value === true) {
$this->middleware('auth:api')
->only([ 'run', 'delete', 'deleteAll' ]);
->only(['run', 'delete', 'deleteAll']);
}
}

Expand All @@ -32,7 +32,7 @@ public function __construct()
public function index()
{
$data = Speedtest::orderBy('created_at', 'desc')
->paginate();
->paginate();

return response()->json([
'method' => 'index of speedtests',
Expand All @@ -49,12 +49,12 @@ public function index()
public function time($days)
{
$rule = [
'days' => [ 'required', 'integer' ],
'days' => ['required', 'integer'],
];

$validator = Validator::make([ 'days' => $days ], $rule);
$validator = Validator::make(['days' => $days], $rule);

if($validator->fails()) {
if ($validator->fails()) {
return response()->json([
'method' => 'get speedtests in last x days',
'error' => $validator->errors(),
Expand All @@ -65,16 +65,16 @@ public function time($days)
$data = Cache::remember('speedtest-days-' . $days, $ttl, function () use ($days) {
$showFailed = (bool)SettingsHelper::get('show_failed_tests_on_graph')->value;

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

return Speedtest::where('created_at', '>=', Carbon::now()->subDays($days))
->where('failed', false)
->orderBy('created_at', 'asc')
->get();
->where('failed', false)
->orderBy('created_at', 'asc')
->get();
});

return response()->json([
Expand All @@ -93,12 +93,12 @@ public function time($days)
public function fail($days)
{
$rule = [
'days' => [ 'required', 'integer' ],
'days' => ['required', 'integer'],
];

$validator = Validator::make([ 'days' => $days ], $rule);
$validator = Validator::make(['days' => $days], $rule);

if($validator->fails()) {
if ($validator->fails()) {
return response()->json([
'method' => 'get speedtests in last x days',
'error' => $validator->errors(),
Expand All @@ -122,20 +122,38 @@ public function fail($days)
public function latest()
{
$data = SpeedtestHelper::latest();
$avg = Speedtest::select(DB::raw('AVG(ping) as ping, AVG(download) as download, AVG(upload) as upload'))
->where('failed', false)
->get();
$max = Speedtest::select(DB::raw('MAX(ping) as ping, MAX(download) as download, MAX(upload) as upload'))
->where('failed', false)
->get();

if($data) {
return response()->json([
'method' => 'get latest speedtest',
'data' => $data,
'average' => $avg[0],
'max' => $max[0],
], 200);

$response = [
'method' => 'get latest speedtest',
'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;
}

if ($data) {
return response()->json($response, 200);
} else {
return response()->json([
'method' => 'get latest speedtest',
Expand All @@ -158,7 +176,7 @@ public function run()
'method' => 'run speedtest',
'data' => 'a new speedtest has been added to the queue'
], 200);
} catch(Exception $e) {
} catch (Exception $e) {
return response()->json([
'method' => 'run speedtest',
'error' => $e
Expand All @@ -175,7 +193,7 @@ public function deleteAll()
{
$ret = SpeedtestHelper::deleteAll();

if($ret['success']) {
if ($ret['success']) {
return response()->json([
'method' => 'delete all speedtests from the database',
'success' => true
Expand Down
42 changes: 42 additions & 0 deletions changelog.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
{
"1.10.0": [
{
"description": "Added automated build for ARM.",
"link": ""
},
{
"description": "Updated dependencies.",
"link": ""
}
],
"1.9.10": [
{
"description": "Laravel will now log into daily files and logs will only be kept for 7 days.",
"link": ""
}
],
"1.9.9": [
{
"description": "Updated dependencies.",
"link": ""
}
],
"1.9.8": [
{
"description": "Fixed bug with minimum display setting.",
"link": ""
}
],
"1.9.7": [
{
"description": "Added option to display minimum values on the top widgets.",
"link": ""
},
{
"description": "New general settings section.",
"link": ""
},
{
"description": "Updated dependencies.",
"link": ""
}
],
"1.9.6": [
{
"description": "Clear the cache on restore.",
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0",
"henrywhitaker3/healthchecks-io": "^1.0",
"laravel-notification-channels/telegram": "^0.4.0",
"laravel-notification-channels/telegram": "^0.5.0",
"laravel/framework": "^7.0",
"laravel/slack-notification-channel": "^2.0",
"laravel/tinker": "^2.0",
Expand Down
Loading

0 comments on commit 17b72b9

Please sign in to comment.