72 lines
1.7 KiB
PHP
72 lines
1.7 KiB
PHP
<?php
|
|
/**
|
|
* Holds the status controller.
|
|
* @package Sakura
|
|
*/
|
|
|
|
namespace Sakura\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use Sakura\DB;
|
|
use Sakura\Status;
|
|
|
|
/**
|
|
* The status page and related stuff.
|
|
* @package Sakura
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
*/
|
|
class StatusController extends Controller
|
|
{
|
|
protected const EVENT_STATE = [
|
|
0 => 'Monitoring',
|
|
1 => 'Investigating',
|
|
2 => 'Resolved',
|
|
];
|
|
|
|
/**
|
|
* Renders the base status page.
|
|
* @return string
|
|
*/
|
|
public function index(): string
|
|
{
|
|
$endpoints = array_keys(config('status.check'));
|
|
$events = DB::table('status_events')
|
|
->get();
|
|
$incidents = [];
|
|
|
|
foreach ($events as $row) {
|
|
$date = Carbon::createFromFormat('Y-m-d H:i:s', $row->event_date);
|
|
$incidents[$date->format('M j, Y')][] = [
|
|
'time' => $date->format('G:i'),
|
|
'state' => static::EVENT_STATE[$row->event_state],
|
|
'comment' => $row->event_text,
|
|
];
|
|
}
|
|
|
|
return view('status/index', compact('endpoints', 'incidents'));
|
|
}
|
|
|
|
/**
|
|
* Gets status information.
|
|
* @return string
|
|
*/
|
|
public function data(): string
|
|
{
|
|
$endpoints = config('status.check');
|
|
$name = $_GET['name'] ?? null;
|
|
|
|
if (!array_key_exists($name, $endpoints)) {
|
|
return $this->json(['error' => 'Unknown host.']);
|
|
}
|
|
|
|
[$address, $port, $protocol] = explode('/', $endpoints[$name]);
|
|
$status = new Status($address, $port, $protocol);
|
|
|
|
if ($status->state === null) {
|
|
$status->check();
|
|
$status->save();
|
|
}
|
|
|
|
return $this->json($status);
|
|
}
|
|
}
|