Skip to content
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
66 changes: 65 additions & 1 deletion Healthchecks/Healthchecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,70 @@

namespace App\SupportedApps\Healthchecks;

class Healthchecks extends \App\SupportedApps
class Healthchecks extends \App\SupportedApps implements \App\EnhancedApps
{
public $config;

public function __construct()
{
}

public function test()
{
$test = parent::appTest($this->url('api/v3/checks/'), $this->getAttrs());
echo $test->status;
}

public function livestats()
{
$status = 'inactive';
$data = [
'up' => 0,
'down' => 0,
];

// Single authenticated GET. execute() returns null on a failed
// connection (it never throws), so guard before reading the body.
$res = parent::execute($this->url('api/v3/checks/'), $this->getAttrs());
if ($res !== null) {
// Management API returns { "checks": [ ... ] }; each check has a
// status of new, up, grace, down, or paused. Errors (a bad API
// key returns 401 with { "error": ... }) carry no "checks" key,
// so coalesce to null rather than reading the property directly
// and leave the tile inactive in that case.
$body = json_decode($res->getBody());
$checks = $body->checks ?? null;
if (is_array($checks)) {
$status = 'active';
foreach ($checks as $check) {
if (!isset($check->status)) {
continue;
}
if ($check->status === 'up') {
$data['up']++;
} elseif ($check->status === 'down') {
$data['down']++;
}
}
}
}

return parent::getLiveStats($status, $data);
}

private function getAttrs()
{
return [
'headers' => [
'Accept' => 'application/json',
'X-Api-Key' => ($this->config->apikey ?? ''),
],
];
}

public function url($endpoint)
{
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
return $api_url;
}
}
2 changes: 1 addition & 1 deletion Healthchecks/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"website": "https://healthchecks.io",
"license": "BSD 3-Clause \"New\" or \"Revised\" License",
"description": "Instant alerts when your cron jobs fail silently.",
"enhanced": false,
"enhanced": true,
"tile_background": "light",
"icon": "healthchecks.png"
}
16 changes: 16 additions & 0 deletions Healthchecks/config.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
<div class="items">
<div class="input">
<label>{{ strtoupper(__('app.url')) }}</label>
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
</div>
<div class="input">
<label>{{ __('app.apps.apikey') }}</label>
{!! Form::text('config[apikey]', isset($item) ? ($item->getconfig()->apikey ?? null) : null, ['placeholder' => __('app.apps.apikey'), 'data-config' => 'apikey', 'class' => 'form-control config-item']) !!}
</div>
<div class="input">
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
{{-- Up/down counts change moderately; refresh on the 30s "data only" cadence, not every 5s. --}}
{!! Form::hidden('config[dataonly]', '1') !!}
</div>
10 changes: 10 additions & 0 deletions Healthchecks/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ul class="livestats">
<li>
<span class="title">Up</span>
<strong>{!! $up ?? 0 !!}</strong>
</li>
<li>
<span class="title">Down</span>
<strong>{!! $down ?? 0 !!}</strong>
</li>
</ul>