Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Meta System #2570

Merged
merged 8 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/Bus/Commands/Incident/CreateIncidentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ final class CreateIncidentCommand
*/
public $template_vars;

/**
* Meta key/value pairs.
*
* @var array
*/
public $meta = [];

/**
* The validation rules.
*
Expand All @@ -112,6 +119,7 @@ final class CreateIncidentCommand
'stickied' => 'required|bool',
'occurred_at' => 'nullable|string',
'template' => 'nullable|string',
'meta' => 'required|array',
];

/**
Expand All @@ -128,10 +136,11 @@ final class CreateIncidentCommand
* @param string|null $occurred_at
* @param string|null $template
* @param array $template_vars
* @param array $meta
*
* @return void
*/
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [])
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [], $meta = [])
{
$this->name = $name;
$this->status = $status;
Expand All @@ -144,5 +153,6 @@ public function __construct($name, $status, $message, $visible, $component_id, $
$this->occurred_at = $occurred_at;
$this->template = $template;
$this->template_vars = $template_vars;
$this->meta = $meta;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\IncidentTemplate;
use CachetHQ\Cachet\Models\Meta;
use CachetHQ\Cachet\Services\Dates\DateFactory;
use Carbon\Carbon;
use Illuminate\Contracts\Auth\Guard;
Expand Down Expand Up @@ -100,6 +101,18 @@ public function handle(CreateIncidentCommand $command)
// Create the incident
$incident = Incident::create($data);

// Store any meta?
if ($meta = $command->meta) {
foreach ($meta as $key => $value) {
Meta::create([
'key' => $key,
'value' => $value,
'meta_type' => 'incidents',
'meta_id' => $incident->id,
]);
}
}

// Update the component.
if ($component = Component::find($command->component_id)) {
dispatch(new UpdateComponentCommand(
Expand Down
9 changes: 9 additions & 0 deletions app/Foundation/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use AltThree\Bus\Dispatcher;
use CachetHQ\Cachet\Bus\Middleware\UseDatabaseTransactions;
use CachetHQ\Cachet\Services\Dates\DateFactory;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -42,6 +43,14 @@ public function boot(Dispatcher $dispatcher)
Str::macro('canonicalize', function ($url) {
return preg_replace('/([^\/])$/', '$1/', $url);
});

Relation::morphMap([
'components' => \CachetHQ\Cachet\Models\Component::class,
'incidents' => \CachetHQ\Cachet\Models\Incident::class,
'metrics' => \CachetHQ\Cachet\Models\Metric::class,
'schedules' => \CachetHQ\Cachet\Models\Schedule::class,
'subscriber' => \CachetHQ\Cachet\Models\Subscriber::class,
]);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function postIncidents()
Binput::get('stickied', false),
Binput::get('occurred_at'),
Binput::get('template'),
Binput::get('vars', [])
Binput::get('vars', []),
Binput::get('meta', [])
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public function incidents()
return $this->hasMany(Incident::class, 'component_id', 'id');
}

/**
* Get all of the meta relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function meta()
{
return $this->morphMany(Meta::class, 'meta');
}

/**
* Get the tags relation.
*
Expand Down
15 changes: 14 additions & 1 deletion app/Models/Incident.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ class Incident extends Model implements HasPresenter
*
* @var string[]
*/
protected $with = ['updates'];
protected $with = [
'meta',
'updates',
];

/**
* Get the component relation.
Expand All @@ -157,6 +160,16 @@ public function component()
return $this->belongsTo(Component::class, 'component_id', 'id');
}

/**
* Get all of the meta relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function meta()
{
return $this->morphMany(Meta::class, 'meta');
}

/**
* Get the updates relation.
*
Expand Down
80 changes: 80 additions & 0 deletions app/Models/Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CachetHQ\Cachet\Models;

use AltThree\Validator\ValidatingTrait;
use Illuminate\Database\Eloquent\Model;

/**
* This is the meta model class.
*
* @author James Brooks <james@alt-three.com>
*/
class Meta extends Model
{
use ValidatingTrait;

/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'id' => 'int',
'key' => 'string',
'value' => 'json',
'meta_id' => 'int',
'meta_type' => 'string',
];

/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = [
'key',
'value',
'meta_id',
'meta_type',
];

/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'id' => 'nullable|int|min:1',
'key' => 'required|string',
'value' => 'nullable',
'meta_id' => 'required|int',
'meta_type' => 'required|string',
];

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'meta';

/**
* Get all of the owning meta models.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function meta()
{
return $this->morphTo();
}
}
10 changes: 10 additions & 0 deletions app/Models/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ public static function boot()
});
}

/**
* Get all of the meta relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function meta()
{
return $this->morphMany(Meta::class, 'meta');
}

/**
* Get the points relation.
*
Expand Down
30 changes: 20 additions & 10 deletions app/Models/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ class Schedule extends Model implements HasPresenter
*/
protected $with = ['components'];

/**
* Get the components relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function components()
{
return $this->hasMany(ScheduleComponent::class);
}

/**
* Get all of the meta relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function meta()
{
return $this->morphMany(Meta::class, 'meta');
}

/**
* Scopes schedules to those in the future.
*
Expand All @@ -155,16 +175,6 @@ public function scopePastSchedules($query)
return $query->where('status', '<', self::COMPLETE)->where('scheduled_at', '<=', Carbon::now());
}

/**
* Get the components relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function components()
{
return $this->hasMany(ScheduleComponent::class);
}

/**
* Get the presenter class.
*
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public static function boot()
});
}

/**
* Get all of the meta relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function meta()
{
return $this->morphMany(Meta::class, 'meta');
}

/**
* Get the subscriptions relation.
*
Expand Down
11 changes: 11 additions & 0 deletions app/Presenters/IncidentPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ public function duration()
return 0;
}

/**
* Return the meta in a key value pair.
*
* @return array
*/
public function meta()
{
return $this->wrappedObject->meta->pluck('value', 'key')->all();
}

/**
* Convert the presenter instance to an array.
*
Expand All @@ -294,6 +304,7 @@ public function toArray()
'latest_icon' => $this->latest_icon(),
'permalink' => $this->permalink(),
'duration' => $this->duration(),
'meta' => $this->meta(),
'occurred_at' => $this->occurred_at(),
'created_at' => $this->created_at(),
'updated_at' => $this->updated_at(),
Expand Down
46 changes: 46 additions & 0 deletions database/migrations/2017_06_13_181049_CreateMetaTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMetaTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('meta', function (Blueprint $table) {
$table->increments('id');
$table->string('key')->index();
$table->string('value');
$table->integer('meta_id')->unsigned();
$table->string('meta_type');
$table->timestamps();

$table->index(['meta_id', 'meta_type']);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('meta');
}
}
Loading