Passed
Push — develop ( 0fc46b...6b7c8d )
by Tony
04:19
created

EventlogDataTable::getBuilderParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * app/DataTables/General/EventlogDataTable.php
4
 *
5
 * Datatable for eventlogs
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\General;
27
28
use App\DataTables\BaseDataTable;
29
use App\Models\Device;
30
use App\Models\General\Eventlog;
31
32
class EventlogDataTable extends BaseDataTable
33
{
34
35
    protected $device_id;
36
37
    /**
38
     * Display ajax response.
39
     *
40
     * @return \Illuminate\Http\JsonResponse
41
     */
42
    public function ajax()
43
    {
44
        return $this->datatables
45
            ->eloquent($this->query())
46
            ->addColumn('hostname', 'datatables.generic.hostname')
47
            ->rawColumns(['hostname'])
48
            ->make(true);
49
    }
50
51
    /**
52
     * Get the query object to be processed by datatables.
53
     *
54
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
55
     */
56
    public function query()
57
    {
58
        if (is_numeric($this->device_id)) {
59
            // eventlogs for a single device
60
            $eventlogs = Device::find($this->device_id)->eventlogs()->select('eventlog.*');
61
        } else {
62
            $eventlogs = Eventlog::with([
63
                'device' => function ($query) {
64
                    return $query->addSelect(['device_id', 'hostname']);
65
                },
66
            ])->select('eventlog.*');
67
        }
68
        return $this->applyScopes($eventlogs);
69
    }
70
71
    /**
72
     * Get columns.
73
     *
74
     * @return array
75
     */
76 1
    public function getColumns()
77
    {
78
        return [
79
            'hostname' => [
80 1
                'title'     => trans('devices.label.hostname'),
81
                'orderable' => false,
82 1
            ],
83
            'type'     => [
84 1
                'title' => trans('general.text.type'),
85 1
                'name'  => 'eventlog.type',
86
            ],
87
            'message'  => [
88 1
                'title' => trans('general.text.message'),
89
            ],
90
            'datetime' => [
91 1
                'title' => trans('general.text.timestamp'),
92
            ],
93
        ];
94
    }
95
96
    /**
97
     * Sort by timestamp descending
98
     *
99
     * @return array
100
     */
101 1
    public function getBuilderParameters()
102
    {
103 1
        $params = parent::getBuilderParameters();
104 1
        $params['order'] = [[3, 'desc']];
105 1
        return $params;
106
    }
107
108
    /**
109
     * Get filename for export.
110
     *
111
     * @return string
112
     */
113
    protected function filename()
114
    {
115
        return 'eventlog';
116
    }
117
118
    /**
119
     * Get ajax url.
120
     *
121
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
122
     */
123 1
    public function getAjax()
124
    {
125 1
        return url('eventlog?device_id=' . $this->device_id);
126
    }
127
128 1
    public function forDevice($device_id)
129
    {
130 1
        $this->device_id = $device_id;
131 1
        return $this;
132
    }
133
}
134