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

Create and save Notification object on status change (intermediate step)

parent e4e2d54e
No related branches found
No related tags found
No related merge requests found
......@@ -18,12 +18,14 @@ namespace App{
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property string $name
* @property string $dashboard_token
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Server[] $servers
* @property-read \Illuminate\Database\Eloquent\Collection|\App\User[] $users
* @method static \Illuminate\Database\Eloquent\Builder|\App\Organization newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Organization newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Organization query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Organization whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Organization whereDashboardToken($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Organization whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Organization whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Organization whereUpdatedAt($value)
......@@ -84,3 +86,27 @@ namespace App{
class Server extends \Eloquent {}
}
namespace App{
/**
* App\Notification
*
* @property int $id
* @property \Illuminate\Support\Carbon $created_at
* @property \Illuminate\Support\Carbon $updated_at
* @property int $server_id
* @property string $change_id
* @property string $type
* @property-read \App\Server $server
* @method static \Illuminate\Database\Eloquent\Builder|\App\Notification newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Notification newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Notification query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Notification whereChangeId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Notification whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Notification whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Notification whereServerId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Notification whereType($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Notification whereUpdatedAt($value)
*/
class Notification extends \Eloquent {}
}
......@@ -2,6 +2,7 @@
namespace App\Jobs;
use App\Notification;
use App\Organization;
use App\Server;
use App\StatusChange;
......@@ -56,7 +57,20 @@ class StatusChangeDetection implements ShouldQueue
$change->server_id = $server->id;
$change->time = time();
$change->status = $current_status;
StatusChange::save($change);
$change->save();
$this->sendNotificationIfRequired($change);
}
public function sendNotificationIfRequired(StatusChange $change)
{
$server = $change->server();
$notification = new Notification();
$notification->server()->associate($server);
$notification->type = "change";
$notification->change_id = $change->id;
$notification->save();
foreach ($server->organization->users as $user) {
Mail::to($user)->send(new \App\Mail\StatusChanged($change));
......
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
protected $dateFormat = 'U';
public function server()
{
return $this->belongsTo('\App\Server');
}
}
......@@ -15,6 +15,7 @@ class StatusChange
public $server_id = 0;
public $status = 0;
public $time = 0;
public $id;
public function parse($array)
{
......@@ -52,17 +53,19 @@ class StatusChange
return Server::id($this->server_id);
}
public static function save($status)
public function save()
{
$data = [
"time" => time(),
"server_id" => $status->server_id,
"status" => $status->status,
$this->time = time();
$data = [
"time" => $this->time,
"server_id" => $this->server_id,
"status" => $this->status,
];
$collection = \Mongo::get()->monitoring->statuschanges;
$collection->insertOne($data);
$r = $collection->insertOne($data);
$this->id = $r->getInsertedId()->__toString();
}
public static function getLastChangesForServer(int $server_id, int $count) : array
......
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNotificationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('created_at');
$table->unsignedInteger('updated_at');
$table->unsignedInteger('server_id');
$table->string('change_id');
$table->string('type');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notifications');
}
}
......@@ -179,7 +179,7 @@ class ExampleTest extends TestCase
$change = new \App\StatusChange();
$change->status = 155;
$change->server_id = $server_id;
\App\StatusChange::save($change);
$change->save();
// Run change detection
$change_detection_job = new \App\Jobs\StatusChangeDetection();
......
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