Skip to content
Snippets Groups Projects
Commit 200b6370 authored by Thibault Debatty's avatar Thibault Debatty
Browse files

store status changes in mysql instead of mongo

parent f6c7a851
No related branches found
No related tags found
No related merge requests found
...@@ -29,7 +29,7 @@ class Kernel extends ConsoleKernel ...@@ -29,7 +29,7 @@ class Kernel extends ConsoleKernel
// $schedule->command('inspire') // $schedule->command('inspire')
// ->hourly(); // ->hourly();
$schedule->job(new StatusChangeDetection())->everyFiveMinutes(); $schedule->job(new StatusChangeDetection())->everyMinute();
} }
/** /**
......
...@@ -44,8 +44,20 @@ class StatusChangeDetection implements ShouldQueue ...@@ -44,8 +44,20 @@ class StatusChangeDetection implements ShouldQueue
public function detectChangeForServer(Server $server) public function detectChangeForServer(Server $server)
{ {
$last_change = StatusChange::getLastChangeForServer($server->id); $last_change = $server->lastChange();
$current_status = $server->status(); $current_status = $server->status();
// probably a new server:
// never been a change detection before
if ($last_change === null) {
$change = new StatusChange();
$change->server_id = $server->id;
$change->time = time();
$change->status = $current_status->code();
$change->record_id = $server->lastRecord()->id;
$change->save();
return;
}
if ($last_change->status == $current_status->code()) { if ($last_change->status == $current_status->code()) {
// no change // no change
...@@ -56,6 +68,7 @@ class StatusChangeDetection implements ShouldQueue ...@@ -56,6 +68,7 @@ class StatusChangeDetection implements ShouldQueue
$change->server_id = $server->id; $change->server_id = $server->id;
$change->time = time(); $change->time = time();
$change->status = $current_status->code(); $change->status = $current_status->code();
$change->record_id = $server->lastRecord()->id;
$change->save(); $change->save();
$this->sendNotificationIfRequired($change); $this->sendNotificationIfRequired($change);
......
<?php
namespace App;
use MongoDB\Client;
/**
* Description of Mongo
*
* @author tibo
*/
class Mongo
{
private static $mongo;
public static function get()
{
if (self::$mongo == null) {
$uri = config('services.mongo.uri');
$uriOptions = config('services.mongo.uriOptions');
$driverOptions = config('services.mongo.driverOptions');
self::$mongo = new Client($uri, $uriOptions, $driverOptions);
}
return self::$mongo;
}
}
...@@ -14,7 +14,7 @@ class LoadAvg extends AbstractSensor ...@@ -14,7 +14,7 @@ class LoadAvg extends AbstractSensor
{ {
/** /**
* *
* @param array<Record> $records * @param array<Record> $records
* @return string * @return string
*/ */
...@@ -27,8 +27,9 @@ class LoadAvg extends AbstractSensor ...@@ -27,8 +27,9 @@ class LoadAvg extends AbstractSensor
$current_load = $this->parse($record->data["loadavg"]); $current_load = $this->parse($record->data["loadavg"]);
return view( return view(
"agent.loadavg", "agent.loadavg",
["current_load" => $current_load]); ["current_load" => $current_load]
);
} }
public function loadPoints(array $records) public function loadPoints(array $records)
......
...@@ -97,7 +97,6 @@ class Server extends Model ...@@ -97,7 +97,6 @@ class Server extends Model
} }
return $this->records_1day; return $this->records_1day;
} }
public function hasData() : bool public function hasData() : bool
...@@ -159,15 +158,19 @@ class Server extends Model ...@@ -159,15 +158,19 @@ class Server extends Model
} }
return $sensors; return $sensors;
} }
public function getChanges($count = 10) public function changes()
{ {
return []; return $this->hasMany(StatusChange::class);
//return StatusChange::getLastChangesForServer($this->id, $count);
} }
public static function id(int $id) : Server public function lastChanges($count = 10)
{
return $this->changes()->orderBy("time", "desc")->limit($count)->get();
}
public function lastChange() : ?StatusChange
{ {
return self::where("id", $id)->first(); return $this->changes()->latest("time")->first();
} }
} }
...@@ -2,37 +2,22 @@ ...@@ -2,37 +2,22 @@
namespace App; namespace App;
use App\Mongo; use Illuminate\Database\Eloquent\Model;
use \Carbon\Carbon; use \Carbon\Carbon;
/** /**
* Represents a change of status, that will be saved in MongoDB. * Represents a change of status.
*
* @property int $time
* @property int $server_id
* @property int $status
* @property int $record_id
* *
* @author tibo * @author tibo
*/ */
class StatusChange class StatusChange extends Model
{ {
public $timestamps = false;
public $server_id = 0;
public $status = 0;
public $time = 0;
public $id;
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 status() : Status public function status() : Status
{ {
...@@ -44,65 +29,8 @@ class StatusChange ...@@ -44,65 +29,8 @@ class StatusChange
return Carbon::createFromTimestamp($this->time); return Carbon::createFromTimestamp($this->time);
} }
public function server() : Server public function server()
{
return Server::id($this->server_id);
}
public function save()
{
$this->time = time();
$data = [
"time" => $this->time,
"server_id" => $this->server_id,
"status" => $this->status,
];
$collection = Mongo::get()->monitoring->statuschanges;
$r = $collection->insertOne($data);
$this->id = $r->getInsertedId()->__toString();
}
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 find(string $id) : StatusChange
{
$collection = Mongo::get()->monitoring->statuschanges;
$result = $collection->findOne(
['_id' => new \MongoDB\BSON\ObjectId($id)]
);
$status = new StatusChange();
$status->parse($result);
$status->id = $id;
return $status;
}
public static function getLastChangeForServer(int $server_id) : StatusChange
{ {
$collection = Mongo::get()->monitoring->statuschanges; return $this->belongsTo(Server::class);
$record = $collection->findOne(
["server_id" => $server_id],
["sort" => ["_id" => -1]]
);
$change = new StatusChange();
$change->server_id = $server_id;
$change->parse($record);
return $change;
} }
} }
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTableStatusChanges extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('status_changes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer("time")->index();
$table->integer("server_id");
$table->integer("status");
$table->integer("record_id");
$table->foreign("server_id")->references("id")->on("servers");
$table->foreign("record_id")->references("id")->on("records");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('status_changes');
}
}
...@@ -95,7 +95,7 @@ window.monitorServerToken = "{{ $server->read_token }}"; ...@@ -95,7 +95,7 @@ window.monitorServerToken = "{{ $server->read_token }}";
</div> </div>
<div class="card-body"> <div class="card-body">
<table class='table table-sm'> <table class='table table-sm'>
@foreach($server->getChanges() as $change) @foreach($server->lastChanges() as $change)
<tr> <tr>
<td>{{ $change->getTimeCarbon()->toDateTimeString() }}</td> <td>{{ $change->getTimeCarbon()->toDateTimeString() }}</td>
<td>{!! $change->status()->badge() !!}</td> <td>{!! $change->status()->badge() !!}</td>
......
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