Completed
Pull Request — develop (#121)
by Neil
03:16
created

AlertsDataTable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 43.18%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 7
c 8
b 1
f 1
lcom 1
cbo 4
dl 0
loc 104
ccs 19
cts 44
cp 0.4318
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B ajax() 0 34 3
A query() 0 5 1
B getColumns() 0 24 1
A filename() 0 4 1
A getAjax() 0 4 1
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
                }
64
                else {
65
                    $btn  = "btn-success";
66
                    $icon = "volume-up";
67
                }
68
                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>';
69
            })
70
            ->make(true);
71
    }
72
73
    /**
74
     * Get the query object to be processed by datatables.
75
     *
76
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
77
     */
78
    public function query()
79
    {
80
        $alerts = Alert::with('device', 'rule')->active()->select('alerts.*');
81
        return $this->applyScopes($alerts);
82
    }
83
84
    /**
85
     * Get columns.
86
     *
87
     * @return array
88 1
     */
89
    public function getColumns()
90 1
    {
91 1
        return [
92 1
            'id'        => [
93
                'title' => trans('alerting.alerts.text.id'),
94
            ],
95
            'state'     => [
96
                'title' => trans('alerting.general.text.state'),
97
            ],
98
            'rule.name' => [
99
                'title' => trans('alerting.general.text.rule'),
100 1
            ],
101
            'device.hostname' => [
102
                'title'       => trans('devices.label.hostname'),
103
            ],
104 1
            'timestamp',
105 1
            'rule.severity' => [
106
                'title'     => trans('alerting.alerts.text.severity'),
107 1
            ],
108 1
            'actions' => [
109
                'className' =>'alerts-ack',
110 1
            ],
111 1
        ];
112
    }
113 1
114 1
    /**
115 1
     * Get filename for export.
116
     *
117 1
     * @return string
118 1
     */
119
    protected function filename()
120 1
    {
121 1
        return 'alerts';
122 1
    }
123
124
    /**
125
     * Get ajax url.
126
     *
127
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
128
     */
129
    public function getAjax()
130
    {
131
        return url('alerting/alerts');
132
    }
133
134
}
135