Skip to content

Commit 625c4fc

Browse files
Merge pull request #2 from cinderblockgames/homepage-api
Homepage api
2 parents 6741fab + 7d6544f commit 625c4fc

File tree

9 files changed

+237
-21
lines changed

9 files changed

+237
-21
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
## Speedtest Tracker
1+
# Speedtest Tracker
22

3-
### Introduction
3+
## Introduction
44

55
Speedtest Tracker is a self-hosted internet performance tracking application that runs speedtest checks against Ookla's Speedtest service.
66

77
### Why might I use this?
88

99
The main use case for Speedtest Tracker is to build a history of your internet's performance so that you can be informed when you're not receiving your ISP's advertised rates.
1010

11-
#### What about that other Speedtest Tracker?
11+
### What about that other Speedtest Tracker?
1212

13-
As far as I can tell https://github.com/henrywhitaker3/Speedtest-Tracker was abandoned. This version is meant to be an actively maintained replacement with an improved UI and feature set.
13+
As far as I can tell https://github.com/henrywhitaker3/Speedtest-Tracker was abandoned. This is meant to be an actively maintained replacement with an improved UI and feature set.
1414

15-
### Getting Started
15+
## Getting Started
1616

1717
Speedtest Tracker is containerized so you can run it anywhere you run your Docker containers. The [install](https://docs.speedtest-tracker.dev/getting-started/installation) documentation will get you up and running with using Docker or Docker Composer along with choosing a database (SQLite, MySQL/MariaDB or Postgresql).
1818

19-
#### FAQs and Features
19+
### FAQs and Features
2020

2121
[FAQs](https://docs.speedtest-tracker.dev/faqs) and a full list of planned and completed [features](https://docs.speedtest-tracker.dev/getting-started/features) can be found in the [documentation](https://docs.speedtest-tracker.dev).
2222

23-
### Screenshots
24-
#### Dashboard
23+
## Screenshots
24+
2525
![Dashboard](.github/screenshots/dashboard_screenshot.png)
26+
**Dashboard**
2627

27-
#### Results page
2828
![Results page](.github/screenshots/results_screenshot.png)
29+
**Results page**
2930

30-
#### General Settings page
3131
![General Settings page](.github/screenshots/general_settings_screenshot.png)
32+
** General Settings page**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Actions;
4+
5+
use App\Helpers\SpeedtestHelper;
6+
use App\Models\Speedtest;
7+
use DB;
8+
use Henrywhitaker3\LaravelActions\Interfaces\ActionInterface;
9+
10+
class GetLatestSpeedtestData implements ActionInterface
11+
{
12+
/**
13+
* Run the action.
14+
*
15+
* @return mixed
16+
*/
17+
public function run()
18+
{
19+
$data = SpeedtestHelper::latest();
20+
21+
// Homepage expects this to in Mbps. This calculation matches the results shown in the UI.
22+
$data['download'] /= 125000;
23+
$data['upload'] /= 125000;
24+
25+
$response = [
26+
'data' => $data,
27+
];
28+
29+
return $response;
30+
}
31+
}

app/Filament/Pages/Settings/GeneralPage.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,61 @@ protected function getFormSchema(): array
8686
->multiple()
8787
->maxItems(10)
8888
->searchable()
89-
->getSearchResultsUsing(function (string $search) {
90-
$url = "https://www.speedtest.net/api/js/servers?engine=js&search={$search}&https_functional=true&limit=10";
91-
92-
$response = Http::get($url);
89+
->options(function () {
90+
$response = Http::get(
91+
url: 'https://www.speedtest.net/api/js/servers',
92+
query: [
93+
'engine' => 'js',
94+
'https_functional' => true,
95+
'limit' => 10,
96+
]
97+
);
98+
99+
if ($response->failed()) {
100+
return [
101+
'' => 'There was an error retrieving Speedtest servers',
102+
];
103+
}
93104

94-
$options = $response->collect()->map(function ($item) {
105+
return $response->collect()->map(function ($item) {
95106
return [
96107
'id' => $item['id'],
97108
'name' => $item['id'].': '.$item['name'].' ('.$item['sponsor'].')',
98109
];
99-
});
110+
})->pluck('name', 'id');
111+
})
112+
->getSearchResultsUsing(function (string $search) {
113+
$response = Http::get(
114+
url: 'https://www.speedtest.net/api/js/servers',
115+
query: [
116+
'engine' => 'js',
117+
'search' => $search,
118+
'https_functional' => true,
119+
'limit' => 10,
120+
]
121+
);
122+
123+
if ($response->failed()) {
124+
return [
125+
'' => 'There was an error retrieving Speedtest servers',
126+
];
127+
}
100128

101-
if (! $options->count() && is_numeric($search)) {
102-
$options = collect([
129+
if (! $response->collect()->count() && is_numeric($search)) {
130+
return collect([
103131
[
104132
'id' => $search,
105133
'name' => $search.': No server found, manually add this ID.',
106134
],
107135
]);
108136
}
109137

110-
return $options->pluck('name', 'id');
138+
return $response->collect()->map(function ($item) {
139+
return [
140+
'id' => $item['id'],
141+
'name' => $item['id'].': '.$item['name'].' ('.$item['sponsor'].')',
142+
];
143+
})->pluck('name', 'id');
111144
})
112145
->columnSpan(2),
113146
])

app/Helpers/SpeedtestHelper.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Helpers;
4+
5+
use App\Models\Result;
6+
7+
class SpeedtestHelper
8+
{
9+
10+
/**
11+
* Returns the latest speedtest object.
12+
*
13+
* @return boolean|\App\Speedtest
14+
*/
15+
public static function latest()
16+
{
17+
$data = Result::latest()->get();
18+
19+
if ($data->isEmpty()) {
20+
return false;
21+
}
22+
23+
return $data->first();
24+
}
25+
26+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Actions\GetLatestSpeedtestData;
6+
use App\Actions\QueueSpeedtest;
7+
use App\Jobs\SpeedtestJob;
8+
use App\Models\Speedtest;
9+
use Carbon\Carbon;
10+
use Exception;
11+
use Illuminate\Http\Request;
12+
use Illuminate\Http\JsonResponse;
13+
14+
class SpeedtestController extends Controller
15+
{
16+
17+
/**
18+
* Return latest speedtest
19+
*
20+
* @return JsonResponse
21+
*/
22+
public function latest()
23+
{
24+
$data = run(GetLatestSpeedtestData::class);
25+
26+
if ($data['data']) {
27+
return response()->json($data, 200);
28+
} else {
29+
return response()->json([
30+
'method' => 'get latest speedtest',
31+
'error' => 'no speedtests have been run'
32+
], 404);
33+
}
34+
}
35+
36+
/**
37+
* Queue a new speedtest
38+
*
39+
* @return JsonResponse
40+
*/
41+
public function run()
42+
{
43+
try {
44+
run(QueueSpeedtest::class);
45+
46+
return response()->json([
47+
'method' => 'run speedtest',
48+
'data' => 'a new speedtest has been added to the queue'
49+
], 200);
50+
} catch (Exception $e) {
51+
return response()->json([
52+
'method' => 'run speedtest',
53+
'error' => $e
54+
], 500);
55+
}
56+
}
57+
58+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"filament/filament": "^2.0",
1313
"filament/spatie-laravel-settings-plugin": "^2.0",
1414
"guzzlehttp/guzzle": "^7.2",
15+
"henrywhitaker3/laravel-actions": "^1.0",
1516
"influxdata/influxdb-client-php": "^2.9",
1617
"laravel-notification-channels/telegram": "^3.0",
1718
"laravel/framework": "^9.19",

composer.lock

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/speedtest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* Build information
88
*/
9-
'build_date' => Carbon::parse('2023-03-07'),
9+
'build_date' => Carbon::parse('2023-03-09'),
1010

11-
'build_version' => '0.11.6',
11+
'build_version' => '0.11.7',
1212
];

routes/api.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Http\Controllers\SpeedtestController;
34
use Illuminate\Http\Request;
45
use Illuminate\Support\Facades\Route;
56

@@ -17,3 +18,11 @@
1718
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
1819
return $request->user();
1920
});
21+
22+
Route::group([
23+
'middleware' => ['api'],
24+
'prefix' => 'speedtest'
25+
], function ($router) {
26+
Route::get('latest', [SpeedtestController::class, 'latest'])
27+
->name('speedtest.latest');
28+
});

0 commit comments

Comments
 (0)