Statistics
| Revision:

root / hci / trunk / eneraptor-web-app / grails-app / controllers / com / eneraptor / hci / SysLogController.groovy @ 42

History | View | Annotate | Download (7.62 KB)

1
package com.eneraptor.hci
2

    
3
import groovy.sql.*
4
import java.sql.*
5

    
6
class SysLogController {
7

    
8
        def dataSource
9
        
10
    def index = { }
11
        
12
        def main = {
13
                
14
                /*
15
                 * DISK USAGE - ONLY IF ENERAPOTR HCI IS SUPERUSER
16
                 */
17
                def freeSpace = 0
18
                def totalSpace = 0
19
                def percent = 0
20
                /*
21
                def db_sql = new Sql(dataSource)
22
                
23
                def single = db_sql.firstRow("show data_directory")
24
                def loca = single.data_directory
25
                def partition = new File(loca)
26
                totalSpace = partition.getTotalSpace()
27
                freeSpace  = partition.getFreeSpace()
28
                
29
                totalSpace = (int)((totalSpace/1024)/1024)
30
                freeSpace = (int)((freeSpace/1024)/1024)
31
                
32
                percent = 100 - ((int) ((freeSpace/totalSpace)*100))
33
                */
34
                
35
                [totalSpace: totalSpace, freeSpace: freeSpace, percent: percent]
36
                
37
        }
38
        
39
        def browse = {
40
                
41
                flash['warnings'] = null
42
                
43
                List devicesForCheckBox = new ArrayList()
44
                DeviceInfo.findAllByInputDeviceOrOutputDevice(true,true).each {
45
                        devicesForCheckBox << it
46
                }
47
                
48
                def entries = new ArrayList()
49
                
50
                List checkedDevices = new ArrayList()
51
                devicesForCheckBox.each {
52
                        if(params['cb_' + it.deviceId]) {
53
                                checkedDevices << it.deviceId
54
                        }
55
                }
56
                
57
                if(params['fromDate'] && params['tillDate']) {
58
                        if(params['all_cb']) {
59
                                def c = LoggedData.createCriteria()
60
                                entries = c {
61
                                        and {
62
                                                between("dateRecieved",params['fromDate'],params['tillDate'])
63
                                        }
64
                                        order("dateRecieved","asc")
65
                                }
66
                        } else {
67
                                if(checkedDevices.size > 0) {
68
                                        def c = LoggedData.createCriteria()
69
                                        entries = c {
70
                                                and {
71
                                                        between("dateRecieved",params['fromDate'],params['tillDate'])
72
                                                        'in'("deviceId",checkedDevices)
73
                                                }
74
                                                order("dateRecieved","asc")
75
                                        }
76
                                }
77
                        }
78
                }
79

    
80
                Map devices = new HashMap()
81
                Map icons   = new HashMap()
82
                List unknownDevices = new ArrayList()
83
                DeviceInfo.list().each {
84
                        devices[it.deviceId] = it.friendlyName
85
                        if(it.iconPath) {
86
                                icons[it.deviceId] = it.iconPath
87
                        }
88
                }
89
                entries.each {
90
                        if(!devices[it.deviceId]) {
91
                                if(!unknownDevices.contains(it.deviceId)) {
92
                                        unknownDevices << it.deviceId
93
                                }
94
                        }
95
                }
96
                unknownDevices.each {
97
                        if(!flash['warnings']) flash['warnings'] = ""
98
                        flash['warnings'] += "<p>There was found at least one log entry for a device with id <b>${it}</b>, which is <b>unknown</b> to HCI. You should <b>add this device</b> by <b>" + g.link(controller: 'config', action: 'addDevice', params: ['deviceId' : it], "clicking here") + "</b>.</p>"
99
                }
100
                
101
                [data: entries, devices: devices, icons: icons, devicesForCheckBox:devicesForCheckBox]
102
                
103
        }
104
        
105
        def detail = {
106
                
107
                def detailsOf = LoggedData.findById(params.id)
108
                if(!detailsOf) {
109
                        flash['errors'] = "<p>There is no logged data with id ${params.id}</p>"
110
                        redirect(action: 'browse')
111
                        return false
112
                }
113
                return detailsOf.getProperties()
114
        }
115
        
116
        def deleteEntry = {
117
                
118
                def entry = LoggedData.findById(params.id)
119
                if(entry) {
120
                        entry.delete()
121
                        flash['confirms'] = "<p>Log entry with id " + params.id + " deleted successfully.</p>"
122
                        redirect(action: 'browse')
123
                } else {
124
                        flash['errors'] = "<p>Cannot find log entry with id " + params.id + "</p>"
125
                        redirect(action: 'browse')
126
                }
127
                
128
        }
129
        
130
        def graph = {
131
                
132
                List devices = new ArrayList()
133
                List data = new ArrayList()
134
                
135
                DeviceInfo.findAllByInputDevice(true).each {
136
                        devices << it
137
                }
138
                
139
                if(params['dataList'])
140
                        data = params['dataList']
141
                else if(params['deviceId'] && params['fromDate'] && params['tillDate']) {
142
                        
143
                        def c = LoggedData.createCriteria()
144
                        def results = c {
145
                                and {
146
                                        eq("deviceId",params['deviceId'])
147
                                        between("dateRecieved",params['fromDate'],params['tillDate'])
148
                                }
149
                                order("dateRecieved","asc")
150
                        }
151
                        
152
                        results.each {
153
                                data << [it.dateRecieved.getTime(),it.reportedData]
154
                        }
155
                        
156
                }
157
                
158
                [data: data, devices: devices, params:params]
159
                
160
        }
161
        
162
        def export = {
163
                
164
                List devicesForCheckBox = new ArrayList()
165
                DeviceInfo.findAllByInputDeviceOrOutputDevice(true,true).each {
166
                        devicesForCheckBox << it
167
                }
168
                
169
                [devicesForCheckBox: devicesForCheckBox]
170
                
171
        }
172
        
173
        def exportDo = {
174
                
175
                List devicesForCheckBox = new ArrayList()
176
                DeviceInfo.findAllByInputDeviceOrOutputDevice(true,true).each {
177
                        devicesForCheckBox << it
178
                }
179
                
180
                List checkedDevices = new ArrayList()
181
                devicesForCheckBox.each {
182
                        if(params['cb_' + it.deviceId]) {
183
                                checkedDevices << it.deviceId
184
                        }
185
                }
186
                
187
                List entries = new ArrayList()
188
                def cont = ""
189
                
190
                if(params['all_cb']) {
191
                        def c = LoggedData.createCriteria()
192
                        entries = c {
193
                                and {
194
                                        between("dateRecieved",params['fromDate'],params['tillDate'])
195
                                }
196
                                order("dateRecieved","asc")
197
                        }
198
                } else {
199
                        if(checkedDevices.size > 0) {
200
                                def c = LoggedData.createCriteria()
201
                                entries = c {
202
                                        and {
203
                                                between("dateRecieved",params['fromDate'],params['tillDate'])
204
                                                'in'("deviceId",checkedDevices)
205
                                        }
206
                                        order("dateRecieved","asc")
207
                                }
208
                        }
209
                }
210
                
211
                if(entries.size == 0) {
212
                        flash['errors'] = "<p>Nothing to export!</p><p>There are no log entries for selected devices in the time interval specified.</p>"
213
                        redirect(action:'export')
214
                }
215
                else if(params['format'] == "Plain text (.txt)") {
216
                        entries.each {
217
                                cont += it.dateRecieved.toString() + " " + it.deviceId + " " + it.reportedData + "\n"
218
                        }
219
                        render(text: cont,contentType:"text/plain",encoding:"UTF-8")
220
                } else if(params['format'] == "PostgreSQL script (.sql)") {
221
                        entries.each {
222
                                cont += "INSERT INTO logged_data(id, \"version\", date_recieved, device_id, reported_data) VALUES (nextval('hibernate_sequence'), 0, '" + it.dateRecieved.toString() + "', '" + it.deviceId + "', '" + it.reportedData + "');\n";
223
                        }
224
                        render(text: cont,contentType:"text/plain",encoding:"UTF-8")
225
                } else {
226
                        cont += "<log>\n"
227
                        entries.each {
228
                                cont += "\t<entry>\n"
229
                                cont += "\t\t<device>" + it.deviceId + "</device>\n"
230
                                cont += "\t\t<time>" + it.dateRecieved.toString() + "</time>\n"
231
                                cont += "\t\t<data>\n"
232
                                cont += "\t\t\t" + it.reportedData + "\n"
233
                                cont += "\t\t</data>\n"
234
                                cont += "\t</entry>\n"
235
                        }
236
                        cont += "</log>"
237
                        render(text: cont,contentType:"text/xml",encoding:"UTF-8")
238
                }
239
        }
240
        
241
        def clear = {
242
                
243
                List devicesForCheckBox = new ArrayList()
244
                DeviceInfo.findAllByInputDeviceOrOutputDevice(true,true).each {
245
                        devicesForCheckBox << it
246
                }
247
                
248
                [devicesForCheckBox: devicesForCheckBox]
249
                
250
        }
251
        
252
        def clearDo = {
253
                
254
                List devicesForCheckBox = new ArrayList()
255
                DeviceInfo.findAllByInputDeviceOrOutputDevice(true,true).each {
256
                        devicesForCheckBox << it
257
                }
258
                
259
                List checkedDevices = new ArrayList()
260
                devicesForCheckBox.each {
261
                        if(params['cb_' + it.deviceId]) {
262
                                checkedDevices << it.deviceId
263
                        }
264
                }
265
                
266
                List entries = new ArrayList()
267
                def cont = ""
268
                
269
                if(params['all_cb']) {
270
                        def c = LoggedData.createCriteria()
271
                        entries = c {
272
                                and {
273
                                        between("dateRecieved",params['fromDate'],params['tillDate'])
274
                                }
275
                                order("dateRecieved","asc")
276
                        }
277
                } else {
278
                        if(checkedDevices.size > 0) {
279
                                def c = LoggedData.createCriteria()
280
                                entries = c {
281
                                        and {
282
                                                between("dateRecieved",params['fromDate'],params['tillDate'])
283
                                                'in'("deviceId",checkedDevices)
284
                                        }
285
                                        order("dateRecieved","asc")
286
                                }
287
                        }
288
                }
289
                
290
                if(entries.size == 0) {
291
                        flash['errors'] = "<p>Nothing to clear!</p><p>There are no log entries for selected devices in the time interval specified.</p>"
292
                } else {
293
                        def entCleared = 0
294
                        entries.each {
295
                                it.delete()
296
                                entCleared++
297
                        }
298
                        flash['confirms'] = "<p>" + entCleared + " log entries succesfully cleared!</p>"
299
                }
300
                redirect(action:'clear')
301
                
302
        }
303
        
304
}