LogsDataTable::ajax()   C
last analyzed

Complexity

Conditions 8
Paths 1

Size

Total Lines 47
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 39
nc 1
nop 0
dl 0
loc 47
ccs 0
cts 0
cp 0
crap 72
rs 5.7377
c 0
b 0
f 0
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
                } else {
50
                    return trans('alerting.general.text.invalid');
51
                }
52
            })
53
            ->editColumn('state', function ($log) {
54
                $icon   = '';
55
                $colour = '';
56
                $text   = '';
57
                if ($log->state == 0) {
58
                    $icon   = 'check';
59
                    $colour = 'green';
60
                    $text   = trans('alerting.logs.text.ok');
61
                } elseif ($log->state == 1) {
62
                    $icon   = 'times';
63
                    $colour = 'red';
64
                    $text   = trans('alerting.logs.text.fail');
65
                } elseif ($log->state == 2) {
66
                    $icon   = 'volume-off';
67
                    $colour = 'lightgrey';
68
                    $text   = trans('alerting.logs.text.ack');
69
                } elseif ($log->state == 3) {
70
                    $icon   = 'arrow-down';
71
                    $colour = 'orange';
72
                    $text   = trans('alerting.logs.text.worse');
73
                } elseif ($log->state == 4) {
74
                    $icon   = 'arrow-up';
75
                    $colour = 'khaki';
76
                    $text   = trans('alerting.logs.text.better');
77
                }
78
                return '<b><span class="fa fa-'.$icon.'" style="color:'.$colour.'"></span> '.$text.'</b>';
79
            })
80
            ->editColumn('time_logged', function ($log) {
81
                return date('Y-m-d H:i:s', $log->time_logged / 1000);
82
            })
83
            ->make(true);
84
    }
85
86
    /**
87
     * Get the query object to be processed by datatables.
88
     *
89
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
90
     */
91
    public function query()
92
    {
93
        $logs = Log::with('device', 'rule')->select('alert_log.*');
94
        return $this->applyScopes($logs);
95
    }
96
97
    /**
98
     * Get columns.
99
     *
100
     * @return array
101
     */
102 1
    public function getColumns()
103
    {
104
        return [
105
            'device.hostname' => [
106 1
                'title'       => trans('devices.label.hostname'),
107
            ],
108
            'rule.name' => [
109 1
                'title' => trans('alerting.general.text.rule'),
110
            ],
111
            'state'     => [
112 1
                'title' => trans('alerting.general.text.state'),
113
            ],
114 1
            'time_logged',
115
        ];
116
    }
117
118
    /**
119
     * Get filename for export.
120
     *
121
     * @return string
122
     */
123
    protected function filename()
124
    {
125
        return 'alert_logs';
126
    }
127
}
128