|
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
|
|
|
public function getColumns() |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
'hostname' => [ |
|
80
|
|
|
'title' => trans('devices.label.hostname'), |
|
81
|
|
|
'orderable' => false, |
|
82
|
|
|
], |
|
83
|
|
|
'type' => [ |
|
84
|
|
|
'title' => trans('general.text.type'), |
|
85
|
|
|
'name' => 'eventlog.type', |
|
86
|
|
|
], |
|
87
|
|
|
'message' => [ |
|
88
|
|
|
'title' => trans('general.text.message'), |
|
89
|
|
|
], |
|
90
|
|
|
'datetime' => [ |
|
91
|
|
|
'title' => trans('general.text.timestamp'), |
|
92
|
|
|
], |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Sort by timestamp descending |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getBuilderParameters() |
|
102
|
|
|
{ |
|
103
|
|
|
$params = parent::getBuilderParameters(); |
|
104
|
|
|
$params['order'] = [[3, 'desc']]; |
|
105
|
|
|
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
|
|
|
public function getAjax() |
|
124
|
|
|
{ |
|
125
|
|
|
return url('eventlog?device_id=' . $this->device_id); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function forDevice($device_id) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->device_id = $device_id; |
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.