1
|
|
|
<?php namespace Arcanesoft\Foundation\Http\Controllers\Admin\System; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelApiHelper\Traits\JsonResponses; |
4
|
|
|
use Arcanedev\LogViewer\Contracts\LogViewer as LogViewerContract; |
5
|
|
|
use Arcanedev\LogViewer\Entities\Log; |
6
|
|
|
use Arcanedev\LogViewer\Entities\LogEntry; |
7
|
|
|
use Arcanedev\LogViewer\Exceptions\LogNotFoundException; |
8
|
|
|
use Arcanesoft\Foundation\Policies\LogViewerPolicy; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
11
|
|
|
use Illuminate\Support\Arr; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class LogViewerController |
16
|
|
|
* |
17
|
|
|
* @package Arcanesoft\Foundation\Http\Controllers\Admin\System |
18
|
|
|
* @author ARCANEDEV <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class LogViewerController extends Controller |
21
|
|
|
{ |
22
|
|
|
/* ----------------------------------------------------------------- |
23
|
|
|
| Trait |
24
|
|
|
| ----------------------------------------------------------------- |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
use JsonResponses; |
28
|
|
|
|
29
|
|
|
/* ----------------------------------------------------------------- |
30
|
|
|
| Properties |
31
|
|
|
| ----------------------------------------------------------------- |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The LogViewer instance. |
36
|
|
|
* |
37
|
|
|
* @var \Arcanedev\LogViewer\Contracts\LogViewer |
38
|
|
|
*/ |
39
|
|
|
protected $logViewer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Logs per page. |
43
|
|
|
* |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
protected $perPage = 30; |
47
|
|
|
|
48
|
|
|
/* ----------------------------------------------------------------- |
49
|
|
|
| Constructor |
50
|
|
|
| ----------------------------------------------------------------- |
51
|
|
|
*/ |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* LogViewerController constructor. |
55
|
|
|
* |
56
|
|
|
* @param \Arcanedev\LogViewer\Contracts\LogViewer $logViewer |
57
|
|
|
*/ |
58
|
|
|
public function __construct(LogViewerContract $logViewer) |
59
|
|
|
{ |
60
|
|
|
parent::__construct(); |
61
|
|
|
|
62
|
|
|
$this->logViewer = $logViewer; |
63
|
|
|
$this->perPage = config('arcanesoft.foundation.log-viewer.per-page', $this->perPage); |
64
|
|
|
$this->setCurrentPage('foundation-system-logviewer'); |
65
|
|
|
$this->addBreadcrumbRoute('LogViewer', 'admin::foundation.system.log-viewer.index'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* ------------------------------------------------------------------------------------------------ |
69
|
|
|
| Main Functions |
70
|
|
|
| ------------------------------------------------------------------------------------------------ |
71
|
|
|
*/ |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Show the LogViewer Dashboard. |
75
|
|
|
* |
76
|
|
|
* @return \Illuminate\View\View |
77
|
|
|
*/ |
78
|
|
|
public function index() |
79
|
|
|
{ |
80
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_DASHBOARD); |
81
|
|
|
|
82
|
|
|
$stats = $this->logViewer->statsTable(); |
83
|
|
|
$percents = $this->calcPercentages($stats->footer(), $stats->header()); |
84
|
|
|
|
85
|
|
|
$this->setTitle('LogViewer Dashboard'); |
86
|
|
|
$this->addBreadcrumb('Dashboard'); |
87
|
|
|
|
88
|
|
|
return $this->view('admin.system.log-viewer.dashboard', compact('percents')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* List all logs. |
93
|
|
|
* |
94
|
|
|
* @param \Illuminate\Http\Request $request |
95
|
|
|
* |
96
|
|
|
* @return \Illuminate\View\View |
97
|
|
|
*/ |
98
|
|
|
public function listLogs(Request $request) |
99
|
|
|
{ |
100
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_LIST); |
101
|
|
|
|
102
|
|
|
$stats = $this->logViewer->statsTable(); |
103
|
|
|
$headers = $stats->header(); |
104
|
|
|
// $footer = $stats->footer(); |
105
|
|
|
|
106
|
|
|
$page = $request->get('page', 1); |
107
|
|
|
$offset = ($page * $this->perPage) - $this->perPage; |
108
|
|
|
$rows = new LengthAwarePaginator( |
109
|
|
|
array_slice($stats->rows(), $offset, $this->perPage, true), |
110
|
|
|
count($stats->rows()), |
111
|
|
|
$this->perPage, |
112
|
|
|
$page |
113
|
|
|
); |
114
|
|
|
$rows->setPath($request->url()); |
115
|
|
|
|
116
|
|
|
$this->setTitle($title = trans('foundation::log-viewer.titles.logs-list')); |
117
|
|
|
$this->addBreadcrumb($title); |
118
|
|
|
|
119
|
|
|
return $this->view('admin.system.log-viewer.list', compact('headers', 'rows', 'footer')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Show the log entries by date. |
124
|
|
|
* |
125
|
|
|
* @param string $date |
126
|
|
|
* |
127
|
|
|
* @return \Illuminate\View\View |
128
|
|
|
*/ |
129
|
|
|
public function show($date) |
130
|
|
|
{ |
131
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_SHOW); |
132
|
|
|
|
133
|
|
|
$log = $this->getLogOrFail($date); |
134
|
|
|
$levels = $this->logViewer->levelsNames(); |
135
|
|
|
$entries = $log->entries($level = 'all')->paginate($this->perPage); |
136
|
|
|
|
137
|
|
|
$this->addBreadcrumbRoute(trans('foundation::log-viewer.titles.logs-list'), 'admin::foundation.system.log-viewer.logs.list'); |
138
|
|
|
$this->setTitle($title = "Log : {$date}"); |
139
|
|
|
$this->addBreadcrumb($title); |
140
|
|
|
|
141
|
|
|
return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'level', 'search', 'entries')); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Filter the log entries by date and level. |
146
|
|
|
* |
147
|
|
|
* @param string $date |
148
|
|
|
* @param string $level |
149
|
|
|
* |
150
|
|
|
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse |
151
|
|
|
*/ |
152
|
|
|
public function showByLevel($date, $level) |
153
|
|
|
{ |
154
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_SHOW); |
155
|
|
|
|
156
|
|
|
$log = $this->getLogOrFail($date); |
157
|
|
|
|
158
|
|
|
if ($level == 'all') |
159
|
|
|
return redirect()->route('admin::foundation.system.log-viewer.logs.show', [$date]); |
160
|
|
|
|
161
|
|
|
$levels = $this->logViewer->levelsNames(); |
162
|
|
|
$entries = $this->logViewer->entries($date, $level)->paginate($this->perPage); |
163
|
|
|
|
164
|
|
|
$this->addBreadcrumbRoute(trans('foundation::log-viewer.titles.logs-list'), 'admin::foundation.system.log-viewer.logs.list'); |
165
|
|
|
|
166
|
|
|
$this->setTitle($date.' | '.ucfirst($level)); |
167
|
|
|
$this->addBreadcrumbRoute($date, 'admin::foundation.system.log-viewer.logs.show', [$date]); |
168
|
|
|
$this->addBreadcrumb(ucfirst($level)); |
169
|
|
|
|
170
|
|
|
return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'entries')); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Show the log with the search query. |
175
|
|
|
* |
176
|
|
|
* @param string $date |
177
|
|
|
* @param string $level |
178
|
|
|
* @param \Illuminate\Http\Request $request |
179
|
|
|
* |
180
|
|
|
* @return \Illuminate\View\View |
181
|
|
|
*/ |
182
|
|
|
public function search($date, $level = 'all', Request $request) |
183
|
|
|
{ |
184
|
|
|
$log = $this->getLogOrFail($date); |
185
|
|
|
|
186
|
|
|
if (is_null($query = $request->get('query'))) |
187
|
|
|
return redirect()->route('admin::foundation.system.log-viewer.logs.show', [$date]); |
188
|
|
|
|
189
|
|
|
$levels = $this->logViewer->levelsNames(); |
190
|
|
|
$entries = $log->entries($level)->filter(function (LogEntry $value) use ($query) { |
191
|
|
|
return Str::contains($value->header, $query); |
192
|
|
|
})->paginate($this->perPage); |
193
|
|
|
|
194
|
|
|
return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'level', 'query', 'entries')); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Download the log. |
199
|
|
|
* |
200
|
|
|
* @param string $date |
201
|
|
|
* |
202
|
|
|
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
203
|
|
|
*/ |
204
|
|
|
public function download($date) |
205
|
|
|
{ |
206
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_DOWNLOAD); |
207
|
|
|
|
208
|
|
|
return $this->logViewer->download($date); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Delete a log. |
213
|
|
|
* |
214
|
|
|
* @param string $date |
215
|
|
|
* |
216
|
|
|
* @return \Illuminate\Http\JsonResponse |
217
|
|
|
*/ |
218
|
|
|
public function delete($date) |
219
|
|
|
{ |
220
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_DELETE); |
221
|
|
|
|
222
|
|
|
if ($this->logViewer->delete($date)) { |
223
|
|
|
$this->notifySuccess( |
224
|
|
|
$message = trans('foundation::log-viewer.messages.deleted.message', compact('date')), |
225
|
|
|
trans('foundation::log-viewer.messages.deleted.title') |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
return $this->jsonResponseSuccess(compact('message')); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $this->jsonResponseError([ |
232
|
|
|
'message' => "An error occurred while deleting the log [$date]" |
233
|
|
|
]); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/* ----------------------------------------------------------------- |
237
|
|
|
| Other Methods |
238
|
|
|
| ----------------------------------------------------------------- |
239
|
|
|
*/ |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get a log or fail. |
243
|
|
|
* |
244
|
|
|
* @param string $date |
245
|
|
|
* |
246
|
|
|
* @return Log|null |
247
|
|
|
*/ |
248
|
|
|
private function getLogOrFail($date) |
249
|
|
|
{ |
250
|
|
|
$log = null; |
251
|
|
|
|
252
|
|
|
try { |
253
|
|
|
$log = $this->logViewer->get($date); |
254
|
|
|
} |
255
|
|
|
catch(LogNotFoundException $e) { |
256
|
|
|
abort(404, $e->getMessage()); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $log; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Calculate the percentage. |
264
|
|
|
* |
265
|
|
|
* @param array $total |
266
|
|
|
* @param array $names |
267
|
|
|
* |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
|
|
private function calcPercentages(array $total, array $names) |
271
|
|
|
{ |
272
|
|
|
$percents = []; |
273
|
|
|
$all = Arr::get($total, 'all'); |
274
|
|
|
|
275
|
|
|
foreach ($total as $level => $count) { |
276
|
|
|
$percents[$level] = [ |
277
|
|
|
'name' => $names[$level], |
278
|
|
|
'count' => $count, |
279
|
|
|
'percent' => round(($count / $all) * 100, 2), |
280
|
|
|
]; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return $percents; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|