Skip to content

Commit

Permalink
Fix tests and update version number
Browse files Browse the repository at this point in the history
  • Loading branch information
henrywhitaker3 committed Apr 10, 2021
1 parent 78fcf49 commit 1d9fba6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 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.11.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)
[![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
38 changes: 38 additions & 0 deletions app/Helpers/SpeedtestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,42 @@ public static function testIsLowerThanThreshold(String $type, Speedtest $test)

throw new InvalidArgumentException();
}

/**
* Get a percentage rate of failure by days
*
* @param integer $days number of days to get rate for
* @return integer percentage fail rate
*/
public static function failureRate(int $days)
{
$ttl = Carbon::now()->addDays(1);
$rate = 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);
});

return $rate;
}
}
2 changes: 1 addition & 1 deletion app/Utils/OoklaTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function run($output = false, $scheduled = true): Speedtest
'scheduled' => $scheduled,
]);

throw new SpeedtestFailureException((string)$output);
throw new SpeedtestFailureException(json_encode($output));
}

Cache::flush();
Expand Down
2 changes: 1 addition & 1 deletion config/speedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
|--------------------------------------------------------------------------
*/

'version' => '1.11.0',
'version' => '1.11.1',

/*
|--------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/SpeedtestHelper/CheckOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CheckOutputTest extends TestCase
{
private OoklaTester $speedtestProvider;

public function __construct()
public function setUp(): void
{
$this->speedtestProvider = new OoklaTester();
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Helpers/SpeedtestHelper/SpeedtestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Tests\Unit\Helpers\SpeedtestHelper;

use App\Exceptions\SpeedtestFailureException;
use App\Helpers\SpeedtestHelper;
use App\Utils\OoklaTester;
use Illuminate\Foundation\Testing\RefreshDatabase;
use JsonException;
use Tests\TestCase;

class SpeedtestTest extends TestCase
Expand Down Expand Up @@ -58,11 +58,11 @@ public function testRunSpeedtestWithExistingOutput()
*/
public function testInvaidJson()
{
$this->expectException(SpeedtestFailureException::class);

$json = '{hi: hi}';

$o = $this->speedtestProvider->run($json);

$this->assertFalse($o);
}

/**
Expand All @@ -72,10 +72,10 @@ public function testInvaidJson()
*/
public function testIncompleteJson()
{
$this->expectException(SpeedtestFailureException::class);

$json = '{"hi": "hi"}';

$o = $this->speedtestProvider->run($json);

$this->assertFalse($o);
}
}

0 comments on commit 1d9fba6

Please sign in to comment.