Completed
Push — fixes ( 6830e4...798510 )
by Tony
03:34
created

LogsDataTable::ajax()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 52
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 1
Metric Value
c 6
b 2
f 1
dl 0
loc 52
rs 6.8493
cc 8
eloc 39
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * app/DataTables/Alerting/LogsDataTable.php
4
 *
5
 * Datatable for alert logs
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Neil Lathwood
23
 * @author     Neil Lathwood <[email protected]>
24
 */
25
26
namespace App\DataTables\Alerting;
27
28
use App\DataTables\BaseDataTable;
29
use App\Models\Alerting\Log;
30
31
class LogsDataTable extends BaseDataTable
32
{
33
    /**
34
     * Display ajax response.
35
     *
36
     * @return \Illuminate\Http\JsonResponse
37
     */
38
    public function ajax()
39
    {
40
        return $this->datatables
41
            ->eloquent($this->query())
42
            ->editColumn('device.hostname', function($log) {
43
                $hostname = is_null($log->device) ? trans('devices.text.deleted') : $log->device->hostname;
44
                return '<a href="'.url("devices/".$log->device_id).'">'.$hostname.'</a>';
45
            })
46
            ->editColumn('rule.name', function($log) {
47
                if ($log->rule_id) {
48
                    return '<a href="'.url("alerting/rules/".$log->rule_id).'">'.$log->rule->name.'</a>';
49
                }
50
                else {
51
                    return trans('alerting.general.text.invalid');
52
                }
53
            })
54
            ->editColumn('state', function($log) {
55
                $icon   = '';
56
                $colour = '';
57
                $text   = '';
58
                if ($log->state == 0) {
59
                    $icon   = 'check';
60
                    $colour = 'green';
61
                    $text   = trans('alerting.logs.text.ok');
62
                }
63
                elseif ($log->state == 1) {
64
                    $icon   = 'times';
65
                    $colour = 'red';
66
                    $text   = trans('alerting.logs.text.fail');
67
                }
68
                elseif ($log->state == 2) {
69
                    $icon   = 'volume-off';
70
                    $colour = 'lightgrey';
71
                    $text   = trans('alerting.logs.text.ack');
72
                }
73
                elseif ($log->state == 3) {
74
                    $icon   = 'arrow-down';
75
                    $colour = 'orange';
76
                    $text   = trans('alerting.logs.text.worse');
77
                }
78
                elseif ($log->state == 4) {
79
                    $icon   = 'arrow-up';
80
                    $colour = 'khaki';
81
                    $text   = trans('alerting.logs.text.better');
82
                }
83
                return '<b><span class="fa fa-'.$icon.'" style="color:'.$colour.'"></span> '.$text.'</b>';
84
            })
85
            ->editColumn('time_logged', function($log) {
86
                return date('Y-m-d H:i:s', $log->time_logged / 1000);
87
            })
88
            ->make(true);
89
    }
90
91
    /**
92
     * Get the query object to be processed by datatables.
93
     *
94
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
95
     */
96
    public function query()
97
    {
98
        $logs = Log::with('device', 'rule')->select('alert_log.*');
99
        return $this->applyScopes($logs);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->applyScopes($logs); of type Illuminate\Database\Quer...tabase\Eloquent\Builder adds the type Illuminate\Database\Query\Builder to the return on line 99 which is incompatible with the return type declared by the interface Yajra\Datatables\Contrac...ataTableContract::query of type Illuminate\Database\Eloquent\Builder.
Loading history...
100
    }
101
102
    /**
103
     * Get columns.
104
     *
105
     * @return array
106
     */
107
    public function getColumns()
108
    {
109
        return [
110
            'device.hostname' => [
111
                'title'       => trans('devices.label.hostname'),
112
            ],
113
            'rule.name' => [
114
                'title' => trans('alerting.general.text.rule'),
115
            ],
116
            'state'     => [
117
                'title' => trans('alerting.general.text.state'),
118
            ],
119
            'time_logged',
120
        ];
121
    }
122
123
    /**
124
     * Get filename for export.
125
     *
126
     * @return string
127
     */
128
    protected function filename()
129
    {
130
        return 'alert_logs';
131
    }
132
}
133