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

Add the /status endpoint #1945

Closed
wants to merge 8 commits into from
Closed
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
39 changes: 3 additions & 36 deletions app/Composers/StatusPageComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace CachetHQ\Cachet\Composers;

use CachetHQ\Cachet\Foundation\ViewObjects\SystemStatusViewObject;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident;
use Illuminate\Contracts\View\View;
use CachetHQ\Cachet\Foundation\Queries\SystemStatusQuery;

class StatusPageComposer
{
Expand All @@ -27,41 +29,6 @@ class StatusPageComposer
*/
public function compose(View $view)
{
$totalComponents = Component::enabled()->count();
$majorOutages = Component::enabled()->status(4)->count();
$isMajorOutage = $totalComponents ? ($majorOutages / $totalComponents) >= 0.5 : false;

// Default data
$withData = [
'system_status' => 'info',
'system_message' => trans_choice('cachet.service.bad', $totalComponents),
'favicon' => 'favicon-high-alert',
];

if ($isMajorOutage) {
$withData = [
'system_status' => 'danger',
'system_message' => trans_choice('cachet.service.major', $totalComponents),
'favicon' => 'favicon-high-alert',
];
} elseif (Component::enabled()->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();
$incidentCount = $incidents->count();

if ($incidentCount === 0 || ($incidentCount >= 1 && (int) $incidents->first()->status === 4)) {
$withData = [
'system_status' => 'success',
'system_message' => trans_choice('cachet.service.good', $totalComponents),
'favicon' => 'favicon',
];
}
} else {
if (Component::enabled()->whereIn('status', [2, 3])->count() > 0) {
$withData['favicon'] = 'favicon-medium-alert';
}
}

// Scheduled maintenance code.
$scheduledMaintenance = Incident::scheduled()->orderBy('scheduled_at')->get();

Expand All @@ -70,7 +37,7 @@ public function compose(View $view)
$componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get();
$ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get();

$view->with($withData)
$view->with((new SystemStatusViewObject(new SystemStatusQuery))->toArray())
->withComponentGroups($componentGroups)
->withUngroupedComponents($ungroupedComponents)
->withScheduledMaintenance($scheduledMaintenance);
Expand Down
47 changes: 47 additions & 0 deletions app/Foundation/Policies/SystemStatus/MajorOutagePolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Foundation\Policies\SystemStatus;

class MajorOutagePolicy
{
/**
* @var int
*/
private $numOfMajorOutages;

/**
* @var int
*/
private $numOfTotalEnabledComponents;

/**
* @param int $numOfMajorOutages
* @param int $numOfTotalEnabledComponents
*/
public function __construct($numOfMajorOutages, $numOfTotalEnabledComponents)
{
$this->numOfMajorOutages = $numOfMajorOutages;
$this->numOfTotalEnabledComponents = $numOfTotalEnabledComponents;
}

/**
* @return bool
*/
public function isMajorOutaged()
{
return (
$this->numOfTotalEnabledComponents
? ($this->numOfMajorOutages / $this->numOfTotalEnabledComponents) >= 0.5
: false
);
}
}
62 changes: 62 additions & 0 deletions app/Foundation/Queries/SystemStatusQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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\Foundation\Queries;

use Illuminate\Database\Eloquent;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Foundation\Policies\SystemStatus\MajorOutagePolicy;

class SystemStatusQuery
{
/**
* @return string
*/
public function getSystemStatus()
{
$systemStatus = 'info';

if ($this->isMajorOutaged()) {
$systemStatus = 'danger';
} elseif ($this->isAllTheEnabledComponentsWork() && $this->isAllTheIncidentsFixed()) {
$systemStatus = 'success';
}

return $systemStatus;
}

/**
* @return bool
*/
public function isMajorOutaged()
{
return
(new MajorOutagePolicy(Component::majorOutaged()->count(), Component::enabled()->count()))
->isMajorOutaged();
}

/**
* @return bool
*/
public function isAllTheEnabledComponentsWork()
{
return Component::notOperationaled()->count() === 0;
}

/**
* @return bool
*/
public function isAllTheIncidentsFixed()
{
return Incident::isAllAreFixed();
}
}
60 changes: 60 additions & 0 deletions app/Foundation/ViewObjects/SystemStatusViewObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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\Foundation\ViewObjects;

use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Foundation\Queries\SystemStatusQuery;

class SystemStatusViewObject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View object isn't really right. It's not used in the context of views exclusively?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's not quite right. I'm thinking about how we should name this object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really need an object tbh. Just pass about an array.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can work but how about a transformer object maybe?

{
/**
* @var SystemStatusQuery
*/
private $systemStatusQuery;

/**
* @param SystemStatusQuery $systemStatusQuery
*/
public function __construct(SystemStatusQuery $systemStatusQuery) {
$this->systemStatusQuery = $systemStatusQuery;
}

/**
* @return array
*/
public function toArray()
{
$systemStatus = $this->systemStatusQuery->getSystemStatus();
$numOfEnabledComponents = Component::enabled()->count();
$viewData = [
'system_status' => $systemStatus,
'system_message' => trans_choice('cachet.service.bad', $numOfEnabledComponents),
'favicon' => 'favicon-high-alert',
];

if ($systemStatus === 'danger') {
$viewData['system_message'] = trans_choice('cachet.service.major', $numOfEnabledComponents);
$viewData['favicon'] = 'favicon-high-alert';
} elseif ($systemStatus === 'success') {
$viewData['system_message'] = trans_choice('cachet.service.major', $numOfEnabledComponents);
$viewData['favicon'] = 'favicon';
}

if ( ! $this->systemStatusQuery->isMajorOutaged()
&& ! $this->systemStatusQuery->isAllTheEnabledComponentsWork()
&& Component::enabled()->whereIn('status', [2, 3])->count() > 0) {
$viewData['favicon'] = 'favicon-medium-alert';
}

return $viewData;
}
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/Api/GeneralController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace CachetHQ\Cachet\Http\Controllers\Api;

use CachetHQ\Cachet\Integrations\Releases;
use CachetHQ\Cachet\Foundation\Queries\SystemStatusQuery;
use CachetHQ\Cachet\Foundation\ViewObjects\SystemStatusViewObject;

/**
* This is the general api controller.
Expand Down Expand Up @@ -44,4 +46,19 @@ public function version()
'latest' => $latest,
])->item(CACHET_VERSION);
}

/**
* Endpoint to show System status
*
* @return \Illuminate\Http\JsonResponse
*/
public function status()
{
$systemStatusViewArray = (new SystemStatusViewObject(new SystemStatusQuery))->toArray();

return [
'system_status' => $systemStatusViewArray['system_status'],
'system_message' => $systemStatusViewArray['system_message']
];
}
}
1 change: 1 addition & 0 deletions app/Http/Routes/ApiRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function map(Registrar $router)
$router->group(['middleware' => ['auth.api']], function (Registrar $router) {
$router->get('ping', 'GeneralController@ping');
$router->get('version', 'GeneralController@version');
$router->get('status', 'GeneralController@status');

$router->get('components', 'ComponentController@getComponents');
$router->get('components/groups', 'ComponentGroupController@getGroups');
Expand Down
24 changes: 24 additions & 0 deletions app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@ public function scopeDisabled(Builder $query)
return $query->where('enabled', false);
}

/**
* Finds all components which have major outaged status.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeMajorOutaged(Builder $query)
{
return $query->enabled()->status(4);
}

/**
* Finds all components which are not operational ones.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotOperationaled(Builder $query)
{
return $this->enabled()->notStatus(1);
}

/**
* Returns all of the tags on this component.
*
Expand Down
27 changes: 26 additions & 1 deletion app/Models/Incident.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class Incident extends Model implements HasPresenter
*/
protected $casts = [
'visible' => 'int',
'status' => 'int',
'scheduled_at' => 'date',
'deleted_at' => 'date',
'deleted_at' => 'date'
];

/**
Expand Down Expand Up @@ -128,6 +129,22 @@ public function scopeNotScheduled($query)
});
}

/**
* Do we have any non-fixed incidents?
*
* @return bool
*/
public static function isAllAreFixed()
{
$incidentsOrderedByCreation = static::notScheduled()->orderBy('created_at', 'desc')->get();
$incidentCount = $incidentsOrderedByCreation->count();

return
$incidentCount === 0
||
($incidentCount >= 1 && (int) $incidentsOrderedByCreation->first()->hasFixedStatus());
}

/**
* An incident belongs to a component.
*
Expand All @@ -148,6 +165,14 @@ public function getIsScheduledAttribute()
return $this->getOriginal('scheduled_at') !== null;
}

/**
* @return bool
*/
public function hasFixedStatus()
{
return $this->getAttribute('status') === 4;
}

/**
* Get the presenter class.
*
Expand Down