2014-11-16 22:26:08 +00:00
|
|
|
<?php
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-04-19 08:52:39 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-01-02 00:18:19 +00:00
|
|
|
namespace CachetHQ\Cachet\Http\Controllers;
|
2014-12-01 16:46:56 +00:00
|
|
|
|
2016-02-01 17:14:38 +00:00
|
|
|
use AltThree\Badger\Facades\Badger;
|
2016-02-17 17:07:34 +00:00
|
|
|
use CachetHQ\Cachet\Http\Controllers\Api\AbstractApiController;
|
2016-02-01 17:14:38 +00:00
|
|
|
use CachetHQ\Cachet\Models\Component;
|
2015-01-02 00:18:19 +00:00
|
|
|
use CachetHQ\Cachet\Models\Incident;
|
2016-02-17 17:07:34 +00:00
|
|
|
use CachetHQ\Cachet\Models\Metric;
|
2016-10-30 20:54:12 +00:00
|
|
|
use CachetHQ\Cachet\Models\Schedule;
|
2016-02-17 17:07:34 +00:00
|
|
|
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
|
2017-01-30 13:31:07 +00:00
|
|
|
use CachetHQ\Cachet\Services\Dates\DateFactory;
|
2017-01-02 16:11:15 +00:00
|
|
|
use Carbon\Carbon;
|
2015-01-04 12:33:38 +00:00
|
|
|
use Exception;
|
2015-01-03 00:36:27 -06:00
|
|
|
use GrahamCampbell\Binput\Facades\Binput;
|
2015-08-05 17:18:51 -05:00
|
|
|
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;
|
2016-02-01 17:14:38 +00:00
|
|
|
use Illuminate\Support\Facades\Response;
|
2015-01-01 16:18:24 +00:00
|
|
|
use Illuminate\Support\Facades\View;
|
2016-02-01 17:14:38 +00:00
|
|
|
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
|
|
|
{
|
2015-08-02 15:43:20 +01: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-11-27 16:05:00 +00:00
|
|
|
*/
|
2014-12-20 21:20:17 +00:00
|
|
|
public function showIndex()
|
|
|
|
{
|
2017-01-02 16:11:15 +00:00
|
|
|
$today = Carbon::now();
|
|
|
|
$startDate = Carbon::now();
|
2015-01-14 20:30:23 +00:00
|
|
|
|
2015-01-03 00:36:27 -06:00
|
|
|
// Check if we have another starting date
|
|
|
|
if (Binput::has('start_date')) {
|
|
|
|
try {
|
|
|
|
// If date provided is valid
|
2017-01-02 16:11:15 +00:00
|
|
|
$oldDate = Carbon::createFromFormat('Y-m-d', Binput::get('start_date'));
|
2015-01-23 17:24:34 +00:00
|
|
|
|
2015-01-03 00:36:27 -06:00
|
|
|
// 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', [
|
2015-04-21 12:45:36 +01:00
|
|
|
$startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00',
|
|
|
|
$startDate->format('Y-m-d').' 23:59:59',
|
2016-10-30 20:54:12 +00:00
|
|
|
])->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {
|
|
|
|
return app(DateFactory::class)->make($incident->occurred_at)->toDateString();
|
2015-04-21 12:45:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add in days that have no incidents
|
2016-10-04 19:16:50 +01:00
|
|
|
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
|
|
|
|
2016-10-04 19:16:50 +01:00
|
|
|
if (!isset($allIncidents[$date->toDateString()])) {
|
|
|
|
$allIncidents[$date->toDateString()] = [];
|
|
|
|
}
|
2015-04-21 12:45:36 +01:00
|
|
|
}
|
2015-01-01 21:58:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 12:45:36 +01: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);
|
2015-04-21 12:45:36 +01:00
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
return View::make('index')
|
2015-08-18 21:27:09 +01:00
|
|
|
->withDaysToShow($daysToShow)
|
2015-08-03 22:32:36 +01:00
|
|
|
->withAllIncidents($allIncidents)
|
|
|
|
->withCanPageForward((bool) $today->gt($startDate))
|
2016-10-30 20:54:12 +00:00
|
|
|
->withCanPageBackward(Incident::where('occurred_at', '<', $startDate->format('Y-m-d'))->count() > 0)
|
2015-08-03 22:32:36 +01:00
|
|
|
->withPreviousDate($startDate->copy()->subDays($daysToShow)->toDateString())
|
2015-08-15 11:03:55 +01:00
|
|
|
->withNextDate($startDate->copy()->addDays($daysToShow)->toDateString());
|
2014-11-27 16:05:00 +00:00
|
|
|
}
|
2015-08-15 11:02:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows an incident in more detail.
|
|
|
|
*
|
|
|
|
* @param \CachetHQ\Cachet\Models\Incident $incident
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function showIncident(Incident $incident)
|
|
|
|
{
|
2016-10-30 20:54:12 +00:00
|
|
|
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);
|
2015-08-15 11:02:14 +01:00
|
|
|
}
|
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');
|
2017-01-02 13:35:33 +00:00
|
|
|
$metrics = app(MetricRepository::class);
|
2016-02-17 17:07:34 +00:00
|
|
|
|
|
|
|
switch ($type) {
|
2017-01-02 13:35:33 +00:00
|
|
|
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,
|
|
|
|
]);
|
|
|
|
}
|
2016-02-01 17:14:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2016-02-01 17:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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']);
|
|
|
|
}
|
2014-11-27 22:08:28 +00:00
|
|
|
}
|