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

View differences:

SysLogController.groovy
40 40
		
41 41
		flash['warnings'] = null
42 42
		
43
		def entries = LoggedData.list()
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

  
44 80
		Map devices = new HashMap()
45 81
		Map icons   = new HashMap()
46 82
		List unknownDevices = new ArrayList()
......
62 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>"
63 99
		}
64 100
		
65
		[data: entries, devices: devices, icons: icons]
101
		[data: entries, devices: devices, icons: icons, devicesForCheckBox:devicesForCheckBox]
66 102
		
67 103
	}
68 104
	
......
122 158
		[data: data, devices: devices, params:params]
123 159
		
124 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
	}
125 303
	
126 304
}

Also available in: Unified diff