Completed
Push — master ( 690f8b...84e983 )
by ARCANEDEV
15:22
created

LogViewerController::prepareChartData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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