From b61278bf7aa019d20c4d0f2f73726a6f73f13dda Mon Sep 17 00:00:00 2001 From: Thibault Debatty <thibault.debatty@gmail.com> Date: Thu, 24 Jan 2019 13:09:44 +0100 Subject: [PATCH] Show status history --- web/app/Server.php | 4 ++ web/app/StatusChange.php | 46 +++++++++++++++++++---- web/resources/views/server/show.blade.php | 16 ++++++++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/web/app/Server.php b/web/app/Server.php index eea1d98..bb3d621 100644 --- a/web/app/Server.php +++ b/web/app/Server.php @@ -220,4 +220,8 @@ class Server extends Model return $this->parseProductName($record->system); } + + public function getChanges($count = 10) { + return \App\StatusChange::getLastChangesForServer($this->id, $count); + } } diff --git a/web/app/StatusChange.php b/web/app/StatusChange.php index 8256fcd..3ab7b64 100644 --- a/web/app/StatusChange.php +++ b/web/app/StatusChange.php @@ -2,6 +2,8 @@ namespace App; +use \Carbon\Carbon; + /** * Represents a change of status, that will be saved in MongoDB. * @@ -13,6 +15,29 @@ class StatusChange { public $status = 0; public $time = 0; + public function parse($array) { + if ($array == null) { + return; + } + + $fields = ["server_id", "status", "time"]; + foreach ($fields as $field) { + if (isset($array[$field])) { + $this->$field = $array[$field]; + } + } + + return $this; + } + + public function getStatusBadge() { + return AbstractSensor::getBadgeForStatus($this->status); + } + + public function getTimeCarbon() : Carbon { + return Carbon::createFromTimestamp($this->time); + } + public static function save($status) { $data = [ "time" => time(), @@ -25,6 +50,19 @@ class StatusChange { $collection->insertOne($data); } + public static function getLastChangesForServer(int $server_id, int $count) : array { + $collection = \Mongo::get()->monitoring->statuschanges; + $records = $collection->find( + ["server_id" => $server_id], + ["limit" => $count, "sort" => ["_id" => -1]]); + + $changes = []; + foreach ($records as $record) { + $changes[] = (new StatusChange())->parse($record); + } + return $changes; + } + public static function getLastChangeForServer(int $server_id) : StatusChange { $collection = \Mongo::get()->monitoring->statuschanges; $record = $collection->findOne( @@ -33,13 +71,7 @@ class StatusChange { $change = new StatusChange(); $change->server_id = $server_id; - - if ($record == null) { - return $change; - } - - $change->time = $record["time"]; - $change->status = $record["status"]; + $change->parse($record); return $change; } } diff --git a/web/resources/views/server/show.blade.php b/web/resources/views/server/show.blade.php index 7ff765c..d2ead56 100644 --- a/web/resources/views/server/show.blade.php +++ b/web/resources/views/server/show.blade.php @@ -94,6 +94,22 @@ window.monitorServerToken = "{{ $server->read_token }}"; </div> @endforeach + <div class="card"> + <div class="card-header"> + History + </div> + <div class="card-body"> + <table class='table table-sm'> + @foreach($server->getChanges() as $change) + <tr> + <td>{{ $change->getTimeCarbon()->toDateTimeString() }}</td> + <td>{!! $change->getStatusBadge() !!}</td> + </tr> + @endforeach + </table> + </div> + </div> + <h3>PHP Client installation</h3> <pre style="font-size: 75%; background: #ddd; overflow: hidden"><code> wget https://gitlab.cylab.be/cylab/monitoring/raw/master/php-client/bin/monitor.phar -- GitLab