Skip to content
Snippets Groups Projects
Commit 5332daf7 authored by Tibo's avatar Tibo
Browse files

SensorWrapper holds the records

parent abf14909
No related branches found
No related tags found
No related merge requests found
......@@ -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]);
}
/**
......
......@@ -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);
......
......@@ -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;
}
......
......@@ -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 {
......
<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>
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment