Statistics
| Revision:

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

History | View | Annotate | Download (10.2 KB)

1 3 alexbesir
package com.eneraptor.hci
2
3 11 alexbesir
import groovy.sql.*
4
import java.sql.*
5
6 3 alexbesir
class SysLogController {
7 11 alexbesir
8
        def dataSource
9
10 3 alexbesir
    def index = { }
11
12
        def main = {
13
14 16 alexbesir
                /*
15
                 * DISK USAGE - ONLY IF ENERAPOTR HCI IS SUPERUSER
16
                 */
17
                def freeSpace = 0
18
                def totalSpace = 0
19
                def percent = 0
20
                /*
21 11 alexbesir
                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 16 alexbesir
                totalSpace = partition.getTotalSpace()
27
                freeSpace  = partition.getFreeSpace()
28 11 alexbesir

29
                totalSpace = (int)((totalSpace/1024)/1024)
30
                freeSpace = (int)((freeSpace/1024)/1024)
31

32 16 alexbesir
                percent = 100 - ((int) ((freeSpace/totalSpace)*100))
33
                */
34 11 alexbesir
35
                [totalSpace: totalSpace, freeSpace: freeSpace, percent: percent]
36
37 3 alexbesir
        }
38
39
        def browse = {
40
41 6 alexbesir
                flash['warnings'] = null
42
43 42 alexbesir
                List devicesForCheckBox = new ArrayList()
44 66 alexbesir
45
                def crit1 = DeviceInfo.createCriteria()
46
                devicesForCheckBox = crit1 {
47
                        and {
48
                                or {
49
                                        eq("outputDevice",true)
50
                                        eq("inputDevice",true)
51
                                }
52
                                eq("hwSet",session.hwSet)
53
                        }
54
                        order("friendlyName","asc")
55 42 alexbesir
                }
56
57
                def entries = new ArrayList()
58
59
                List checkedDevices = new ArrayList()
60
                devicesForCheckBox.each {
61
                        if(params['cb_' + it.deviceId]) {
62
                                checkedDevices << it.deviceId
63
                        }
64
                }
65
66
                if(params['fromDate'] && params['tillDate']) {
67
                        if(params['all_cb']) {
68
                                def c = LoggedData.createCriteria()
69
                                entries = c {
70
                                        and {
71
                                                between("dateRecieved",params['fromDate'],params['tillDate'])
72
                                        }
73
                                        order("dateRecieved","asc")
74
                                }
75
                        } else {
76
                                if(checkedDevices.size > 0) {
77
                                        def c = LoggedData.createCriteria()
78
                                        entries = c {
79
                                                and {
80
                                                        between("dateRecieved",params['fromDate'],params['tillDate'])
81
                                                        'in'("deviceId",checkedDevices)
82
                                                }
83
                                                order("dateRecieved","asc")
84
                                        }
85
                                }
86
                        }
87
                }
88
89 6 alexbesir
                Map devices = new HashMap()
90
                Map icons   = new HashMap()
91
                List unknownDevices = new ArrayList()
92
                DeviceInfo.list().each {
93
                        devices[it.deviceId] = it.friendlyName
94
                        if(it.iconPath) {
95
                                icons[it.deviceId] = it.iconPath
96
                        }
97
                }
98
                entries.each {
99
                        if(!devices[it.deviceId]) {
100
                                if(!unknownDevices.contains(it.deviceId)) {
101
                                        unknownDevices << it.deviceId
102
                                }
103
                        }
104
                }
105
                unknownDevices.each {
106
                        if(!flash['warnings']) flash['warnings'] = ""
107
                        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>"
108
                }
109 3 alexbesir
110 42 alexbesir
                [data: entries, devices: devices, icons: icons, devicesForCheckBox:devicesForCheckBox]
111 3 alexbesir
112
        }
113
114
        def detail = {
115
116 6 alexbesir
                def detailsOf = LoggedData.findById(params.id)
117
                if(!detailsOf) {
118
                        flash['errors'] = "<p>There is no logged data with id ${params.id}</p>"
119
                        redirect(action: 'browse')
120
                        return false
121
                }
122
                return detailsOf.getProperties()
123 3 alexbesir
        }
124
125
        def deleteEntry = {
126
127
                def entry = LoggedData.findById(params.id)
128
                if(entry) {
129
                        entry.delete()
130
                        flash['confirms'] = "<p>Log entry with id " + params.id + " deleted successfully.</p>"
131
                        redirect(action: 'browse')
132
                } else {
133
                        flash['errors'] = "<p>Cannot find log entry with id " + params.id + "</p>"
134
                        redirect(action: 'browse')
135
                }
136
137
        }
138 11 alexbesir
139
        def graph = {
140
141
                List devices = new ArrayList()
142
                List data = new ArrayList()
143
144 66 alexbesir
                def crit1 = DeviceInfo.createCriteria()
145
                devices = crit1 {
146
                        and {
147
                                eq("inputDevice",true)
148
                                eq("hwSet",session.hwSet)
149
                        }
150
                        order("friendlyName","asc")
151 11 alexbesir
                }
152
153
                if(params['dataList'])
154
                        data = params['dataList']
155
                else if(params['deviceId'] && params['fromDate'] && params['tillDate']) {
156
157
                        def c = LoggedData.createCriteria()
158
                        def results = c {
159
                                and {
160
                                        eq("deviceId",params['deviceId'])
161
                                        between("dateRecieved",params['fromDate'],params['tillDate'])
162
                                }
163
                                order("dateRecieved","asc")
164
                        }
165
166
                        results.each {
167
                                data << [it.dateRecieved.getTime(),it.reportedData]
168
                        }
169
170
                }
171
172
                [data: data, devices: devices, params:params]
173
174
        }
175 42 alexbesir
176
        def export = {
177
178
                List devicesForCheckBox = new ArrayList()
179 66 alexbesir
180
                def crit1 = DeviceInfo.createCriteria()
181
                devicesForCheckBox = crit1 {
182
                        and {
183
                                or {
184
                                        eq("outputDevice",true)
185
                                        eq("inputDevice",true)
186
                                }
187
                                eq("hwSet",session.hwSet)
188
                        }
189
                        order("friendlyName","asc")
190 42 alexbesir
                }
191
192
                [devicesForCheckBox: devicesForCheckBox]
193
194
        }
195
196
        def exportDo = {
197
198
                List devicesForCheckBox = new ArrayList()
199
                DeviceInfo.findAllByInputDeviceOrOutputDevice(true,true).each {
200
                        devicesForCheckBox << it
201
                }
202
203
                List checkedDevices = new ArrayList()
204
                devicesForCheckBox.each {
205
                        if(params['cb_' + it.deviceId]) {
206
                                checkedDevices << it.deviceId
207
                        }
208
                }
209
210
                List entries = new ArrayList()
211
                def cont = ""
212
213
                if(params['all_cb']) {
214
                        def c = LoggedData.createCriteria()
215
                        entries = c {
216
                                and {
217
                                        between("dateRecieved",params['fromDate'],params['tillDate'])
218
                                }
219
                                order("dateRecieved","asc")
220
                        }
221
                } else {
222
                        if(checkedDevices.size > 0) {
223
                                def c = LoggedData.createCriteria()
224
                                entries = c {
225
                                        and {
226
                                                between("dateRecieved",params['fromDate'],params['tillDate'])
227
                                                'in'("deviceId",checkedDevices)
228
                                        }
229
                                        order("dateRecieved","asc")
230
                                }
231
                        }
232
                }
233
234
                if(entries.size == 0) {
235
                        flash['errors'] = "<p>Nothing to export!</p><p>There are no log entries for selected devices in the time interval specified.</p>"
236
                        redirect(action:'export')
237
                }
238
                else if(params['format'] == "Plain text (.txt)") {
239
                        entries.each {
240
                                cont += it.dateRecieved.toString() + " " + it.deviceId + " " + it.reportedData + "\n"
241
                        }
242
                        render(text: cont,contentType:"text/plain",encoding:"UTF-8")
243
                } else if(params['format'] == "PostgreSQL script (.sql)") {
244
                        entries.each {
245
                                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";
246
                        }
247
                        render(text: cont,contentType:"text/plain",encoding:"UTF-8")
248
                } else {
249
                        cont += "<log>\n"
250
                        entries.each {
251
                                cont += "\t<entry>\n"
252
                                cont += "\t\t<device>" + it.deviceId + "</device>\n"
253
                                cont += "\t\t<time>" + it.dateRecieved.toString() + "</time>\n"
254
                                cont += "\t\t<data>\n"
255
                                cont += "\t\t\t" + it.reportedData + "\n"
256
                                cont += "\t\t</data>\n"
257
                                cont += "\t</entry>\n"
258
                        }
259
                        cont += "</log>"
260
                        render(text: cont,contentType:"text/xml",encoding:"UTF-8")
261
                }
262
        }
263
264
        def clear = {
265
266
                List devicesForCheckBox = new ArrayList()
267 66 alexbesir
268
                def crit1 = DeviceInfo.createCriteria()
269
                devicesForCheckBox = crit1 {
270
                        and {
271
                                or {
272
                                        eq("outputDevice",true)
273
                                        eq("inputDevice",true)
274
                                }
275
                                eq("hwSet",session.hwSet)
276
                        }
277
                        order("friendlyName","asc")
278 42 alexbesir
                }
279
280
                [devicesForCheckBox: devicesForCheckBox]
281
282
        }
283
284
        def clearDo = {
285
286
                List devicesForCheckBox = new ArrayList()
287
                DeviceInfo.findAllByInputDeviceOrOutputDevice(true,true).each {
288
                        devicesForCheckBox << it
289
                }
290
291
                List checkedDevices = new ArrayList()
292
                devicesForCheckBox.each {
293
                        if(params['cb_' + it.deviceId]) {
294
                                checkedDevices << it.deviceId
295
                        }
296
                }
297
298
                List entries = new ArrayList()
299
                def cont = ""
300
301
                if(params['all_cb']) {
302
                        def c = LoggedData.createCriteria()
303
                        entries = c {
304
                                and {
305
                                        between("dateRecieved",params['fromDate'],params['tillDate'])
306
                                }
307
                                order("dateRecieved","asc")
308
                        }
309
                } else {
310
                        if(checkedDevices.size > 0) {
311
                                def c = LoggedData.createCriteria()
312
                                entries = c {
313
                                        and {
314
                                                between("dateRecieved",params['fromDate'],params['tillDate'])
315
                                                'in'("deviceId",checkedDevices)
316
                                        }
317
                                        order("dateRecieved","asc")
318
                                }
319
                        }
320
                }
321
322
                if(entries.size == 0) {
323
                        flash['errors'] = "<p>Nothing to clear!</p><p>There are no log entries for selected devices in the time interval specified.</p>"
324
                } else {
325
                        def entCleared = 0
326
                        entries.each {
327
                                it.delete()
328
                                entCleared++
329
                        }
330
                        flash['confirms'] = "<p>" + entCleared + " log entries succesfully cleared!</p>"
331
                }
332
                redirect(action:'clear')
333
334
        }
335 68 alexbesir
336
        def live = {
337 76 alexbesir
338
                List outputDevices = new ArrayList()
339
340
                def crit1 = DeviceInfo.createCriteria()
341
                outputDevices = crit1 {
342
                        and {
343
                                eq("outputDevice",true)
344
                                eq("hwSet",session.hwSet)
345
                        }
346
                        order("friendlyName","asc")
347
                }
348
                outputDevices << new DeviceInfo(deviceId: 'all',friendlyName: 'All')
349 68 alexbesir
350
                List devices = new ArrayList()
351
352 76 alexbesir
                def crit2 = DeviceInfo.createCriteria()
353
                devices = crit2 {
354 68 alexbesir
                        and {
355
                                eq("inputDevice",true)
356
                                eq("hwSet",session.hwSet)
357
                        }
358
                        order("friendlyName","asc")
359
                }
360
361 76 alexbesir
                [devices: devices,outputDevices:outputDevices]
362 68 alexbesir
363
        }
364
365
        def liveGraph = {
366
367
                List data = new ArrayList()
368 76 alexbesir
369 68 alexbesir
                java.util.Date now = new java.util.Date()
370 76 alexbesir
                java.util.Date beforeNMinutes = new java.util.Date(
371
                        now.time - (60000 * (params.timeSpan as int))
372
                )
373 68 alexbesir
374 76 alexbesir
                List actionLog = new ArrayList()
375
                List actionData = new ArrayList()
376
377 68 alexbesir
                if(params['deviceId'] && params['timeSpan']) {
378
379
                        def c = LoggedData.createCriteria()
380
                        def results = c {
381
                                and {
382
                                        eq("deviceId",params['deviceId'])
383
                                        between("dateRecieved",beforeNMinutes,now)
384
                                }
385
                                order("dateRecieved","asc")
386
                        }
387
388
                        results.each {
389
                                data << [it.dateRecieved.getTime(),it.reportedData]
390
                        }
391
392
                }
393
394 76 alexbesir
                if(params.outputDevice != 'none') {
395 68 alexbesir
396 76 alexbesir
                        if(params.outputDevice != 'all') {
397
                                def crit2 = LoggedData.createCriteria()
398
                                actionData = crit2 {
399
                                        and {
400
                                                eq("deviceId",params.outputDevice)
401
                                                between("dateRecieved",beforeNMinutes,now)
402
                                        }
403
                                        order("dateRecieved","asc")
404
                                }
405
                        } else {
406
407
                                List allOutputDevices = new ArrayList()
408
                                allOutputDevices = DeviceInfo.findAllByOutputDevice(true)
409
410
                                def crit3 = LoggedData.createCriteria()
411
                                actionData = crit3 {
412
                                        and {
413
                                                "in"("deviceId",allOutputDevices*.deviceId)
414
                                                between("dateRecieved",beforeNMinutes,now)
415
                                        }
416
                                        order("dateRecieved","asc")
417
                                }
418
                        }
419
420
                        actionData.each {
421
                                actionLog << [it.dateRecieved.getTime(),it.reportedData]
422
                        }
423
424
                }
425
426
                [params:params, data:data, actionLog:actionLog]
427
428 68 alexbesir
        }
429 3 alexbesir
430
}