This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Controllers/StatusController.php

79 lines
2 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Holds the status controller.
* @package Sakura
*/
namespace Sakura\Controllers;
2016-12-09 22:28:25 +00:00
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
{
2016-12-09 22:28:25 +00:00
protected const EVENT_STATE = [
0 => 'Monitoring',
1 => 'Investigating',
2 => 'Resolved',
];
2016-08-05 02:35:37 +00:00
/**
* Renders the base status page.
* @return string
*/
2016-12-10 19:43:17 +00:00
public function index(): string
2016-12-09 22:28:25 +00:00
{
$endpoints = array_keys(config('status.check'));
$events = DB::table('status_events')
2016-12-10 22:36:24 +00:00
->where('event_date', '>', Carbon::now()->subWeek(1)->format('Y-m-d'))
2016-12-09 22:28:25 +00:00
->get();
$incidents = [];
2016-12-10 22:36:24 +00:00
for ($i = 0; $i < 7; $i++) {
$date = Carbon::now()->subDay($i);
$incidents[$date->format('M j, Y')] = [];
}
2016-12-09 22:28:25 +00:00
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
*/
2016-12-10 19:43:17 +00:00
public function data(): string
{
2016-12-09 22:28:25 +00:00
$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]);
2016-12-10 22:36:24 +00:00
$status = new Status($address, $port, $protocol, true, ($_GET['history'] ?? '1') !== '0');
2016-12-09 22:28:25 +00:00
if ($status->state === null) {
$status->check();
$status->save();
}
return $this->json($status);
}
}