From 5332daf7f36a83c04a80b35f355e7eb598e24035 Mon Sep 17 00:00:00 2001 From: Thibault Debatty <thibault.debatty@gmail.com> Date: Thu, 25 Jun 2020 12:32:01 +0200 Subject: [PATCH] SensorWrapper holds the records --- app/Http/Controllers/ServerController.php | 4 +--- app/SensorWrapper.php | 12 ++++++---- app/Server.php | 22 ++++++++++-------- resources/assets/js/bootstrap.js | 2 -- .../assets/js/components/ExampleComponent.vue | 23 ------------------- resources/views/server/show.blade.php | 8 +++---- 6 files changed, 24 insertions(+), 47 deletions(-) delete mode 100644 resources/assets/js/components/ExampleComponent.vue diff --git a/app/Http/Controllers/ServerController.php b/app/Http/Controllers/ServerController.php index 38f3a30..e9a414c 100644 --- a/app/Http/Controllers/ServerController.php +++ b/app/Http/Controllers/ServerController.php @@ -66,9 +66,7 @@ class ServerController extends Controller */ public function show(Server $server) { - return view("server.show", [ - "server" => $server, - "records" => $server->lastRecords1Day()]); + return view("server.show", ["server" => $server]); } /** diff --git a/app/SensorWrapper.php b/app/SensorWrapper.php index 1e80d42..2f0e8ec 100644 --- a/app/SensorWrapper.php +++ b/app/SensorWrapper.php @@ -7,13 +7,15 @@ use Illuminate\Support\Facades\Log; class SensorWrapper { private $sensor; + private $records; private $report; private $status; - public function __construct(Sensor $sensor) + public function __construct(Sensor $sensor, array $records) { $this->sensor = $sensor; + $this->records = $records; } public function id() : string @@ -26,11 +28,11 @@ class SensorWrapper return $this->sensor->name(); } - public function report(array $records): string + public function report(): string { if (is_null($this->report)) { try { - $this->report = $this->sensor->report($records); + $this->report = $this->sensor->report($this->records); } catch (\Exception $ex) { Log::error('Sensor failed : ' . $ex->getTraceAsString()); $this->report = "<p>Sensor " . $this->getName() . " failed :-(</p>"; @@ -40,11 +42,11 @@ class SensorWrapper return $this->report; } - public function status(array $records): Status + public function status(): Status { if (is_null($this->status)) { try { - $this->status = new Status($this->sensor->status($records)); + $this->status = new Status($this->sensor->status($this->records)); } catch (\Exception $ex) { Log::error('Sensor failed : ' . $ex->getTraceAsString()); $this->status = new Status(Status::UNKNOWN); diff --git a/app/Server.php b/app/Server.php index 510ef8a..dfdb879 100644 --- a/app/Server.php +++ b/app/Server.php @@ -96,10 +96,12 @@ class Server extends Model return $hearbeat->lastRecordTime($this->lastRecord()); } - public function clientVersion(array $records) : string + public function clientVersion() : string { $sensor = new \App\Sensor\ClientVersion($this); - return $sensor->installedVersion($records); + return $sensor->installedVersion([ + $this->lastRecord() + ]); } public function lastClientUrl() @@ -110,21 +112,20 @@ class Server extends Model /** * - * @param array $records * @return \App\Status */ - public function status(array $records) : Status + public function status() : Status { - return Status::max($this->statusArray($records)); + return Status::max($this->statusArray()); } - public function statusArray(array $records) + public function statusArray() { $status_array = []; foreach ($this->getSensors() as $sensor) { $sensor_name = $sensor->id(); try { - $status_array[$sensor_name] = $sensor->status($records); + $status_array[$sensor_name] = $sensor->status(); } catch (\Exception $ex) { $status_array[$sensor_name] = Sensor::STATUS_UNKNOWN; Log::error("Sensor $sensor_name failed : " . $ex->getTraceAsString()); @@ -133,11 +134,11 @@ class Server extends Model return $status_array; } - public function getSensorsNOK(array $records) + public function getSensorsNOK() { $sensorsNOK = []; foreach ($this->getSensors() as $sensor) { - if ($sensor->status($records)->code() > 0) { + if ($sensor->status()->code() > 0) { $sensorsNOK[] = $sensor; } } @@ -146,10 +147,11 @@ class Server extends Model public function getSensors() { + $records = $this->lastRecords1Day(); $sensors = []; foreach (self::$sensors as $sensor) { - $sensors[] = new SensorWrapper(new $sensor()); + $sensors[] = new SensorWrapper(new $sensor(), $records); } return $sensors; } diff --git a/resources/assets/js/bootstrap.js b/resources/assets/js/bootstrap.js index 9d6e58c..c412458 100644 --- a/resources/assets/js/bootstrap.js +++ b/resources/assets/js/bootstrap.js @@ -20,7 +20,6 @@ try { */ window.axios = require('axios'); - window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; /** @@ -30,7 +29,6 @@ window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; */ let token = document.head.querySelector('meta[name="csrf-token"]'); - if (token) { window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content; } else { diff --git a/resources/assets/js/components/ExampleComponent.vue b/resources/assets/js/components/ExampleComponent.vue deleted file mode 100644 index 601e61c..0000000 --- a/resources/assets/js/components/ExampleComponent.vue +++ /dev/null @@ -1,23 +0,0 @@ -<template> - <div class="container"> - <div class="row"> - <div class="col-md-8 col-md-offset-2"> - <div class="panel panel-default"> - <div class="panel-heading">Example Component</div> - - <div class="panel-body"> - I'm an example component! - </div> - </div> - </div> - </div> - </div> -</template> - -<script> - export default { - mounted() { - console.log('Component mounted.') - } - } -</script> diff --git a/resources/views/server/show.blade.php b/resources/views/server/show.blade.php index 36e9b10..184f2dc 100644 --- a/resources/views/server/show.blade.php +++ b/resources/views/server/show.blade.php @@ -26,7 +26,7 @@ window.monitorServerToken = "{{ $server->read_token }}"; <div class="col-md-4"> <div class="card"> <div class="card-body"> - <p>{!! $server->status($records)->badge() !!}</p> + <p>{!! $server->status()->badge() !!}</p> <p> Last heartbeet:<br> @@ -34,7 +34,7 @@ window.monitorServerToken = "{{ $server->read_token }}"; ({{ $server->lastRecordTime()->diffForHumans() }}) </p> - <p>Client version: {{ $server->clientVersion($records) }}</p> + <p>Client version: {{ $server->clientVersion() }}</p> <p>Uptime: {{ $server->uptime() }}</p> </div> @@ -98,11 +98,11 @@ window.monitorServerToken = "{{ $server->read_token }}"; {{ $sensor->name() }} <div class="float-right"> - {!! $sensor->status($records)->badge() !!} + {!! $sensor->status()->badge() !!} </div> </div> <div class="card-body"> - {!! $sensor->report($records) !!} + {!! $sensor->report() !!} </div> </div> @endforeach -- GitLab