AlertsDataTable::ajax()   B
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 21
nc 1
nop 0
dl 0
loc 36
ccs 0
cts 12
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * app/DataTables/Alerting/AlertsDataTable.php
4
 *
5
 * Datatable for alerts
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\Alert;
30
31
class AlertsDataTable 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('id', '#{{ $id }}')
43
            ->editColumn('state', '@if ($state == 0)
44
                                        <div class="label label-success">SUCCESS</div>
45
                                    @elseif ($state == 1)
46
                                        <div class="label label-danger">FAILED</div>
47
                                    @elseif ($state == 2)
48
                                        <div class="label label-warning">MUTED</div>
49
                                    @else
50
                                        <div class="label label-default">UNKNOWN</div>
51
                                    @endif')
52
            ->editColumn('rule.name', function ($alert) {
53
                return '<a href="'.url("alerting/rules/".$alert->rule_id).'">'.$alert->rule->name.'</a>';
54
            })
55
            ->editColumn('device.hostname', function ($alert) {
56
                $hostname = is_null($alert->device) ? trans('devices.text.deleted') : $alert->device->hostname;
57
                return '<a href="'.url("devices/".$alert->device_id).'">'.$hostname.'</a>';
58
            })
59
            ->addColumn('actions', function ($alert) {
60
                if ($alert->state == 2) {
61
                    $btn  = "btn-danger";
62
                    $icon = "volume-off";
63
                } else {
64
                    $btn  = "btn-success";
65
                    $icon = "volume-up";
66
                }
67
                return '<a id="alerts-ack" data-id="'.$alert->id.'" data-state="'.$alert->state.'" class="btn btn-xs '.$btn.'"><i class="fa fa-'.$icon.' fa-fw"></i></a>';
68
            })
69
            ->editColumn('rule.severity', function ($alert) {
70
                return $alert->rule->severity;
71
            })
72
            ->make(true);
73
    }
74
75
    /**
76
     * Get the query object to be processed by datatables.
77
     *
78
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
79
     */
80
    public function query()
81
    {
82
        $alerts = Alert::has('rule')->active()->select('alerts.*');
83
        return $this->applyScopes($alerts);
84
    }
85
86
    /**
87
     * Get columns.
88
     *
89
     * @return array
90
     */
91 1
    public function getColumns()
92
    {
93
        return [
94
            'id'        => [
95 1
                'title' => trans('alerting.alerts.text.id'),
96 1
            ],
97
            'state'     => [
98 1
                'title' => trans('alerting.general.text.state'),
99
            ],
100
            'rule.name' => [
101 1
                'title' => trans('alerting.general.text.rule'),
102
            ],
103
            'device.hostname' => [
104 1
                'title'       => trans('devices.label.hostname'),
105
            ],
106 1
            'timestamp',
107
            'rule.severity' => [
108 1
                'title'     => trans('alerting.alerts.text.severity'),
109
            ],
110
            'actions' => [
111
                'className' =>'alerts-ack',
112
            ],
113
        ];
114
    }
115
116
    /**
117
     * Get filename for export.
118
     *
119
     * @return string
120
     */
121
    protected function filename()
122
    {
123
        return 'alerts';
124
    }
125
126
    /**
127
     * Get ajax url.
128
     *
129
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
130
     */
131 1
    public function getAjax()
132
    {
133 1
        return url('alerting/alerts');
134
    }
135
}
136