Completed
Push — develop ( a8d9bd...cad05c )
by Tony
15:59
created

LogsDataTable::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
crap 1
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\Models\Alerting\Alert;
29
use App\Models\Alerting\Log;
30
use Yajra\Datatables\Services\DataTable;
31
32
class LogsDataTable extends DataTable
33
{
34
    /**
35
     * Display ajax response.
36
     *
37
     * @return \Illuminate\Http\JsonResponse
38
     */
39
    public function ajax()
40
    {
41
        return $this->datatables
42
            ->eloquent($this->query())
43
            ->editColumn('device.hostname', function($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { r...'hostname'] . '</a>'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
                return '<a href="'.url("devices/".$this['device']['device_id']).'">'.$this['device']['hostname'].'</a>';
45
            })
46
            ->editColumn('rule.name', function($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { i...text.invalid'); } } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
                if ($this['rule']['id']) {
48
                    return '<a href="'.url("alerting/rules/".$this['rule']['id']).'">'.$this['rule']['name'].'</a>';
49
                } else {
50
                    return trans('alerting.general.text.invalid');
51
                }
52
            })
53
            ->editColumn('state', function($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { $...> ' . $text . '</b>'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
                $icon   = '';
55
                $colour = '';
56
                $text   = '';
57
                if ($this['state'] == 0) {
58
                    $icon   = 'check';
59
                    $colour = 'green';
60
                    $text   = trans('alerting.logs.text.ok');
61
                }
62
                elseif ($this['state'] == 1) {
63
                    $icon   = 'times';
64
                    $colour = 'red';
65
                    $text   = trans('alerting.logs.text.fail');
66
                }
67
                elseif ($this['state'] == 2) {
68
                    $icon   = 'volume-off';
69
                    $colour = 'lightgrey';
70
                    $text   = trans('alerting.logs.text.ack');
71
                }
72
                elseif ($this['state'] == 3) {
73
                    $icon   = 'arrow-down';
74
                    $colour = 'orange';
75
                    $text   = trans('alerting.logs.text.worse');
76
                }
77
                elseif ($this['state'] == 4) {
78
                    $icon   = 'arrow-up';
79
                    $colour = 'khaki';
80
                    $text   = trans('alerting.logs.text.better');
81
                }
82
                return '<b><span class="fa fa-'.$icon.'" style="color:'.$colour.'"></span> '.$text.'</b>';
83
            })
84
            ->make(true);
85
    }
86
87
    /**
88
     * Get the query object to be processed by datatables.
89
     *
90
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
91
     */
92
    public function query()
93
    {
94
        $logs = Log::query()->with('device','user','rule')->select('alert_log.*');
95
        return $this->applyScopes($logs);
96
    }
97
98
    /**
99
     * Optional method if you want to use html builder.
100
     *
101
     * @return \Yajra\Datatables\Html\Builder
102
     */
103 1
    public function html()
104
    {
105 1
        return $this->builder()
106 1
                    ->columns($this->getColumns())
107 1
                    ->parameters($this->getBuilderParameters());
108
    }
109
110
    /**
111
     * Get columns.
112
     *
113
     * @return array
114
     */
115 1
    private function getColumns()
116
    {
117
        return [
118
            'device.hostname' => [
119 1
                'title'       => trans('devices.label.hostname'),
120 1
            ],
121
            'rule.name' => [
122 1
                'title' => trans('alerting.general.text.rule'),
123 1
            ],
124
            'state'     => [
125 1
                'title' => trans('alerting.general.text.state'),
126 1
            ],
127 1
            'time_logged',
128 1
        ];
129
    }
130
131
    /**
132
     * Get filename for export.
133
     *
134
     * @return string
135
     */
136
    protected function filename()
137
    {
138
        return 'logs';
139
    }
140
141
    /**
142
     * Get Builder Params
143
     *
144
     * @return array
145
     */
146 1
    protected function getBuilderParameters()
147
    {
148
        return [
149 1
            'dom' => 'Blfrtip',
150 1
            'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]],
151
            'buttons' => [
152 1
                'csv', 'excel', 'pdf', 'print', 'reset', 'reload',
153 1
            ],
154 1
        ];
155
    }
156
157
}
158