-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
DashboardController.php
204 lines (177 loc) · 5.91 KB
/
DashboardController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
* This file is part of Cachet.
*
* (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\Dashboard;
use CachetHQ\Cachet\Bus\Commands\User\WelcomeUserCommand;
use CachetHQ\Cachet\Integrations\Contracts\Feed;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Jenssegers\Date\Date;
/**
* This is the dashboard controller class.
*
* @author James Brooks <[email protected]>
*/
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()) {
$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();
}
}