root / hci / trunk / eneraptor-web-app / grails-app / controllers / com / eneraptor / hci / StatisticsController.groovy @ 63
History | View | Annotate | Download (29.1 KB)
1 | 42 | alexbesir | |
---|---|---|---|
2 | 3 | alexbesir | package com.eneraptor.hci
|
3 | |||
4 | 42 | alexbesir | import groovy.sql.Sql |
5 | |||
6 | 3 | alexbesir | class StatisticsController { |
7 | 42 | alexbesir | |
8 | def dataSource
|
||
9 | 63 | alexbesir | |
10 | def graphDataService
|
||
11 | 42 | alexbesir | |
12 | 3 | alexbesir | def main = { }
|
13 | 42 | alexbesir | |
14 | def graphs = {
|
||
15 | |||
16 | List savedGraphs = new ArrayList() |
||
17 | 63 | alexbesir | List combinedGraphs = new ArrayList() |
18 | 42 | alexbesir | |
19 | savedGraphs = EneGraph.list() |
||
20 | 63 | alexbesir | combinedGraphs = CombGraph.list() |
21 | 42 | alexbesir | |
22 | 63 | alexbesir | [savedGraphs: savedGraphs, combinedGraphs:combinedGraphs] |
23 | 42 | alexbesir | |
24 | } |
||
25 | |||
26 | def newGraph = {
|
||
27 | |||
28 | List graphTypes = ["min","max","avg","sum"] |
||
29 | List graphTypesFriendly = ["Minimum value","Maximum value","Average value","Cumulative"] |
||
30 | |||
31 | List devices = new ArrayList() |
||
32 | List devicesFriendly = new ArrayList() |
||
33 | |||
34 | DeviceInfo.list().each { |
||
35 | devices << it.deviceId
|
||
36 | devicesFriendly << it.friendlyName
|
||
37 | } |
||
38 | |||
39 | [graphTypes:graphTypes,graphTypesFriendly:graphTypesFriendly, devices:devices, devicesFriendly:devicesFriendly] |
||
40 | |||
41 | } |
||
42 | |||
43 | def newGraphDo = {
|
||
44 | |||
45 | String errors = "" |
||
46 | Date chosenTimeFrameStart = null |
||
47 | Date chosenTimeFrameEnd = null |
||
48 | |||
49 | if(params['timeFrameType'] == "day") { |
||
50 | chosenTimeFrameStart = params['dayMainTimeFrameStart']
|
||
51 | chosenTimeFrameEnd = params['dayMainTimeFrameEnd']
|
||
52 | } else if(params['timeFrameType'] == "month") { |
||
53 | chosenTimeFrameStart = params['monthMainTimeFrameStart']
|
||
54 | chosenTimeFrameEnd = params['monthMainTimeFrameEnd']
|
||
55 | } else if(params['timeFrameType'] == "year") { |
||
56 | chosenTimeFrameStart = params['yearMainTimeFrameStart']
|
||
57 | chosenTimeFrameEnd = params['yearMainTimeFrameEnd']
|
||
58 | } else if(params['timeFrameType'] == "custom") { |
||
59 | chosenTimeFrameStart = params['customMainTimeFrameStart']
|
||
60 | chosenTimeFrameEnd = params['customMainTimeFrameEnd']
|
||
61 | } else {
|
||
62 | errors += "<p>Time frame type invalid!</p>"
|
||
63 | } |
||
64 | |||
65 | def newEneGraph = new EneGraph( |
||
66 | name: params['graphName'], |
||
67 | type: params['graphType'], |
||
68 | timeFrameType: params['timeFrameType'], |
||
69 | timeFrameStart: chosenTimeFrameStart,
|
||
70 | timeFrameEnd: chosenTimeFrameEnd,
|
||
71 | innerSections: params['customMainTimeFrameInnerSections'], |
||
72 | advHoursTimeFrameStart: params['advHoursStart'], |
||
73 | advHoursTimeFrameEnd: params['advHoursEnd'], |
||
74 | advDayTimeFrameStart: params['advDayStart'], |
||
75 | advDayTimeFrameEnd: params['advDayEnd'], |
||
76 | advMonthTimeFrameStart: params['advMonthStart'], |
||
77 | advMonthTimeFrameEnd: params['advMonthEnd'], |
||
78 | deviceId: params['deviceId'] |
||
79 | ) |
||
80 | |||
81 | if(!newEneGraph.validate()) {
|
||
82 | errors += "<p>Graph could not be created - there were some input errors. Please check if all input values are valid.</p>"
|
||
83 | flash['errors'] = errors
|
||
84 | redirect(action:'newGraph') |
||
85 | } else {
|
||
86 | if(newEneGraph.save()){
|
||
87 | flash['confirms'] = "<p>Graph created succesfully!</p>" |
||
88 | redirect(action:'newGraph') |
||
89 | } else {
|
||
90 | flash['errors'] = "<p>There was an error while saving graph.</p>" |
||
91 | redirect(action:'newGraph') |
||
92 | } |
||
93 | } |
||
94 | |||
95 | return true |
||
96 | |||
97 | } |
||
98 | |||
99 | def showGraph = {
|
||
100 | |||
101 | 50 | alexbesir | def graphToShow = EneGraph.get(params['id']) |
102 | 43 | alexbesir | String query
|
103 | 42 | alexbesir | |
104 | List data = new ArrayList() |
||
105 | 63 | alexbesir | data = graphDataService.getGraphData(graphToShow) |
106 | 42 | alexbesir | |
107 | [data:data]
|
||
108 | |||
109 | } |
||
110 | 50 | alexbesir | |
111 | def editGraph = {
|
||
112 | |||
113 | EneGraph chosenGraph = EneGraph.get(params['id'])
|
||
114 | |||
115 | List graphTypes = ["min","max","avg","sum"] |
||
116 | List graphTypesFriendly = ["Minimum value","Maximum value","Average value","Cumulative"] |
||
117 | |||
118 | List devices = new ArrayList() |
||
119 | List devicesFriendly = new ArrayList() |
||
120 | |||
121 | DeviceInfo.list().each { |
||
122 | devices << it.deviceId
|
||
123 | devicesFriendly << it.friendlyName
|
||
124 | } |
||
125 | |||
126 | [graph: chosenGraph, graphTypes:graphTypes, graphTypesFriendly:graphTypesFriendly, devices:devices, devicesFriendly:devicesFriendly] |
||
127 | |||
128 | } |
||
129 | |||
130 | def editGraphDo = {
|
||
131 | |||
132 | String errors = "" |
||
133 | |||
134 | EneGraph graphToEdit = EneGraph.get(params['graphId'])
|
||
135 | |||
136 | graphToEdit.name = params['graphName']
|
||
137 | graphToEdit.type = params['graphType']
|
||
138 | graphToEdit.timeFrameType = params['timeFrameType']
|
||
139 | graphToEdit.timeFrameStart = params['mainTimeFrameStart']
|
||
140 | graphToEdit.timeFrameEnd = params['mainTimeFrameEnd']
|
||
141 | graphToEdit.innerSections = params['mainTimeFrameInnerSections'] as int |
||
142 | graphToEdit.advHoursTimeFrameStart = params['advHoursStart'] as int |
||
143 | graphToEdit.advHoursTimeFrameEnd = params['advHoursEnd'] as int |
||
144 | graphToEdit.advDayTimeFrameStart = params['advDayStart'] as int |
||
145 | graphToEdit.advDayTimeFrameEnd = params['advDayEnd'] as int |
||
146 | graphToEdit.advMonthTimeFrameStart = params['advMonthStart'] as int |
||
147 | graphToEdit.advMonthTimeFrameEnd = params['advMonthEnd'] as int |
||
148 | graphToEdit.deviceId = params['deviceId']
|
||
149 | |||
150 | if(!graphToEdit.validate()) {
|
||
151 | errors += "<p>Graph could not be modified - there were some input errors. Please check if all input values are valid.</p>"
|
||
152 | flash['errors'] = errors
|
||
153 | 63 | alexbesir | redirect(action:'editGraph',params: [id: params.graphId]) |
154 | 50 | alexbesir | } else {
|
155 | if(graphToEdit.save()){
|
||
156 | flash['confirms'] = "<p>Graph modified succesfully!</p>" |
||
157 | redirect(action:'graphs') |
||
158 | } else {
|
||
159 | flash['errors'] = "<p>There was an error while modifying graph.</p>" |
||
160 | 63 | alexbesir | redirect(action:'editGraph',params: [id: params.graphId]) |
161 | 50 | alexbesir | } |
162 | } |
||
163 | |||
164 | return true |
||
165 | |||
166 | } |
||
167 | |||
168 | def newReport = {
|
||
169 | |||
170 | def graphsAll = EneGraph.list()
|
||
171 | def graphs = new ArrayList() |
||
172 | def graphsKeys = new ArrayList() |
||
173 | graphsAll.each { |
||
174 | graphs << it.name
|
||
175 | graphsKeys << it.id
|
||
176 | } |
||
177 | |||
178 | [graphs:graphs, graphsKeys:graphsKeys] |
||
179 | |||
180 | } |
||
181 | |||
182 | def makeReport = {
|
||
183 | |||
184 | def graphToShow = EneGraph.get(params['id']) |
||
185 | String query
|
||
186 | |||
187 | List data = new ArrayList() |
||
188 | List detailedData = new ArrayList() |
||
189 | def results
|
||
190 | def innerTimeFrameHalf = 0; |
||
191 | def db_sql = new Sql(dataSource) |
||
192 | |||
193 | if(graphToShow.timeFrameType == "day") { |
||
194 | db_sql.eachRow "select date_trunc('day',ld.date_recieved) dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and date_trunc('day',ld.date_recieved) >= '" + graphToShow.timeFrameStart + "' and date_trunc('day',ld.date_recieved) <= '" + graphToShow.timeFrameEnd + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ") group by date_trunc('day',ld.date_recieved) order by date_trunc('day',ld.date_recieved) asc", { |
||
195 | data << [it.dt.getTime(), it.vl] |
||
196 | detailedData << [it.dt.toString().substring(0,10), it.vl] |
||
197 | } |
||
198 | } else if (graphToShow.timeFrameType == "month") { |
||
199 | db_sql.eachRow "select date_trunc('month',ld.date_recieved) dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and date_trunc('month',ld.date_recieved) >= '" + graphToShow.timeFrameStart + "' and date_trunc('month',ld.date_recieved) <= '" + graphToShow.timeFrameEnd + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ") group by date_trunc('month',ld.date_recieved) order by date_trunc('month',ld.date_recieved) asc", { |
||
200 | data << [it.dt.getTime(), it.vl] |
||
201 | detailedData << [it.dt.toString().substring(0,7), it.vl] |
||
202 | } |
||
203 | } else if (graphToShow.timeFrameType == "year") { |
||
204 | db_sql.eachRow "select date_trunc('year',ld.date_recieved) dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and date_trunc('year',ld.date_recieved) >= '" + graphToShow.timeFrameStart + "' and date_trunc('year',ld.date_recieved) <= '" + graphToShow.timeFrameEnd + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ") group by date_trunc('year',ld.date_recieved) order by date_trunc('year',ld.date_recieved) asc", { |
||
205 | data << [it.dt.getTime(), it.vl] |
||
206 | detailedData << [it.dt.toString().substring(0,4), it.vl] |
||
207 | } |
||
208 | } else if (graphToShow.timeFrameType == "custom") { |
||
209 | def innerTimeFrame = (long)((graphToShow.timeFrameEnd.getTime() - graphToShow.timeFrameStart.getTime()) / graphToShow.innerSections) |
||
210 | innerTimeFrameHalf = (long)(innerTimeFrame/2) |
||
211 | query = ""
|
||
212 | for(int i = 0; i < graphToShow.innerSections;i++) { |
||
213 | def currDateMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame) + innerTimeFrame/2 |
||
214 | def currDate = new Date((long)currDateMillis) |
||
215 | def currDateStartMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame)
|
||
216 | def currDateStart = new Date((long)currDateStartMillis) |
||
217 | def currDateStopMillis = graphToShow.timeFrameStart.getTime() + ((i+1)*innerTimeFrame) |
||
218 | def currDateStop = new Date((long)currDateStopMillis) |
||
219 | query += "(select timestamp without time zone '" + currDate.toTimestamp() + "' dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and ld.date_recieved >= '" + currDateStart.toTimestamp() + "' and ld.date_recieved <= '" + currDateStop.toTimestamp() + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ")) " |
||
220 | if (i < (graphToShow.innerSections-1) ) query += "union " |
||
221 | } |
||
222 | query += "order by dt asc"
|
||
223 | db_sql.eachRow(query) { |
||
224 | data << [it.dt.getTime(), it.vl] |
||
225 | detailedData << [(new Date(it.dt.getTime()-innerTimeFrameHalf)).toTimestamp().toString(),(new Date(it.dt.getTime()+innerTimeFrameHalf)).toTimestamp().toString(), it.vl] |
||
226 | } |
||
227 | } |
||
228 | |||
229 | [data:data, detailedData:detailedData, graph:graphToShow] |
||
230 | |||
231 | } |
||
232 | |||
233 | def exportReport = {
|
||
234 | |||
235 | if(params['exportType'] == 'pdf') { |
||
236 | exportReportPdf(params:params)
|
||
237 | } else if(params['exportType'] == 'xml') { |
||
238 | exportReportXml(params:params)
|
||
239 | } else if(params['exportType'] == 'csv') { |
||
240 | exportReportCsv(params:params)
|
||
241 | } else {
|
||
242 | flash['errors'] = "<p>The specified export type is not valid!</p>" |
||
243 | redirect(action:'graphs') |
||
244 | } |
||
245 | |||
246 | return true |
||
247 | |||
248 | } |
||
249 | |||
250 | def exportReportPdf = {
|
||
251 | |||
252 | redirect(controller: 'pdf', action: 'pdfLink', params:[pdfController: 'pdf',pdfAction:'reportToPdf',reportId:params['reportId']]) |
||
253 | |||
254 | } |
||
255 | |||
256 | def exportReportXml = {
|
||
257 | |||
258 | def cont = "" |
||
259 | |||
260 | def graphToShow = EneGraph.get(params['reportId']) |
||
261 | String query
|
||
262 | |||
263 | List data = new ArrayList() |
||
264 | List detailedData = new ArrayList() |
||
265 | def results
|
||
266 | def innerTimeFrameHalf = 0; |
||
267 | def db_sql = new Sql(dataSource) |
||
268 | |||
269 | if(graphToShow.timeFrameType == "day") { |
||
270 | db_sql.eachRow "select date_trunc('day',ld.date_recieved) dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and date_trunc('day',ld.date_recieved) >= '" + graphToShow.timeFrameStart + "' and date_trunc('day',ld.date_recieved) <= '" + graphToShow.timeFrameEnd + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ") group by date_trunc('day',ld.date_recieved) order by date_trunc('day',ld.date_recieved) asc", { |
||
271 | data << [it.dt.getTime(), it.vl] |
||
272 | detailedData << [it.dt.toString().substring(0,10), it.vl] |
||
273 | } |
||
274 | } else if (graphToShow.timeFrameType == "month") { |
||
275 | db_sql.eachRow "select date_trunc('month',ld.date_recieved) dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and date_trunc('month',ld.date_recieved) >= '" + graphToShow.timeFrameStart + "' and date_trunc('month',ld.date_recieved) <= '" + graphToShow.timeFrameEnd + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ") group by date_trunc('month',ld.date_recieved) order by date_trunc('month',ld.date_recieved) asc", { |
||
276 | data << [it.dt.getTime(), it.vl] |
||
277 | detailedData << [it.dt.toString().substring(0,7), it.vl] |
||
278 | } |
||
279 | } else if (graphToShow.timeFrameType == "year") { |
||
280 | db_sql.eachRow "select date_trunc('year',ld.date_recieved) dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and date_trunc('year',ld.date_recieved) >= '" + graphToShow.timeFrameStart + "' and date_trunc('year',ld.date_recieved) <= '" + graphToShow.timeFrameEnd + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ") group by date_trunc('year',ld.date_recieved) order by date_trunc('year',ld.date_recieved) asc", { |
||
281 | data << [it.dt.getTime(), it.vl] |
||
282 | detailedData << [it.dt.toString().substring(0,4), it.vl] |
||
283 | } |
||
284 | } else if (graphToShow.timeFrameType == "custom") { |
||
285 | def innerTimeFrame = (long)((graphToShow.timeFrameEnd.getTime() - graphToShow.timeFrameStart.getTime()) / graphToShow.innerSections) |
||
286 | innerTimeFrameHalf = (long)(innerTimeFrame/2) |
||
287 | query = ""
|
||
288 | for(int i = 0; i < graphToShow.innerSections;i++) { |
||
289 | def currDateMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame) + innerTimeFrame/2 |
||
290 | def currDate = new Date((long)currDateMillis) |
||
291 | def currDateStartMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame)
|
||
292 | def currDateStart = new Date((long)currDateStartMillis) |
||
293 | def currDateStopMillis = graphToShow.timeFrameStart.getTime() + ((i+1)*innerTimeFrame) |
||
294 | def currDateStop = new Date((long)currDateStopMillis) |
||
295 | query += "(select timestamp without time zone '" + currDate.toTimestamp() + "' dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and ld.date_recieved >= '" + currDateStart.toTimestamp() + "' and ld.date_recieved <= '" + currDateStop.toTimestamp() + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ")) " |
||
296 | if (i < (graphToShow.innerSections-1) ) query += "union " |
||
297 | } |
||
298 | query += "order by dt asc"
|
||
299 | db_sql.eachRow(query) { |
||
300 | data << [it.dt.getTime(), it.vl] |
||
301 | detailedData << [(new Date(it.dt.getTime()-innerTimeFrameHalf)).toTimestamp().toString(),(new Date(it.dt.getTime()+innerTimeFrameHalf)).toTimestamp().toString(), it.vl] |
||
302 | } |
||
303 | } |
||
304 | |||
305 | |||
306 | if(graphToShow.timeFrameType == 'custom') { |
||
307 | cont += "<report>" + "\n" |
||
308 | cont += "\t<desc>" + graphToShow.name + "</desc>" + "\n" |
||
309 | cont += "\t<type>" + graphToShow.type + "</type>" + "\n" |
||
310 | cont += "\t<device>" + graphToShow.deviceId + "</device>" + "\n" |
||
311 | cont += "\t<timeframe>" + "\n" |
||
312 | cont += "\t\t<type>" + graphToShow.timeFrameType + "</type>" + "\n" |
||
313 | cont += "\t\t<start>" + graphToShow.timeFrameStart + "</start>" + "\n" |
||
314 | cont += "\t\t<end>" + graphToShow.timeFrameEnd + "</end>" + "\n" |
||
315 | cont += "\t\t<innernum>" + graphToShow.innerSections + "</innernum>" + "\n" |
||
316 | cont += "\t</timeframe>" + "\n" |
||
317 | cont += "\t<values>" + "\n" |
||
318 | detailedData.each { |
||
319 | cont += "\t\t<inner>" + "\n" |
||
320 | cont += "\t\t\t<start>" + it[0] + "</start>" + "\n" |
||
321 | cont += "\t\t\t<end>" + it[1] + "</end>" + "\n" |
||
322 | cont += "\t\t\t<value>" + it[2] + "</value>" + "\n" |
||
323 | cont += "\t\t</inner>" + "\n" |
||
324 | } |
||
325 | cont += "\t</values>" + "\n" |
||
326 | cont += "</report>" + "\n" |
||
327 | } else {
|
||
328 | cont += "<report>" + "\n" |
||
329 | cont += "\t<desc>" + graphToShow.name + "</desc>" + "\n" |
||
330 | cont += "\t<type>" + graphToShow.type + "</type>" + "\n" |
||
331 | cont += "\t<device>" + graphToShow.deviceId + "</device>" + "\n" |
||
332 | cont += "\t<timeframe>" + "\n" |
||
333 | cont += "\t\t<type>" + graphToShow.timeFrameType + "</type>" + "\n" |
||
334 | cont += "\t\t<start>" + graphToShow.timeFrameStart + "</start>" + "\n" |
||
335 | cont += "\t\t<end>" + graphToShow.timeFrameEnd + "</end>" + "\n" |
||
336 | cont += "\t</timeframe>" + "\n" |
||
337 | cont += "\t<values>" + "\n" |
||
338 | detailedData.each { |
||
339 | cont += "\t\t<inner>" + "\n" |
||
340 | cont += "\t\t\t<time>" + it[0] + "</time>" + "\n" |
||
341 | cont += "\t\t\t<value>" + it[1] + "</value>" + "\n" |
||
342 | cont += "\t\t</inner>" + "\n" |
||
343 | } |
||
344 | cont += "\t</values>" + "\n" |
||
345 | cont += "</report>" + "\n" |
||
346 | } |
||
347 | |||
348 | render(text: cont,contentType:"text/xml",encoding:"UTF-8") |
||
349 | |||
350 | } |
||
351 | |||
352 | def exportReportCsv = {
|
||
353 | |||
354 | def cont = "" |
||
355 | |||
356 | def graphToShow = EneGraph.get(params['reportId']) |
||
357 | String query
|
||
358 | |||
359 | List data = new ArrayList() |
||
360 | List detailedData = new ArrayList() |
||
361 | def results
|
||
362 | def innerTimeFrameHalf = 0; |
||
363 | def db_sql = new Sql(dataSource) |
||
364 | |||
365 | if(graphToShow.timeFrameType == "day") { |
||
366 | db_sql.eachRow "select date_trunc('day',ld.date_recieved) dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and date_trunc('day',ld.date_recieved) >= '" + graphToShow.timeFrameStart + "' and date_trunc('day',ld.date_recieved) <= '" + graphToShow.timeFrameEnd + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ") group by date_trunc('day',ld.date_recieved) order by date_trunc('day',ld.date_recieved) asc", { |
||
367 | data << [it.dt.getTime(), it.vl] |
||
368 | detailedData << [it.dt.toString().substring(0,10), it.vl] |
||
369 | } |
||
370 | } else if (graphToShow.timeFrameType == "month") { |
||
371 | db_sql.eachRow "select date_trunc('month',ld.date_recieved) dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and date_trunc('month',ld.date_recieved) >= '" + graphToShow.timeFrameStart + "' and date_trunc('month',ld.date_recieved) <= '" + graphToShow.timeFrameEnd + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ") group by date_trunc('month',ld.date_recieved) order by date_trunc('month',ld.date_recieved) asc", { |
||
372 | data << [it.dt.getTime(), it.vl] |
||
373 | detailedData << [it.dt.toString().substring(0,7), it.vl] |
||
374 | } |
||
375 | } else if (graphToShow.timeFrameType == "year") { |
||
376 | db_sql.eachRow "select date_trunc('year',ld.date_recieved) dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and date_trunc('year',ld.date_recieved) >= '" + graphToShow.timeFrameStart + "' and date_trunc('year',ld.date_recieved) <= '" + graphToShow.timeFrameEnd + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ") group by date_trunc('year',ld.date_recieved) order by date_trunc('year',ld.date_recieved) asc", { |
||
377 | data << [it.dt.getTime(), it.vl] |
||
378 | detailedData << [it.dt.toString().substring(0,4), it.vl] |
||
379 | } |
||
380 | } else if (graphToShow.timeFrameType == "custom") { |
||
381 | def innerTimeFrame = (long)((graphToShow.timeFrameEnd.getTime() - graphToShow.timeFrameStart.getTime()) / graphToShow.innerSections) |
||
382 | innerTimeFrameHalf = (long)(innerTimeFrame/2) |
||
383 | query = ""
|
||
384 | for(int i = 0; i < graphToShow.innerSections;i++) { |
||
385 | def currDateMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame) + innerTimeFrame/2 |
||
386 | def currDate = new Date((long)currDateMillis) |
||
387 | def currDateStartMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame)
|
||
388 | def currDateStart = new Date((long)currDateStartMillis) |
||
389 | def currDateStopMillis = graphToShow.timeFrameStart.getTime() + ((i+1)*innerTimeFrame) |
||
390 | def currDateStop = new Date((long)currDateStopMillis) |
||
391 | query += "(select timestamp without time zone '" + currDate.toTimestamp() + "' dt, " + graphToShow['type'] + "(to_number(ld.reported_data,'9999999999D99999')) vl from logged_data as ld where (device_id = '" + graphToShow['deviceId'] + "' and ld.date_recieved >= '" + currDateStart.toTimestamp() + "' and ld.date_recieved <= '" + currDateStop.toTimestamp() + "' and date_part('hour',ld.date_recieved) >= " + graphToShow.advHoursTimeFrameStart + " and date_part('hour',ld.date_recieved) <= " + graphToShow.advHoursTimeFrameEnd + " and date_part('day',ld.date_recieved) >= " + graphToShow.advDayTimeFrameStart + " and date_part('day',ld.date_recieved) <= " + graphToShow.advDayTimeFrameEnd + " and date_part('month',ld.date_recieved) >= " + graphToShow.advMonthTimeFrameStart + " and date_part('month',ld.date_recieved) <= " + graphToShow.advMonthTimeFrameEnd + ")) " |
||
392 | if (i < (graphToShow.innerSections-1) ) query += "union " |
||
393 | } |
||
394 | query += "order by dt asc"
|
||
395 | db_sql.eachRow(query) { |
||
396 | data << [it.dt.getTime(), it.vl] |
||
397 | detailedData << [(new Date(it.dt.getTime()-innerTimeFrameHalf)).toTimestamp().toString(),(new Date(it.dt.getTime()+innerTimeFrameHalf)).toTimestamp().toString(), it.vl] |
||
398 | } |
||
399 | } |
||
400 | |||
401 | |||
402 | if(graphToShow.timeFrameType == 'custom') { |
||
403 | detailedData.each { |
||
404 | cont += it[0] + "," + it[1] + "," + it[2] + "\n" |
||
405 | } |
||
406 | } else {
|
||
407 | detailedData.each { |
||
408 | cont += it[0] + "," + it[1] + "\n" |
||
409 | } |
||
410 | } |
||
411 | |||
412 | render(text: cont,contentType:"text/csv",encoding:"UTF-8") |
||
413 | |||
414 | } |
||
415 | 58 | alexbesir | |
416 | def deleteGraph = {
|
||
417 | |||
418 | if(params['id']) { |
||
419 | |||
420 | def graphToDelete = EneGraph.get(params['id']) |
||
421 | if(graphToDelete) {
|
||
422 | |||
423 | 63 | alexbesir | if(graphToDelete.delete()) {
|
424 | flash['confirms'] = "<p>Graph deleted successfully.</p>" |
||
425 | redirect(action:'graphs') |
||
426 | return true |
||
427 | } else {
|
||
428 | flash['errors'] = "<p>Cannot remove this graph. This graph is part of a <b>combined</b> graph.</p><p>You should remove the <b>combined</b> graph first.</p>" |
||
429 | redirect(action:'graphs') |
||
430 | return false |
||
431 | } |
||
432 | |||
433 | } else {
|
||
434 | flash['errors'] = "<p>No graph was deleted. Graph ID invalid!</p>" |
||
435 | redirect(action:'graphs') |
||
436 | return false |
||
437 | } |
||
438 | |||
439 | } else {
|
||
440 | |||
441 | flash['errors'] = "<p>No graph was deleted. Graph ID not specified!</p>" |
||
442 | redirect(action:'graphs') |
||
443 | return false |
||
444 | |||
445 | } |
||
446 | |||
447 | } |
||
448 | |||
449 | def newCombGraph = {
|
||
450 | |||
451 | def graphs = EneGraph.list()
|
||
452 | |||
453 | [graphs:graphs]
|
||
454 | |||
455 | } |
||
456 | |||
457 | def newCombGraphDo = {
|
||
458 | |||
459 | List selgrph = new ArrayList() |
||
460 | selgrph = params.list('graphsToCombine')
|
||
461 | |||
462 | if(selgrph.size() < 2) { |
||
463 | flash.errors = "<p>You have to select at least 2 single device graphs to combine!</p>"
|
||
464 | redirect(action:'newCombGraph') |
||
465 | return false |
||
466 | } |
||
467 | |||
468 | CombGraph newOne = new CombGraph(
|
||
469 | name: params.newCombGraphName
|
||
470 | ) |
||
471 | |||
472 | selgrph.each { |
||
473 | newOne.addToGraphs(EneGraph.get(it))
|
||
474 | } |
||
475 | |||
476 | if(newOne.validate()) {
|
||
477 | newOne.save() |
||
478 | flash.confirms = "<p>New combined graph added succesfully!</p>"
|
||
479 | redirect(action:'graphs') |
||
480 | return true |
||
481 | } else {
|
||
482 | flash.errors = "<p>New graph could not be created. Check if all fields are filled and if values are valid.</p>"
|
||
483 | redirect(action:'newCombGraph') |
||
484 | return false |
||
485 | } |
||
486 | |||
487 | } |
||
488 | |||
489 | def deleteCombGraph = {
|
||
490 | |||
491 | if(params['id']) { |
||
492 | |||
493 | def graphToDelete = CombGraph.get(params['id']) |
||
494 | if(graphToDelete) {
|
||
495 | |||
496 | 58 | alexbesir | graphToDelete.delete() |
497 | flash['confirms'] = "<p>Graph deleted successfully.</p>" |
||
498 | redirect(action:'graphs') |
||
499 | return true |
||
500 | |||
501 | } else {
|
||
502 | flash['errors'] = "<p>No graph was deleted. Graph ID invalid!</p>" |
||
503 | redirect(action:'graphs') |
||
504 | return false |
||
505 | } |
||
506 | |||
507 | } else {
|
||
508 | |||
509 | flash['errors'] = "<p>No graph was deleted. Graph ID not specified!</p>" |
||
510 | redirect(action:'graphs') |
||
511 | return false |
||
512 | |||
513 | 63 | alexbesir | } |
514 | 58 | alexbesir | |
515 | } |
||
516 | 63 | alexbesir | |
517 | def editCombGraph = {
|
||
518 | |||
519 | CombGraph chosenGraph = CombGraph.get(params['id'])
|
||
520 | List allGraphs = new ArrayList() |
||
521 | EneGraph.list().each { |
||
522 | if(chosenGraph.graphs.contains(it)) { |
||
523 | allGraphs << [gr: it, sl: true] |
||
524 | } else {
|
||
525 | allGraphs << [gr: it, sl: false] |
||
526 | } |
||
527 | } |
||
528 | |||
529 | [chosenGraph: chosenGraph, graphs: allGraphs] |
||
530 | |||
531 | } |
||
532 | |||
533 | def editCombGraphDo = {
|
||
534 | |||
535 | CombGraph chosenGraph = CombGraph.get(params['graphId'])
|
||
536 | |||
537 | chosenGraph.name = params['editCombGraphName']
|
||
538 | List newGraphs = new ArrayList() |
||
539 | newGraphs = params.list('graphsToCombine')
|
||
540 | |||
541 | if(newGraphs.size() < 2) { |
||
542 | flash.errors = "<p>You have to select at least 2 single device graphs to combine!</p>"
|
||
543 | redirect(action:'editCombGraph',params:[id: params.graphId]) |
||
544 | return false |
||
545 | } |
||
546 | |||
547 | List graphsToRemove = new ArrayList() |
||
548 | |||
549 | chosenGraph.graphs.each { |
||
550 | graphsToRemove << it
|
||
551 | } |
||
552 | |||
553 | graphsToRemove.each { |
||
554 | chosenGraph.removeFromGraphs(it)
|
||
555 | } |
||
556 | |||
557 | newGraphs.each { |
||
558 | chosenGraph.addToGraphs(EneGraph.get(it))
|
||
559 | } |
||
560 | |||
561 | if(chosenGraph.validate()) {
|
||
562 | if(chosenGraph.save(flush:true)) { |
||
563 | flash.confirms = "<p>Graph edited succesfully!</p>"
|
||
564 | redirect(action:'graphs') |
||
565 | return true |
||
566 | } else {
|
||
567 | flash.errors = "<p>Graph could not be edited. Check if all fields are filled and if values are valid.</p>"
|
||
568 | redirect(action:'editCombGraph', params:[id: params.graphId]) |
||
569 | return false |
||
570 | } |
||
571 | } else {
|
||
572 | flash.errors = "<p>Graph could not be edited. Check if all fields are filled and if values are valid.</p>"
|
||
573 | redirect(action:'editCombGraph', params:[id: params.graphId]) |
||
574 | return false |
||
575 | } |
||
576 | |||
577 | } |
||
578 | |||
579 | def showCombGraph = {
|
||
580 | |||
581 | if(params.id) {
|
||
582 | CombGraph chGrph = CombGraph.get(params.id) |
||
583 | if(chGrph) {
|
||
584 | |||
585 | List datas = new ArrayList() |
||
586 | |||
587 | chGrph.graphs.each { |
||
588 | datas << graphDataService.getGraphData(it)
|
||
589 | } |
||
590 | |||
591 | return [data: datas] |
||
592 | |||
593 | } else {
|
||
594 | flash.errors = "<p>Invalid graph ID!</p>"
|
||
595 | redirect(action:'graphs') |
||
596 | return false |
||
597 | } |
||
598 | } else {
|
||
599 | flash.errors = "<p>Invalid graph ID!</p>"
|
||
600 | redirect(action:'graphs') |
||
601 | return false |
||
602 | } |
||
603 | |||
604 | } |
||
605 | |||
606 | def makeCombReport = {
|
||
607 | |||
608 | |||
609 | |||
610 | } |
||
611 | 3 | alexbesir | |
612 | } |