Skip to content

Commit

Permalink
Add incident column for when an incident occurred at (#2212)
Browse files Browse the repository at this point in the history
Add incident column for when an incident occurred at. Closes #2208
[ci skip] [skip ci]
  • Loading branch information
jbrooksuk committed Oct 29, 2016
1 parent 0e0a7d9 commit a0f2d66
Show file tree
Hide file tree
Showing 26 changed files with 326 additions and 74 deletions.
12 changes: 6 additions & 6 deletions app/Bus/Commands/Incident/ReportIncidentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ final class ReportIncidentCommand
public $stickied;

/**
* The date at which the incident occurred.
* The date at which the incident occurred at.
*
* @var string|null
*/
public $incident_date;
public $occurred_at;

/**
* A given incident template.
Expand Down Expand Up @@ -110,7 +110,7 @@ final class ReportIncidentCommand
'component_status' => 'nullable|required_with:component_id|int|min:0|max:4',
'notify' => 'nullable|bool',
'stickied' => 'required|bool',
'incident_date' => 'nullable|string',
'occurred_at' => 'nullable|string',
'template' => 'nullable|string',
];

Expand All @@ -125,13 +125,13 @@ final class ReportIncidentCommand
* @param int $component_status
* @param bool $notify
* @param bool $stickied
* @param string|null $incident_date
* @param string|null $occurred_at
* @param string|null $template
* @param array $template_vars
*
* @return void
*/
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $incident_date, $template, array $template_vars = [])
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [])
{
$this->name = $name;
$this->status = $status;
Expand All @@ -141,7 +141,7 @@ public function __construct($name, $status, $message, $visible, $component_id, $
$this->component_status = $component_status;
$this->notify = $notify;
$this->stickied = $stickied;
$this->incident_date = $incident_date;
$this->occurred_at = $occurred_at;
$this->template = $template;
$this->template_vars = $template_vars;
}
Expand Down
20 changes: 14 additions & 6 deletions app/Bus/Commands/Incident/UpdateIncidentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

use CachetHQ\Cachet\Models\Incident;

/**
* This is the update incident command.
*
* @author James Brooks <james@alt-three.com>
* @author Joseph Cohem <joe@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
*/
final class UpdateIncidentCommand
{
/**
Expand Down Expand Up @@ -79,11 +86,11 @@ final class UpdateIncidentCommand
public $stickied;

/**
* The date that the incident occurred on.
* The timestamp that the incident occurred at.
*
* @var string
* @var string|null
*/
public $incident_date;
public $occurred_at;

/**
* A given incident template.
Expand Down Expand Up @@ -113,6 +120,7 @@ final class UpdateIncidentCommand
'component_status' => 'nullable|int|min:0|max:4|required_with:component_id',
'notify' => 'nullable|bool',
'stickied' => 'nullable|bool',
'occurred_at' => 'nullable|string',
'template' => 'nullable|string',
];

Expand All @@ -128,13 +136,13 @@ final class UpdateIncidentCommand
* @param int $component_status
* @param bool $notify
* @param bool $stickied
* @param string|null $incident_date
* @param string|null $occurred_at
* @param string|null $template
* @param array $template_vars
*
* @return void
*/
public function __construct(Incident $incident, $name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $incident_date, $template, array $template_vars = [])
public function __construct(Incident $incident, $name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [])
{
$this->incident = $incident;
$this->name = $name;
Expand All @@ -145,7 +153,7 @@ public function __construct(Incident $incident, $name, $status, $message, $visib
$this->component_status = $component_status;
$this->notify = $notify;
$this->stickied = $stickied;
$this->incident_date = $incident_date;
$this->occurred_at = $occurred_at;
$this->template = $template;
$this->template_vars = $template_vars;
}
Expand Down
25 changes: 25 additions & 0 deletions app/Bus/Exceptions/Incident/InvalidIncidentTimestampException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Bus\Exceptions\Incident;

use CachetHQ\Cachet\Bus\Exceptions\ExceptionInterface;
use Exception;

/**
* This is the invalid incident timestamp exception.
*
* @author James Brooks <james@alt-three.com>
*/
class InvalidIncidentTimestampException extends Exception implements ExceptionInterface
{
//
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Incident\ReportIncidentCommand;
use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasReportedEvent;
use CachetHQ\Cachet\Bus\Exceptions\Incident\InvalidIncidentTimestampException;
use CachetHQ\Cachet\Dates\DateFactory;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
Expand Down Expand Up @@ -75,11 +76,12 @@ public function handle(ReportIncidentCommand $command)
}

// The incident occurred at a different time.
if ($command->incident_date) {
$incidentDate = $this->dates->create('d/m/Y H:i', $command->incident_date);

$data['created_at'] = $incidentDate;
$data['updated_at'] = $incidentDate;
if ($occurredAt = $command->occurredAt) {
if ($date = $this->dates->create('Y-m-d H:i', $occurredAt)) {
$incident->fill(['occurred_at' => $date]);
} else {
throw new InvalidIncidentTimestampException("Unable to pass timestamp {$occurredAt}");
}
}

// Create the incident
Expand Down Expand Up @@ -127,7 +129,7 @@ protected function parseTemplate(IncidentTemplate $template, ReportIncidentComma
'visible' => $command->visible,
'notify' => $command->notify,
'stickied' => $command->stickied,
'incident_date' => $command->incident_date,
'occurredAt' => $command->occurredAt,
'component' => Component::find($command->component_id) ?: null,
'component_status' => $command->component_status,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Incident\UpdateIncidentCommand;
use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasUpdatedEvent;
use CachetHQ\Cachet\Bus\Exceptions\Incident\InvalidIncidentTimestampException;
use CachetHQ\Cachet\Dates\DateFactory;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
Expand Down Expand Up @@ -61,18 +62,20 @@ public function handle(UpdateIncidentCommand $command)
}

$incident = $command->incident;
$incident->update($this->filter($command));
$incident->fill($this->filter($command));

// The incident occurred at a different time.
if ($command->incident_date) {
$incidentDate = $this->dates->create('d/m/Y H:i', $command->incident_date);

$incident->update([
'created_at' => $incidentDate,
'updated_at' => $incidentDate,
]);
if ($occurredAt = $command->occurredAt) {
if ($date = $this->dates->create('Y-m-d H:i', $occurredAt)) {
$incident->fill(['occurred_at' => $date]);
} else {
throw new InvalidIncidentTimestampException("Unable to pass timestamp {$occurredAt}");
}
}

// Rather than making lots of updates, just fill and save.
$incident->save();

// Update the component.
if ($component = Component::find($command->component_id)) {
dispatch(new UpdateComponentCommand(
Expand Down Expand Up @@ -138,7 +141,7 @@ protected function parseTemplate(IncidentTemplate $template, UpdateIncidentComma
'visible' => $command->visible,
'notify' => $command->notify,
'stickied' => $command->stickied,
'incident_date' => $command->incident_date,
'occurredAt' => $command->occurredAt,
'component' => Component::find($command->component_id) ?: null,
'component_status' => $command->component_status,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function notify(IncidentWasReportedEvent $event, $subscriber)
'has_component' => ($event->incident->component) ? true : false,
'component_name' => $component ? $component->name : null,
'name' => $incident->name,
'timestamp' => $incident->created_at_formatted,
'timestamp' => $incident->occurred_at_formatted,
'status' => $incident->human_status,
'html_content' => $incident->formattedMessage,
'text_content' => $incident->message,
Expand Down
4 changes: 2 additions & 2 deletions app/Composers/Modules/StickiedComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class StickiedComposer
*/
public function compose(View $view)
{
$stickiedIncidents = Incident::stickied()->orderBy('scheduled_at', 'desc')->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) {
return app(DateFactory::class)->make($incident->is_scheduled ? $incident->scheduled_at : $incident->created_at)->toDateString();
$stickiedIncidents = Incident::stickied()->orderBy('scheduled_at', 'desc')->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {
return app(DateFactory::class)->make($incident->is_scheduled ? $incident->scheduled_at : $incident->occurred_at)->toDateString();
});
$view->withStickiedIncidents($stickiedIncidents);
}
Expand Down
3 changes: 3 additions & 0 deletions app/Console/Commands/DemoSeederCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CachetHQ\Cachet\Models\Subscriber;
use CachetHQ\Cachet\Models\User;
use CachetHQ\Cachet\Settings\Repository;
use Carbon\Carbon;
use DateInterval;
use DateTime;
use Illuminate\Console\Command;
Expand Down Expand Up @@ -209,6 +210,7 @@ protected function seedIncidents()
'scheduled_at' => null,
'visible' => 1,
'stickied' => false,
'occurred_at' => Carbon::now(),
],
[
'name' => 'This is an unresolved incident',
Expand All @@ -218,6 +220,7 @@ protected function seedIncidents()
'scheduled_at' => null,
'visible' => 1,
'stickied' => false,
'occurred_at' => Carbon::now(),
],
];

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function postIncidents()
Binput::get('component_status'),
Binput::get('notify', true),
Binput::get('stickied', false),
Binput::get('created_at'),
Binput::get('occurred_at'),
Binput::get('template'),
Binput::get('vars', [])
));
Expand Down Expand Up @@ -107,7 +107,7 @@ public function putIncident(Incident $incident)
Binput::get('component_status'),
Binput::get('notify', true),
Binput::get('stickied', false),
Binput::get('created_at'),
Binput::get('occurred_at'),
Binput::get('template'),
Binput::get('vars', [])
));
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Dashboard/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ public function showDashboard()
*/
protected function getIncidents()
{
$allIncidents = Incident::notScheduled()->whereBetween('created_at', [
$allIncidents = Incident::notScheduled()->whereBetween('occurred_at', [
$this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00',
$this->startDate->format('Y-m-d').' 23:59:59',
])->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) {
return (new Date($incident->created_at))
])->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {
return (new Date($incident->occurred_at))
->setTimezone($this->dateTimeZone)->toDateString();
});

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Dashboard/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function createIncidentAction()
Binput::get('component_status'),
Binput::get('notify', false),
Binput::get('stickied', false),
Binput::get('created_at'),
Binput::get('occurred_at'),
null,
[]
));
Expand Down Expand Up @@ -259,7 +259,7 @@ public function editIncidentAction(Incident $incident)
Binput::get('component_status'),
Binput::get('notify', true),
Binput::get('stickied', false),
Binput::get('created_at'),
Binput::get('occurred_at'),
null,
[]
));
Expand Down
11 changes: 8 additions & 3 deletions app/Http/Controllers/FeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;

/**
* This is the feed controller.
*
* @author James Brooks <james@alt-three.com>
*/
class FeedController extends Controller
{
/**
Expand Down Expand Up @@ -80,12 +85,12 @@ private function feedAction(ComponentGroup &$group, $isRss)
{
if ($group->exists) {
$group->components->map(function ($component) use ($isRss) {
$component->incidents()->visible()->orderBy('created_at', 'desc')->get()->map(function ($incident) use ($isRss) {
$component->incidents()->visible()->orderBy('occurred_at', 'desc')->get()->map(function ($incident) use ($isRss) {
$this->feedAddItem($incident, $isRss);
});
});
} else {
Incident::visible()->orderBy('created_at', 'desc')->get()->map(function ($incident) use ($isRss) {
Incident::visible()->orderBy('occurred_at', 'desc')->get()->map(function ($incident) use ($isRss) {
$this->feedAddItem($incident, $isRss);
});
}
Expand All @@ -105,7 +110,7 @@ private function feedAddItem(Incident $incident, $isRss)
$incident->name,
Config::get('setting.app_name'),
Str::canonicalize(cachet_route('incident', [$incident->id])),
$isRss ? $incident->created_at->toRssString() : $incident->created_at->toAtomString(),
$isRss ? $incident->occurred_at->toRssString() : $incident->occurred_at->toAtomString(),
$isRss ? $incident->message : Markdown::convertToHtml($incident->message)
);
}
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/StatusPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ public function showIndex()

$incidentVisibility = Auth::check() ? 0 : 1;

$allIncidents = Incident::notScheduled()->where('visible', '>=', $incidentVisibility)->whereBetween('created_at', [
$allIncidents = Incident::notScheduled()->where('visible', '>=', $incidentVisibility)->whereBetween('occurred_at', [
$startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00',
$startDate->format('Y-m-d').' 23:59:59',
])->orderBy('scheduled_at', 'desc')->orderBy('created_at', 'desc')->get()->load('updates')->groupBy(function (Incident $incident) {
return app(DateFactory::class)->make($incident->is_scheduled ? $incident->scheduled_at : $incident->created_at)->toDateString();
])->orderBy('scheduled_at', 'desc')->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {
return app(DateFactory::class)->make($incident->is_scheduled ? $incident->scheduled_at : $incident->occurred_at)->toDateString();
});

// Add in days that have no incidents
Expand All @@ -109,7 +109,7 @@ public function showIndex()
->withDaysToShow($daysToShow)
->withAllIncidents($allIncidents)
->withCanPageForward((bool) $today->gt($startDate))
->withCanPageBackward(Incident::notScheduled()->where('created_at', '<', $startDate->format('Y-m-d'))->count() > 0)
->withCanPageBackward(Incident::notScheduled()->where('occurred_at', '<', $startDate->format('Y-m-d'))->count() > 0)
->withPreviousDate($startDate->copy()->subDays($daysToShow)->toDateString())
->withNextDate($startDate->copy()->addDays($daysToShow)->toDateString());
}
Expand Down
2 changes: 1 addition & 1 deletion app/Integrations/Core/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getStatus()
];
} elseif ($enabledScope->notStatus(1)->count() === 0) {
// If all our components are ok, do we have any non-fixed incidents?
$incidents = Incident::notScheduled()->orderBy('created_at', 'desc')->get()->filter(function ($incident) {
$incidents = Incident::notScheduled()->orderBy('occurred_at', 'desc')->get()->filter(function ($incident) {
return $incident->status > 0;
});
$incidentCount = $incidents->count();
Expand Down
Loading

0 comments on commit a0f2d66

Please sign in to comment.