*/ class DashboardController extends Controller { /** * Start date. * * @var \Jenssegers\Date\Date */ protected $startDate; /** * The timezone the status page is running in. * * @var string */ protected $timeZone; /** * The feed integration. * * @var \CachetHQ\Cachet\Integrations\Contracts\Feed */ protected $feed; /** * The user session object. * * @var \Illuminate\Contracts\Auth\Guard */ protected $guard; /** * Creates a new dashboard controller instance. * * @param \CachetHQ\Cachet\Integrations\Contracts\Feed $feed * @param \Illuminate\Contracts\Auth\Guard $guard * * @return void */ public function __construct(Feed $feed, Guard $guard) { $this->feed = $feed; $this->guard = $guard; $this->startDate = new Date(); $this->dateTimeZone = Config::get('cachet.timezone'); } /** * Redirect /admin to /dashboard. * * @return \Illuminate\Http\RedirectResponse */ public function redirectAdmin() { return cachet_redirect('dashboard'); } /** * Shows the dashboard view. * * @return \Illuminate\View\View */ public function showDashboard() { $components = Component::orderBy('order')->get(); $incidents = $this->getIncidents(); $subscribers = $this->getSubscribers(); $componentGroups = $this->getVisibleGroupedComponents(); $ungroupedComponents = Component::ungrouped()->get(); $welcomeUser = !Auth::user()->welcomed; if ($welcomeUser) { execute(new WelcomeUserCommand(Auth::user())); } $entries = null; if ($feed = $this->feed->latest() !== Feed::FAILED) { if (is_object($feed)) { $entries = array_slice($feed->channel->item, 0, 5); } } return View::make('dashboard.index') ->withPageTitle(trans('dashboard.dashboard')) ->withComponents($components) ->withIncidents($incidents) ->withSubscribers($subscribers) ->withEntries($entries) ->withComponentGroups($componentGroups) ->withUngroupedComponents($ungroupedComponents) ->withWelcomeUser($welcomeUser); } /** * Fetches all of the incidents over the last 30 days. * * @return \Illuminate\Support\Collection */ protected function getIncidents() { $allIncidents = Incident::whereBetween('occurred_at', [ $this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00', $this->startDate->format('Y-m-d').' 23:59:59', ])->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) { return (new Date($incident->occurred_at)) ->setTimezone($this->dateTimeZone)->toDateString(); }); // Add in days that have no incidents foreach (range(0, 30) as $i) { $date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i); if (!isset($allIncidents[$date->toDateString()])) { $allIncidents[$date->toDateString()] = []; } } // Sort the array so it takes into account the added days $allIncidents = $allIncidents->sortBy(function ($value, $key) { return strtotime($key); }, SORT_REGULAR, false); return $allIncidents; } /** * Fetches all of the subscribers over the last 30 days. * * @return \Illuminate\Support\Collection */ protected function getSubscribers() { $allSubscribers = Subscriber::whereBetween('created_at', [ $this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00', $this->startDate->format('Y-m-d').' 23:59:59', ])->orderBy('created_at', 'desc')->get()->groupBy(function (Subscriber $incident) { return (new Date($incident->created_at)) ->setTimezone($this->dateTimeZone)->toDateString(); }); // Add in days that have no incidents foreach (range(0, 30) as $i) { $date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i); if (!isset($allSubscribers[$date->toDateString()])) { $allSubscribers[$date->toDateString()] = []; } } // Sort the array so it takes into account the added days $allSubscribers = $allSubscribers->sortBy(function ($value, $key) { return strtotime($key); }, SORT_REGULAR, false); return $allSubscribers; } /** * Get visible grouped components. * * @return \Illuminate\Support\Collection */ protected function getVisibleGroupedComponents() { $componentGroupsBuilder = ComponentGroup::query(); if (!$this->guard->check()) { $componentGroupsBuilder = ComponentGroup::visible(); } $usedComponentGroups = Component::grouped()->pluck('group_id'); return $componentGroupsBuilder->used($usedComponentGroups) ->get(); } }