-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the system status api endpoint. Closes #1936
- Loading branch information
Showing
8 changed files
with
215 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\Providers; | ||
|
||
use CachetHQ\Cachet\Integrations\Contracts\System as SystemContract; | ||
use CachetHQ\Cachet\Integrations\Core\System; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
/** | ||
* This is the integration service provider. | ||
* | ||
* @author James Brooks <james@alt-three.com> | ||
*/ | ||
class IntegrationServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->registerSystem(); | ||
} | ||
|
||
/** | ||
* Register the system class. | ||
* | ||
* @return void | ||
*/ | ||
protected function registerSystem() | ||
{ | ||
$this->app->singleton(SystemContract::class, function (Container $app) { | ||
return new System(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?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\Integrations\Contracts; | ||
|
||
/** | ||
* This is the system interface. | ||
* | ||
* @author James Brooks <james@alt-three.com> | ||
*/ | ||
interface System | ||
{ | ||
/** | ||
* Get the entire system status. | ||
* | ||
* @return array | ||
*/ | ||
public function getStatus(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?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\Integrations\Core; | ||
|
||
use CachetHQ\Cachet\Integrations\Contracts\System as SystemContract; | ||
use CachetHQ\Cachet\Models\Component; | ||
use CachetHQ\Cachet\Models\Incident; | ||
|
||
/** | ||
* This is the core system class. | ||
* | ||
* @author James Brooks <james@alt-three.com> | ||
*/ | ||
class System implements SystemContract | ||
{ | ||
/** | ||
* Get the entire system status. | ||
* | ||
* @return array | ||
*/ | ||
public function getStatus() | ||
{ | ||
$enabledScope = Component::enabled(); | ||
$totalComponents = Component::enabled()->count(); | ||
$majorOutages = Component::enabled()->status(4)->count(); | ||
$isMajorOutage = $totalComponents ? ($majorOutages / $totalComponents) >= 0.5 : false; | ||
|
||
// Default data | ||
$status = [ | ||
'system_status' => 'info', | ||
'system_message' => trans_choice('cachet.service.bad', $totalComponents), | ||
'favicon' => 'favicon-high-alert', | ||
]; | ||
|
||
if ($isMajorOutage) { | ||
$status = [ | ||
'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)) { | ||
$status = [ | ||
'system_status' => 'success', | ||
'system_message' => trans_choice('cachet.service.good', $totalComponents), | ||
'favicon' => 'favicon', | ||
]; | ||
} | ||
} elseif (Component::enabled()->whereIn('status', [2, 3])->count() > 0) { | ||
$status['favicon'] = 'favicon-medium-alert'; | ||
} | ||
|
||
return $status; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
tests/Foundation/Providers/IntegrationServiceProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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\Tests\Cachet\Foundation\Providers; | ||
|
||
use AltThree\TestBench\ServiceProviderTrait; | ||
use CachetHQ\Cachet\Integrations\Contracts\System; | ||
use CachetHQ\Tests\Cachet\AbstractTestCase; | ||
|
||
/** | ||
* This is the integration service provider test class. | ||
* | ||
* @author James Brooks <james@alt-three.com> | ||
*/ | ||
class IntegrationServiceProviderTest extends AbstractTestCase | ||
{ | ||
use ServiceProviderTrait; | ||
|
||
public function testSystemIsInjectable() | ||
{ | ||
$this->assertIsInjectable(System::class); | ||
} | ||
} |