Skip to content

Commit bd34264

Browse files
authored
Merge pull request #2570 from CachetHQ/system-meta
Global Meta System
2 parents 5fecccc + c9c462b commit bd34264

File tree

15 files changed

+290
-14
lines changed

15 files changed

+290
-14
lines changed

app/Bus/Commands/Incident/CreateIncidentCommand.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ final class CreateIncidentCommand
9696
*/
9797
public $template_vars;
9898

99+
/**
100+
* Meta key/value pairs.
101+
*
102+
* @var array
103+
*/
104+
public $meta = [];
105+
99106
/**
100107
* The validation rules.
101108
*
@@ -112,6 +119,7 @@ final class CreateIncidentCommand
112119
'stickied' => 'required|bool',
113120
'occurred_at' => 'nullable|string',
114121
'template' => 'nullable|string',
122+
'meta' => 'required|array',
115123
];
116124

117125
/**
@@ -128,10 +136,11 @@ final class CreateIncidentCommand
128136
* @param string|null $occurred_at
129137
* @param string|null $template
130138
* @param array $template_vars
139+
* @param array $meta
131140
*
132141
* @return void
133142
*/
134-
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [])
143+
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [], $meta = [])
135144
{
136145
$this->name = $name;
137146
$this->status = $status;
@@ -144,5 +153,6 @@ public function __construct($name, $status, $message, $visible, $component_id, $
144153
$this->occurred_at = $occurred_at;
145154
$this->template = $template;
146155
$this->template_vars = $template_vars;
156+
$this->meta = $meta;
147157
}
148158
}

app/Bus/Handlers/Commands/Incident/CreateIncidentCommandHandler.php

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CachetHQ\Cachet\Models\Component;
1919
use CachetHQ\Cachet\Models\Incident;
2020
use CachetHQ\Cachet\Models\IncidentTemplate;
21+
use CachetHQ\Cachet\Models\Meta;
2122
use CachetHQ\Cachet\Services\Dates\DateFactory;
2223
use Carbon\Carbon;
2324
use Illuminate\Contracts\Auth\Guard;
@@ -100,6 +101,18 @@ public function handle(CreateIncidentCommand $command)
100101
// Create the incident
101102
$incident = Incident::create($data);
102103

104+
// Store any meta?
105+
if ($meta = $command->meta) {
106+
foreach ($meta as $key => $value) {
107+
Meta::create([
108+
'key' => $key,
109+
'value' => $value,
110+
'meta_type' => 'incidents',
111+
'meta_id' => $incident->id,
112+
]);
113+
}
114+
}
115+
103116
// Update the component.
104117
if ($component = Component::find($command->component_id)) {
105118
dispatch(new UpdateComponentCommand(

app/Foundation/Providers/AppServiceProvider.php

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use AltThree\Bus\Dispatcher;
1515
use CachetHQ\Cachet\Bus\Middleware\UseDatabaseTransactions;
1616
use CachetHQ\Cachet\Services\Dates\DateFactory;
17+
use Illuminate\Database\Eloquent\Relations\Relation;
1718
use Illuminate\Support\ServiceProvider;
1819
use Illuminate\Support\Str;
1920

@@ -42,6 +43,14 @@ public function boot(Dispatcher $dispatcher)
4243
Str::macro('canonicalize', function ($url) {
4344
return preg_replace('/([^\/])$/', '$1/', $url);
4445
});
46+
47+
Relation::morphMap([
48+
'components' => \CachetHQ\Cachet\Models\Component::class,
49+
'incidents' => \CachetHQ\Cachet\Models\Incident::class,
50+
'metrics' => \CachetHQ\Cachet\Models\Metric::class,
51+
'schedules' => \CachetHQ\Cachet\Models\Schedule::class,
52+
'subscriber' => \CachetHQ\Cachet\Models\Subscriber::class,
53+
]);
4554
}
4655

4756
/**

app/Http/Controllers/Api/IncidentController.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public function postIncidents()
7878
Binput::get('stickied', false),
7979
Binput::get('occurred_at'),
8080
Binput::get('template'),
81-
Binput::get('vars', [])
81+
Binput::get('vars', []),
82+
Binput::get('meta', [])
8283
));
8384
} catch (QueryException $e) {
8485
throw new BadRequestHttpException();

app/Models/Component.php

+10
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ public function incidents()
134134
return $this->hasMany(Incident::class, 'component_id', 'id');
135135
}
136136

137+
/**
138+
* Get all of the meta relation.
139+
*
140+
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
141+
*/
142+
public function meta()
143+
{
144+
return $this->morphMany(Meta::class, 'meta');
145+
}
146+
137147
/**
138148
* Get the tags relation.
139149
*

app/Models/Incident.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ class Incident extends Model implements HasPresenter
145145
*
146146
* @var string[]
147147
*/
148-
protected $with = ['updates'];
148+
protected $with = [
149+
'meta',
150+
'updates',
151+
];
149152

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

163+
/**
164+
* Get all of the meta relation.
165+
*
166+
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
167+
*/
168+
public function meta()
169+
{
170+
return $this->morphMany(Meta::class, 'meta');
171+
}
172+
160173
/**
161174
* Get the updates relation.
162175
*

app/Models/Meta.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Cachet.
5+
*
6+
* (c) Alt Three Services Limited
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CachetHQ\Cachet\Models;
13+
14+
use AltThree\Validator\ValidatingTrait;
15+
use Illuminate\Database\Eloquent\Model;
16+
17+
/**
18+
* This is the meta model class.
19+
*
20+
* @author James Brooks <james@alt-three.com>
21+
*/
22+
class Meta extends Model
23+
{
24+
use ValidatingTrait;
25+
26+
/**
27+
* The attributes that should be casted to native types.
28+
*
29+
* @var string[]
30+
*/
31+
protected $casts = [
32+
'id' => 'int',
33+
'key' => 'string',
34+
'value' => 'json',
35+
'meta_id' => 'int',
36+
'meta_type' => 'string',
37+
];
38+
39+
/**
40+
* The fillable properties.
41+
*
42+
* @var string[]
43+
*/
44+
protected $fillable = [
45+
'key',
46+
'value',
47+
'meta_id',
48+
'meta_type',
49+
];
50+
51+
/**
52+
* The validation rules.
53+
*
54+
* @var string[]
55+
*/
56+
public $rules = [
57+
'id' => 'nullable|int|min:1',
58+
'key' => 'required|string',
59+
'value' => 'nullable',
60+
'meta_id' => 'required|int',
61+
'meta_type' => 'required|string',
62+
];
63+
64+
/**
65+
* The table associated with the model.
66+
*
67+
* @var string
68+
*/
69+
protected $table = 'meta';
70+
71+
/**
72+
* Get all of the owning meta models.
73+
*
74+
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
75+
*/
76+
public function meta()
77+
{
78+
return $this->morphTo();
79+
}
80+
}

app/Models/Metric.php

+10
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ public static function boot()
165165
});
166166
}
167167

168+
/**
169+
* Get all of the meta relation.
170+
*
171+
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
172+
*/
173+
public function meta()
174+
{
175+
return $this->morphMany(Meta::class, 'meta');
176+
}
177+
168178
/**
169179
* Get the points relation.
170180
*

app/Models/Schedule.php

+20-10
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ class Schedule extends Model implements HasPresenter
131131
*/
132132
protected $with = ['components'];
133133

134+
/**
135+
* Get the components relation.
136+
*
137+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
138+
*/
139+
public function components()
140+
{
141+
return $this->hasMany(ScheduleComponent::class);
142+
}
143+
144+
/**
145+
* Get all of the meta relation.
146+
*
147+
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
148+
*/
149+
public function meta()
150+
{
151+
return $this->morphMany(Meta::class, 'meta');
152+
}
153+
134154
/**
135155
* Scopes schedules to those in the future.
136156
*
@@ -155,16 +175,6 @@ public function scopePastSchedules($query)
155175
return $query->where('status', '<', self::COMPLETE)->where('scheduled_at', '<=', Carbon::now());
156176
}
157177

158-
/**
159-
* Get the components relation.
160-
*
161-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
162-
*/
163-
public function components()
164-
{
165-
return $this->hasMany(ScheduleComponent::class);
166-
}
167-
168178
/**
169179
* Get the presenter class.
170180
*

app/Models/Subscriber.php

+10
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public static function boot()
9090
});
9191
}
9292

93+
/**
94+
* Get all of the meta relation.
95+
*
96+
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
97+
*/
98+
public function meta()
99+
{
100+
return $this->morphMany(Meta::class, 'meta');
101+
}
102+
93103
/**
94104
* Get the subscriptions relation.
95105
*

app/Presenters/IncidentPresenter.php

+11
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@ public function duration()
279279
return 0;
280280
}
281281

282+
/**
283+
* Return the meta in a key value pair.
284+
*
285+
* @return array
286+
*/
287+
public function meta()
288+
{
289+
return $this->wrappedObject->meta->pluck('value', 'key')->all();
290+
}
291+
282292
/**
283293
* Convert the presenter instance to an array.
284294
*
@@ -294,6 +304,7 @@ public function toArray()
294304
'latest_icon' => $this->latest_icon(),
295305
'permalink' => $this->permalink(),
296306
'duration' => $this->duration(),
307+
'meta' => $this->meta(),
297308
'occurred_at' => $this->occurred_at(),
298309
'created_at' => $this->created_at(),
299310
'updated_at' => $this->updated_at(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Cachet.
5+
*
6+
* (c) Alt Three Services Limited
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Illuminate\Database\Migrations\Migration;
13+
use Illuminate\Database\Schema\Blueprint;
14+
use Illuminate\Support\Facades\Schema;
15+
16+
class CreateMetaTable extends Migration
17+
{
18+
/**
19+
* Run the migrations.
20+
*
21+
* @return void
22+
*/
23+
public function up()
24+
{
25+
Schema::create('meta', function (Blueprint $table) {
26+
$table->increments('id');
27+
$table->string('key')->index();
28+
$table->string('value');
29+
$table->integer('meta_id')->unsigned();
30+
$table->string('meta_type');
31+
$table->timestamps();
32+
33+
$table->index(['meta_id', 'meta_type']);
34+
});
35+
}
36+
37+
/**
38+
* Reverse the migrations.
39+
*
40+
* @return void
41+
*/
42+
public function down()
43+
{
44+
Schema::dropIfExists('meta');
45+
}
46+
}

0 commit comments

Comments
 (0)