Cachet/app/Http/Controllers/StatusPageController.php

180 lines
6.0 KiB
PHP
Raw Normal View History

2014-11-16 22:26:08 +00:00
<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (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\Http\Controllers;
2014-12-01 16:46:56 +00:00
use AltThree\Badger\Facades\Badger;
2016-02-17 17:07:34 +00:00
use CachetHQ\Cachet\Http\Controllers\Api\AbstractApiController;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
2016-02-17 17:07:34 +00:00
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\Schedule;
2016-02-17 17:07:34 +00:00
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
use CachetHQ\Cachet\Services\Dates\DateFactory;
use Carbon\Carbon;
2015-01-04 12:33:38 +00:00
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Routing\Controller;
2015-05-20 08:41:02 +01:00
use Illuminate\Support\Facades\Auth;
2016-01-29 22:49:06 +00:00
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Response;
2015-01-01 16:18:24 +00:00
use Illuminate\Support\Facades\View;
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;
2014-12-01 16:46:56 +00:00
2017-01-02 13:34:10 +00:00
/**
* This is the status page controller class.
*
* @author James Brooks <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
* @author Joseph Cohen <joe@alt-three.com>
*/
2016-02-17 17:07:34 +00:00
class StatusPageController extends AbstractApiController
2015-01-01 15:45:04 +00:00
{
/**
* Displays the status page.
2014-12-29 23:07:46 +00:00
*
2014-12-01 08:38:26 +00:00
* @return \Illuminate\View\View
*/
2014-12-20 21:20:17 +00:00
public function showIndex()
{
$today = Carbon::now();
$startDate = Carbon::now();
// Check if we have another starting date
if (Binput::has('start_date')) {
try {
// If date provided is valid
$oldDate = Carbon::createFromFormat('Y-m-d', Binput::get('start_date'));
// If trying to get a future date fallback to today
if ($today->gt($oldDate)) {
$startDate = $oldDate;
}
} catch (Exception $e) {
// Fallback to today
}
}
2016-12-30 19:21:45 +00:00
$daysToShow = max(0, (int) Config::get('setting.app_incident_days', 0) - 1);
$incidentDays = range(0, $daysToShow);
2015-05-20 08:41:02 +01:00
2016-12-30 19:21:45 +00:00
$allIncidents = Incident::where('visible', '>=', (int) !Auth::check())->whereBetween('occurred_at', [
$startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00',
$startDate->format('Y-m-d').' 23:59:59',
])->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {
return app(DateFactory::class)->make($incident->occurred_at)->toDateString();
});
// Add in days that have no incidents
if (Config::get('setting.only_disrupted_days') === false) {
foreach ($incidentDays as $i) {
$date = app(DateFactory::class)->make($startDate)->subDays($i);
2015-01-27 13:05:54 -06:00
if (!isset($allIncidents[$date->toDateString()])) {
$allIncidents[$date->toDateString()] = [];
}
}
2015-01-01 21:58:27 +00:00
}
// Sort the array so it takes into account the added days
2015-05-29 20:43:12 +01:00
$allIncidents = $allIncidents->sortBy(function ($value, $key) {
2015-05-02 09:09:02 +01:00
return strtotime($key);
2016-12-30 19:21:45 +00:00
}, SORT_REGULAR, true);
return View::make('index')
->withDaysToShow($daysToShow)
->withAllIncidents($allIncidents)
->withCanPageForward((bool) $today->gt($startDate))
->withCanPageBackward(Incident::where('occurred_at', '<', $startDate->format('Y-m-d'))->count() > 0)
->withPreviousDate($startDate->copy()->subDays($daysToShow)->toDateString())
->withNextDate($startDate->copy()->addDays($daysToShow)->toDateString());
}
/**
* Shows an incident in more detail.
*
* @param \CachetHQ\Cachet\Models\Incident $incident
*
* @return \Illuminate\View\View
*/
public function showIncident(Incident $incident)
{
return View::make('single-incident')->withIncident($incident);
}
/**
* Show a single schedule.
*
* @param \CachetHQ\Cachet\Models\Schedule $schedule
*
* @return \Illuminate\View\View
*/
public function showSchedule(Schedule $schedule)
{
return View::make('single-schedule')->withSchedule($schedule);
}
2016-02-17 17:07:34 +00:00
/**
* Returns metrics in a readily formatted way.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
*
* @return \Illuminate\Http\JsonResponse
*/
public function getMetrics(Metric $metric)
{
$type = Binput::get('filter', 'last_hour');
$metrics = app(MetricRepository::class);
2016-02-17 17:07:34 +00:00
switch ($type) {
case 'last_hour': $metricData = $metrics->listPointsLastHour($metric); break;
case 'today': $metricData = $metrics->listPointsToday($metric); break;
case 'week': $metricData = $metrics->listPointsForWeek($metric); break;
case 'month': $metricData = $metrics->listPointsForMonth($metric); break;
default: $metricData = [];
2016-02-17 17:07:34 +00:00
}
return $this->item([
'metric' => $metric->toArray(),
'items' => $metricData,
]);
}
/**
* Generates a Shield (badge) for the component.
*
* @param \CachetHQ\Cachet\Models\Component $component
*
* @return \Illuminate\Http\Response
*/
public function showComponentBadge(Component $component)
{
$component = AutoPresenter::decorate($component);
switch ($component->status_color) {
2017-01-02 13:36:49 +00:00
case 'reds': $color = Config::get('setting.style_reds', '#FF6F6F'); break;
case 'blues': $color = Config::get('setting.style_blues', '#3498DB'); break;
case 'greens': $color = Config::get('setting.style_greens', '#7ED321'); break;
case 'yellows': $color = Config::get('setting.style_yellows', '#F7CA18'); break;
default: $color = null;
}
$badge = Badger::generate(
$component->name,
$component->human_status,
substr($color, 1),
Binput::get('style', 'flat-square')
);
return Response::make($badge, 200, ['Content-Type' => 'image/svg+xml']);
}
}