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

save and show the record that triggered a report

parent 320fea78
No related branches found
No related tags found
No related merge requests found
Pipeline #13339 passed
...@@ -118,7 +118,7 @@ class AgentScheduler ...@@ -118,7 +118,7 @@ class AgentScheduler
} }
foreach ($this->triggers[$trigger_label] as $agent) { foreach ($this->triggers[$trigger_label] as $agent) {
RunAgent::dispatch($agent, $record->server); RunAgent::dispatch($agent, $record);
} }
} }
} }
<?php
namespace App\Http\Controllers;
use App\Record;
class RecordController extends Controller
{
public function show(Record $record)
{
return view("record.show")->with(["record" => $record]);
}
}
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
namespace App\Jobs; namespace App\Jobs;
use App\Sensor; use App\Sensor;
use App\Server; use App\Record;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
...@@ -24,19 +24,19 @@ class RunAgent implements ShouldQueue ...@@ -24,19 +24,19 @@ class RunAgent implements ShouldQueue
/** /**
* *
* @var Server * @var Record
*/ */
public $server; public $record;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @return void * @return void
*/ */
public function __construct(Sensor $agent, Server $server) public function __construct(Sensor $agent, Record $record)
{ {
$this->agent = $agent; $this->agent = $agent;
$this->server = $server; $this->record = $record;
} }
/** /**
...@@ -47,12 +47,16 @@ class RunAgent implements ShouldQueue ...@@ -47,12 +47,16 @@ class RunAgent implements ShouldQueue
public function handle() public function handle()
{ {
$trigger_label = $this->agent->config()->trigger_label; $trigger_label = $this->agent->config()->trigger_label;
$records = $this->server->lastRecords($trigger_label); $record = $this->record;
$server = $this->record->server;
$report = $this->agent->analyze($records, $this->server->info()); $records = $server->lastRecords($trigger_label);
$report = $this->agent->analyze($records, $server->info());
$report->time = time(); $report->time = time();
$report->server_id = $this->server->id; $report->server_id = $server->id;
$report->label = $this->agent->config()->label; $report->label = $this->agent->config()->label;
$report->record_id = $record->id;
$report->save(); $report->save();
} }
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App; namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
...@@ -30,4 +31,9 @@ class Record extends Model ...@@ -30,4 +31,9 @@ class Record extends Model
return false; return false;
} }
public function time() : Carbon
{
return Carbon::createFromTimestamp($this->time);
}
} }
...@@ -33,6 +33,11 @@ class Report extends Model implements HasStatus ...@@ -33,6 +33,11 @@ class Report extends Model implements HasStatus
parent::__construct($attributes); parent::__construct($attributes);
} }
public function record()
{
return $this->belongsTo(Record::class);
}
public function setTitle(string $title) : Report public function setTitle(string $title) : Report
{ {
$this->title = $title; $this->title = $title;
......
...@@ -194,7 +194,7 @@ class ServerInfo ...@@ -194,7 +194,7 @@ class ServerInfo
{ {
$record = $this->server->lastRecord("version"); $record = $this->server->lastRecord("version");
if (is_null($record)) { if (is_null($record)) {
return new Carbon(); return Carbon::createFromTimestamp(0);
} }
return Carbon::createFromTimestamp($record->time); return Carbon::createFromTimestamp($record->time);
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ReportsAddRecordId extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('reports', function (Blueprint $table) {
$table->unsignedBigInteger("record_id")->nullable();
$table->foreign('record_id')->references('id')->on('records')
->onDelete("cascade");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('reports', function (Blueprint $table) {
//
});
}
}
@extends('layouts.app')
@section('content')
<div class="container">
<p>
<span class="badge badge-secondary">#{{ $record->id }}</span>
<span class="badge badge-primary">{{ $record->label }}</span>
<a class="badge badge-primary"
href="{{ action("ServerController@show", ["server" => $record->server]) }}">
<i class="fas fa-desktop"></i> {{ $record->server->name }}
</a>
<span class="badge badge-secondary">
<i class="far fa-clock"></i> {{ $record->time() }}
</span>
</p>
<div class="card">
<div class="card-body">
<pre><code class="small">{{ $record->data }}</code></pre>
</div>
</div>
</div>
@endsection
...@@ -59,6 +59,10 @@ window.monitorServerToken = "{{ $server->read_token }}"; ...@@ -59,6 +59,10 @@ window.monitorServerToken = "{{ $server->read_token }}";
{{ $report->title() }} {{ $report->title() }}
<div class="float-right"> <div class="float-right">
<a class="badge badge-secondary"
href="{{ action("RecordController@show", ["record" => $report->record]) }}">
<i class="fas fa-search"></i>
</a>
{!! $report->status()->badge() !!} {!! $report->status()->badge() !!}
</div> </div>
</div> </div>
......
...@@ -55,3 +55,4 @@ Route::get( ...@@ -55,3 +55,4 @@ Route::get(
Route::resource('app/organizations', 'OrganizationController'); Route::resource('app/organizations', 'OrganizationController');
Route::resource("app/organizations.user", "OrganizationUserController")->only(["create", "store", "destroy"]); Route::resource("app/organizations.user", "OrganizationUserController")->only(["create", "store", "destroy"]);
Route::resource('app/servers', 'ServerController')->except(["index"]); Route::resource('app/servers', 'ServerController')->except(["index"]);
Route::get("app/records/{record}", "RecordController@show");
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