|
1
|
|
|
<?php namespace Arcanedev\LogViewer\Http\Controllers; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LogViewer\Contracts\LogViewer as LogViewerContract; |
|
4
|
|
|
use Arcanedev\LogViewer\Exceptions\LogNotFoundException; |
|
5
|
|
|
use Arcanedev\LogViewer\Tables\StatsTable; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
|
8
|
|
|
use Illuminate\Routing\Controller; |
|
9
|
|
|
use Illuminate\Support\Arr; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class LogViewerController |
|
13
|
|
|
* |
|
14
|
|
|
* @package LogViewer\Http\Controllers |
|
15
|
|
|
* @author ARCANEDEV <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class LogViewerController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
20
|
|
|
| Properties |
|
21
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
22
|
|
|
*/ |
|
23
|
|
|
/** |
|
24
|
|
|
* The log viewer instance |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Arcanedev\LogViewer\Contracts\LogViewer |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $logViewer; |
|
29
|
|
|
|
|
30
|
|
|
/** @var int */ |
|
31
|
|
|
protected $perPage = 30; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
protected $showRoute = 'log-viewer::logs.show'; |
|
35
|
|
|
|
|
36
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
37
|
|
|
| Constructor |
|
38
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
39
|
|
|
*/ |
|
40
|
|
|
/** |
|
41
|
|
|
* LogViewerController constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Arcanedev\LogViewer\Contracts\LogViewer $logViewer |
|
44
|
|
|
*/ |
|
45
|
30 |
|
public function __construct(LogViewerContract $logViewer) |
|
46
|
|
|
{ |
|
47
|
30 |
|
$this->logViewer = $logViewer; |
|
48
|
30 |
|
$this->perPage = config('log-viewer.per-page', $this->perPage); |
|
49
|
30 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
52
|
|
|
| Main Functions |
|
53
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
54
|
|
|
*/ |
|
55
|
|
|
/** |
|
56
|
|
|
* Show the dashboard. |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Illuminate\View\View |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function index() |
|
61
|
|
|
{ |
|
62
|
3 |
|
$stats = $this->logViewer->statsTable(); |
|
63
|
3 |
|
$chartData = $this->prepareChartData($stats); |
|
64
|
3 |
|
$percents = $this->calcPercentages($stats->footer(), $stats->header()); |
|
65
|
|
|
|
|
66
|
3 |
|
return $this->view('dashboard', compact('chartData', 'percents')); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* List all logs. |
|
71
|
|
|
* |
|
72
|
|
|
* @param \Illuminate\Http\Request $request |
|
73
|
|
|
* |
|
74
|
|
|
* @return \Illuminate\View\View |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function listLogs(Request $request) |
|
77
|
|
|
{ |
|
78
|
3 |
|
$stats = $this->logViewer->statsTable(); |
|
79
|
3 |
|
$headers = $stats->header(); |
|
80
|
3 |
|
$rows = $this->paginate($stats->rows(), $request); |
|
81
|
|
|
|
|
82
|
3 |
|
return $this->view('logs', compact('headers', 'rows', 'footer')); |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Show the log. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $date |
|
89
|
|
|
* |
|
90
|
|
|
* @return \Illuminate\View\View |
|
91
|
|
|
*/ |
|
92
|
6 |
|
public function show($date) |
|
93
|
|
|
{ |
|
94
|
6 |
|
$log = $this->getLogOrFail($date); |
|
95
|
3 |
|
$levels = $this->logViewer->levelsNames(); |
|
96
|
3 |
|
$entries = $log->entries()->paginate($this->perPage); |
|
97
|
|
|
|
|
98
|
3 |
|
return $this->view('show', compact('log', 'levels', 'entries')); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Filter the log entries by level. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $date |
|
105
|
|
|
* @param string $level |
|
106
|
|
|
* |
|
107
|
|
|
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse |
|
108
|
|
|
*/ |
|
109
|
6 |
|
public function showByLevel($date, $level) |
|
110
|
|
|
{ |
|
111
|
6 |
|
$log = $this->getLogOrFail($date); |
|
112
|
|
|
|
|
113
|
6 |
|
if ($level === 'all') |
|
114
|
4 |
|
return redirect()->route($this->showRoute, [$date]); |
|
115
|
|
|
|
|
116
|
3 |
|
$levels = $this->logViewer->levelsNames(); |
|
117
|
3 |
|
$entries = $this->logViewer |
|
118
|
3 |
|
->entries($date, $level) |
|
119
|
3 |
|
->paginate($this->perPage); |
|
120
|
|
|
|
|
121
|
3 |
|
return $this->view('show', compact('log', 'levels', 'entries')); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Download the log |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $date |
|
128
|
|
|
* |
|
129
|
|
|
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
|
130
|
|
|
*/ |
|
131
|
3 |
|
public function download($date) |
|
132
|
|
|
{ |
|
133
|
3 |
|
return $this->logViewer->download($date); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Delete a log. |
|
138
|
|
|
* |
|
139
|
|
|
* @param \Illuminate\Http\Request $request |
|
140
|
|
|
* |
|
141
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
142
|
|
|
*/ |
|
143
|
9 |
|
public function delete(Request $request) |
|
144
|
|
|
{ |
|
145
|
9 |
|
if ( ! $request->ajax()) |
|
146
|
5 |
|
abort(405, 'Method Not Allowed'); |
|
147
|
|
|
|
|
148
|
6 |
|
$date = $request->get('date'); |
|
149
|
|
|
|
|
150
|
6 |
|
return response()->json([ |
|
151
|
6 |
|
'result' => $this->logViewer->delete($date) ? 'success' : 'error' |
|
152
|
1 |
|
]); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
156
|
|
|
| Other Functions |
|
157
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
158
|
|
|
*/ |
|
159
|
|
|
/** |
|
160
|
|
|
* Get the evaluated view contents for the given view. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $view |
|
163
|
|
|
* @param array $data |
|
164
|
|
|
* @param array $mergeData |
|
165
|
|
|
* |
|
166
|
|
|
* @return \Illuminate\View\View |
|
167
|
|
|
*/ |
|
168
|
12 |
|
protected function view($view, $data = [], $mergeData = []) |
|
169
|
|
|
{ |
|
170
|
12 |
|
return view('log-viewer::'.$view, $data, $mergeData); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Paginate logs. |
|
175
|
|
|
* |
|
176
|
|
|
* @param array $data |
|
177
|
|
|
* @param \Illuminate\Http\Request $request |
|
178
|
|
|
* |
|
179
|
|
|
* @return \Illuminate\Pagination\LengthAwarePaginator |
|
180
|
|
|
*/ |
|
181
|
3 |
|
protected function paginate(array $data, Request $request) |
|
182
|
|
|
{ |
|
183
|
3 |
|
$data = collect($data); |
|
184
|
3 |
|
$page = $request->get('page', 1); |
|
185
|
3 |
|
$url = $request->url(); |
|
186
|
|
|
|
|
187
|
3 |
|
return new LengthAwarePaginator( |
|
188
|
3 |
|
$data->forPage($page, $this->perPage), |
|
189
|
3 |
|
$data->count(), |
|
190
|
3 |
|
$this->perPage, |
|
191
|
1 |
|
$page, |
|
192
|
3 |
|
compact('url') |
|
193
|
1 |
|
); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Get a log or fail |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $date |
|
200
|
|
|
* |
|
201
|
|
|
* @return \Arcanedev\LogViewer\Entities\Log|null |
|
202
|
|
|
*/ |
|
203
|
12 |
|
protected function getLogOrFail($date) |
|
204
|
|
|
{ |
|
205
|
12 |
|
$log = null; |
|
206
|
|
|
|
|
207
|
|
|
try { |
|
208
|
12 |
|
$log = $this->logViewer->get($date); |
|
209
|
|
|
} |
|
210
|
6 |
|
catch (LogNotFoundException $e) { |
|
211
|
3 |
|
abort(404, $e->getMessage()); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
9 |
|
return $log; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Prepare chart data. |
|
219
|
|
|
* |
|
220
|
|
|
* @param \Arcanedev\LogViewer\Tables\StatsTable $stats |
|
221
|
|
|
* |
|
222
|
|
|
* @return string |
|
223
|
|
|
*/ |
|
224
|
3 |
|
protected function prepareChartData(StatsTable $stats) |
|
225
|
|
|
{ |
|
226
|
3 |
|
$totals = $stats->totals()->all(); |
|
227
|
|
|
|
|
228
|
3 |
|
return json_encode([ |
|
229
|
3 |
|
'labels' => Arr::pluck($totals, 'label'), |
|
230
|
|
|
'datasets' => [ |
|
231
|
|
|
[ |
|
232
|
3 |
|
'data' => Arr::pluck($totals, 'value'), |
|
233
|
3 |
|
'backgroundColor' => Arr::pluck($totals, 'color'), |
|
234
|
3 |
|
'hoverBackgroundColor' => Arr::pluck($totals, 'highlight'), |
|
235
|
1 |
|
], |
|
236
|
1 |
|
], |
|
237
|
1 |
|
]); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Calculate the percentage. |
|
242
|
|
|
* |
|
243
|
|
|
* @param array $total |
|
244
|
|
|
* @param array $names |
|
245
|
|
|
* |
|
246
|
|
|
* @return array |
|
247
|
|
|
*/ |
|
248
|
3 |
|
protected function calcPercentages(array $total, array $names) |
|
249
|
|
|
{ |
|
250
|
3 |
|
$percents = []; |
|
251
|
3 |
|
$all = Arr::get($total, 'all'); |
|
252
|
|
|
|
|
253
|
3 |
|
foreach ($total as $level => $count) { |
|
254
|
3 |
|
$percents[$level] = [ |
|
255
|
3 |
|
'name' => $names[$level], |
|
256
|
3 |
|
'count' => $count, |
|
257
|
3 |
|
'percent' => $all ? round(($count / $all) * 100, 2) : 0, |
|
258
|
|
|
]; |
|
259
|
1 |
|
} |
|
260
|
|
|
|
|
261
|
3 |
|
return $percents; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|