diff --git a/web/app/Server.php b/web/app/Server.php index eea1d982e2c1785a46a0b4de85f3d2be0ef824f6..bb3d62131e87ada7b8dae0e7ae99f210dff0f217 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 8256fcd22f32cfe95be440614e3e6fb862b86fb7..3ab7b641f2ccf845ea41f92aecea71746de886a9 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 7ff765c42f3c46811c9ce353f31937b2e3225efc..d2ead56219a1b3e0645ae2bb4d5d38640c6d209f 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