Revision 50 hci/trunk/eneraptor-web-app/grails-app/controllers/com/eneraptor/hci/StatisticsController.groovy
StatisticsController.groovy | ||
---|---|---|
19 | 19 |
|
20 | 20 |
} |
21 | 21 |
|
22 |
def reports = { } |
|
23 |
|
|
24 | 22 |
def newGraph = { |
25 | 23 |
|
26 | 24 |
List graphTypes = ["min","max","avg","sum"] |
... | ... | |
96 | 94 |
|
97 | 95 |
def showGraph = { |
98 | 96 |
|
99 |
def graphToShow = EneGraph.get(params['graphId'])
|
|
97 |
def graphToShow = EneGraph.get(params['id'])
|
|
100 | 98 |
String query |
101 | 99 |
|
102 | 100 |
List data = new ArrayList() |
... | ... | |
104 | 102 |
def db_sql = new Sql(dataSource) |
105 | 103 |
|
106 | 104 |
if(graphToShow.timeFrameType == "day") { |
107 |
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 + "') group by date_trunc('day',ld.date_recieved) order by date_trunc('day',ld.date_recieved) asc", { |
|
105 |
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", {
|
|
108 | 106 |
data << [it.dt.getTime(), it.vl] |
109 | 107 |
} |
110 | 108 |
} else if (graphToShow.timeFrameType == "month") { |
111 |
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 + "') group by date_trunc('month',ld.date_recieved) order by date_trunc('month',ld.date_recieved) asc", { |
|
109 |
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", {
|
|
112 | 110 |
data << [it.dt.getTime(), it.vl] |
113 | 111 |
} |
114 | 112 |
} else if (graphToShow.timeFrameType == "year") { |
115 |
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 + "') group by date_trunc('year',ld.date_recieved) order by date_trunc('year',ld.date_recieved) asc", { |
|
113 |
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", {
|
|
116 | 114 |
data << [it.dt.getTime(), it.vl] |
117 | 115 |
} |
118 | 116 |
} else if (graphToShow.timeFrameType == "custom") { |
... | ... | |
125 | 123 |
def currDateStart = new Date((long)currDateStartMillis) |
126 | 124 |
def currDateStopMillis = graphToShow.timeFrameStart.getTime() + ((i+1)*innerTimeFrame) |
127 | 125 |
def currDateStop = new Date((long)currDateStopMillis) |
128 |
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() + "')) " |
|
126 |
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 + ")) "
|
|
129 | 127 |
if (i < (graphToShow.innerSections-1) ) query += "union " |
130 | 128 |
} |
131 | 129 |
query += "order by dt asc" |
... | ... | |
137 | 135 |
[data:data] |
138 | 136 |
|
139 | 137 |
} |
138 |
|
|
139 |
def editGraph = { |
|
140 |
|
|
141 |
EneGraph chosenGraph = EneGraph.get(params['id']) |
|
142 |
|
|
143 |
List graphTypes = ["min","max","avg","sum"] |
|
144 |
List graphTypesFriendly = ["Minimum value","Maximum value","Average value","Cumulative"] |
|
145 |
|
|
146 |
List devices = new ArrayList() |
|
147 |
List devicesFriendly = new ArrayList() |
|
148 |
|
|
149 |
DeviceInfo.list().each { |
|
150 |
devices << it.deviceId |
|
151 |
devicesFriendly << it.friendlyName |
|
152 |
} |
|
153 |
|
|
154 |
[graph: chosenGraph, graphTypes:graphTypes, graphTypesFriendly:graphTypesFriendly, devices:devices, devicesFriendly:devicesFriendly] |
|
155 |
|
|
156 |
} |
|
157 |
|
|
158 |
def editGraphDo = { |
|
159 |
|
|
160 |
String errors = "" |
|
161 |
|
|
162 |
EneGraph graphToEdit = EneGraph.get(params['graphId']) |
|
163 |
|
|
164 |
graphToEdit.name = params['graphName'] |
|
165 |
graphToEdit.type = params['graphType'] |
|
166 |
graphToEdit.timeFrameType = params['timeFrameType'] |
|
167 |
graphToEdit.timeFrameStart = params['mainTimeFrameStart'] |
|
168 |
graphToEdit.timeFrameEnd = params['mainTimeFrameEnd'] |
|
169 |
graphToEdit.innerSections = params['mainTimeFrameInnerSections'] as int |
|
170 |
graphToEdit.advHoursTimeFrameStart = params['advHoursStart'] as int |
|
171 |
graphToEdit.advHoursTimeFrameEnd = params['advHoursEnd'] as int |
|
172 |
graphToEdit.advDayTimeFrameStart = params['advDayStart'] as int |
|
173 |
graphToEdit.advDayTimeFrameEnd = params['advDayEnd'] as int |
|
174 |
graphToEdit.advMonthTimeFrameStart = params['advMonthStart'] as int |
|
175 |
graphToEdit.advMonthTimeFrameEnd = params['advMonthEnd'] as int |
|
176 |
graphToEdit.deviceId = params['deviceId'] |
|
177 |
|
|
178 |
if(!graphToEdit.validate()) { |
|
179 |
errors += "<p>Graph could not be modified - there were some input errors. Please check if all input values are valid.</p>" |
|
180 |
flash['errors'] = errors |
|
181 |
redirect(action:'editGraph') |
|
182 |
} else { |
|
183 |
if(graphToEdit.save()){ |
|
184 |
flash['confirms'] = "<p>Graph modified succesfully!</p>" |
|
185 |
redirect(action:'graphs') |
|
186 |
} else { |
|
187 |
flash['errors'] = "<p>There was an error while modifying graph.</p>" |
|
188 |
redirect(action:'editGraph') |
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 |
return true |
|
193 |
|
|
194 |
} |
|
195 |
|
|
196 |
def newReport = { |
|
197 |
|
|
198 |
def graphsAll = EneGraph.list() |
|
199 |
def graphs = new ArrayList() |
|
200 |
def graphsKeys = new ArrayList() |
|
201 |
graphsAll.each { |
|
202 |
graphs << it.name |
|
203 |
graphsKeys << it.id |
|
204 |
} |
|
205 |
|
|
206 |
[graphs:graphs, graphsKeys:graphsKeys] |
|
207 |
|
|
208 |
} |
|
209 |
|
|
210 |
def makeReport = { |
|
211 |
|
|
212 |
def graphToShow = EneGraph.get(params['id']) |
|
213 |
String query |
|
214 |
|
|
215 |
List data = new ArrayList() |
|
216 |
List detailedData = new ArrayList() |
|
217 |
def results |
|
218 |
def innerTimeFrameHalf = 0; |
|
219 |
def db_sql = new Sql(dataSource) |
|
220 |
|
|
221 |
if(graphToShow.timeFrameType == "day") { |
|
222 |
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", { |
|
223 |
data << [it.dt.getTime(), it.vl] |
|
224 |
detailedData << [it.dt.toString().substring(0,10), it.vl] |
|
225 |
} |
|
226 |
} else if (graphToShow.timeFrameType == "month") { |
|
227 |
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", { |
|
228 |
data << [it.dt.getTime(), it.vl] |
|
229 |
detailedData << [it.dt.toString().substring(0,7), it.vl] |
|
230 |
} |
|
231 |
} else if (graphToShow.timeFrameType == "year") { |
|
232 |
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", { |
|
233 |
data << [it.dt.getTime(), it.vl] |
|
234 |
detailedData << [it.dt.toString().substring(0,4), it.vl] |
|
235 |
} |
|
236 |
} else if (graphToShow.timeFrameType == "custom") { |
|
237 |
def innerTimeFrame = (long)((graphToShow.timeFrameEnd.getTime() - graphToShow.timeFrameStart.getTime()) / graphToShow.innerSections) |
|
238 |
innerTimeFrameHalf = (long)(innerTimeFrame/2) |
|
239 |
query = "" |
|
240 |
for(int i = 0; i < graphToShow.innerSections;i++) { |
|
241 |
def currDateMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame) + innerTimeFrame/2 |
|
242 |
def currDate = new Date((long)currDateMillis) |
|
243 |
def currDateStartMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame) |
|
244 |
def currDateStart = new Date((long)currDateStartMillis) |
|
245 |
def currDateStopMillis = graphToShow.timeFrameStart.getTime() + ((i+1)*innerTimeFrame) |
|
246 |
def currDateStop = new Date((long)currDateStopMillis) |
|
247 |
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 + ")) " |
|
248 |
if (i < (graphToShow.innerSections-1) ) query += "union " |
|
249 |
} |
|
250 |
query += "order by dt asc" |
|
251 |
db_sql.eachRow(query) { |
|
252 |
data << [it.dt.getTime(), it.vl] |
|
253 |
detailedData << [(new Date(it.dt.getTime()-innerTimeFrameHalf)).toTimestamp().toString(),(new Date(it.dt.getTime()+innerTimeFrameHalf)).toTimestamp().toString(), it.vl] |
|
254 |
} |
|
255 |
} |
|
256 |
|
|
257 |
[data:data, detailedData:detailedData, graph:graphToShow] |
|
258 |
|
|
259 |
} |
|
260 |
|
|
261 |
def exportReport = { |
|
262 |
|
|
263 |
if(params['exportType'] == 'pdf') { |
|
264 |
exportReportPdf(params:params) |
|
265 |
} else if(params['exportType'] == 'xml') { |
|
266 |
exportReportXml(params:params) |
|
267 |
} else if(params['exportType'] == 'csv') { |
|
268 |
exportReportCsv(params:params) |
|
269 |
} else { |
|
270 |
flash['errors'] = "<p>The specified export type is not valid!</p>" |
|
271 |
redirect(action:'graphs') |
|
272 |
} |
|
273 |
|
|
274 |
return true |
|
275 |
|
|
276 |
} |
|
277 |
|
|
278 |
def exportReportPdf = { |
|
279 |
|
|
280 |
redirect(controller: 'pdf', action: 'pdfLink', params:[pdfController: 'pdf',pdfAction:'reportToPdf',reportId:params['reportId']]) |
|
281 |
|
|
282 |
} |
|
283 |
|
|
284 |
def exportReportXml = { |
|
285 |
|
|
286 |
def cont = "" |
|
287 |
|
|
288 |
def graphToShow = EneGraph.get(params['reportId']) |
|
289 |
String query |
|
290 |
|
|
291 |
List data = new ArrayList() |
|
292 |
List detailedData = new ArrayList() |
|
293 |
def results |
|
294 |
def innerTimeFrameHalf = 0; |
|
295 |
def db_sql = new Sql(dataSource) |
|
296 |
|
|
297 |
if(graphToShow.timeFrameType == "day") { |
|
298 |
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", { |
|
299 |
data << [it.dt.getTime(), it.vl] |
|
300 |
detailedData << [it.dt.toString().substring(0,10), it.vl] |
|
301 |
} |
|
302 |
} else if (graphToShow.timeFrameType == "month") { |
|
303 |
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", { |
|
304 |
data << [it.dt.getTime(), it.vl] |
|
305 |
detailedData << [it.dt.toString().substring(0,7), it.vl] |
|
306 |
} |
|
307 |
} else if (graphToShow.timeFrameType == "year") { |
|
308 |
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", { |
|
309 |
data << [it.dt.getTime(), it.vl] |
|
310 |
detailedData << [it.dt.toString().substring(0,4), it.vl] |
|
311 |
} |
|
312 |
} else if (graphToShow.timeFrameType == "custom") { |
|
313 |
def innerTimeFrame = (long)((graphToShow.timeFrameEnd.getTime() - graphToShow.timeFrameStart.getTime()) / graphToShow.innerSections) |
|
314 |
innerTimeFrameHalf = (long)(innerTimeFrame/2) |
|
315 |
query = "" |
|
316 |
for(int i = 0; i < graphToShow.innerSections;i++) { |
|
317 |
def currDateMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame) + innerTimeFrame/2 |
|
318 |
def currDate = new Date((long)currDateMillis) |
|
319 |
def currDateStartMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame) |
|
320 |
def currDateStart = new Date((long)currDateStartMillis) |
|
321 |
def currDateStopMillis = graphToShow.timeFrameStart.getTime() + ((i+1)*innerTimeFrame) |
|
322 |
def currDateStop = new Date((long)currDateStopMillis) |
|
323 |
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 + ")) " |
|
324 |
if (i < (graphToShow.innerSections-1) ) query += "union " |
|
325 |
} |
|
326 |
query += "order by dt asc" |
|
327 |
db_sql.eachRow(query) { |
|
328 |
data << [it.dt.getTime(), it.vl] |
|
329 |
detailedData << [(new Date(it.dt.getTime()-innerTimeFrameHalf)).toTimestamp().toString(),(new Date(it.dt.getTime()+innerTimeFrameHalf)).toTimestamp().toString(), it.vl] |
|
330 |
} |
|
331 |
} |
|
332 |
|
|
333 |
|
|
334 |
if(graphToShow.timeFrameType == 'custom') { |
|
335 |
cont += "<report>" + "\n" |
|
336 |
cont += "\t<desc>" + graphToShow.name + "</desc>" + "\n" |
|
337 |
cont += "\t<type>" + graphToShow.type + "</type>" + "\n" |
|
338 |
cont += "\t<device>" + graphToShow.deviceId + "</device>" + "\n" |
|
339 |
cont += "\t<timeframe>" + "\n" |
|
340 |
cont += "\t\t<type>" + graphToShow.timeFrameType + "</type>" + "\n" |
|
341 |
cont += "\t\t<start>" + graphToShow.timeFrameStart + "</start>" + "\n" |
|
342 |
cont += "\t\t<end>" + graphToShow.timeFrameEnd + "</end>" + "\n" |
|
343 |
cont += "\t\t<innernum>" + graphToShow.innerSections + "</innernum>" + "\n" |
|
344 |
cont += "\t</timeframe>" + "\n" |
|
345 |
cont += "\t<values>" + "\n" |
|
346 |
detailedData.each { |
|
347 |
cont += "\t\t<inner>" + "\n" |
|
348 |
cont += "\t\t\t<start>" + it[0] + "</start>" + "\n" |
|
349 |
cont += "\t\t\t<end>" + it[1] + "</end>" + "\n" |
|
350 |
cont += "\t\t\t<value>" + it[2] + "</value>" + "\n" |
|
351 |
cont += "\t\t</inner>" + "\n" |
|
352 |
} |
|
353 |
cont += "\t</values>" + "\n" |
|
354 |
cont += "</report>" + "\n" |
|
355 |
} else { |
|
356 |
cont += "<report>" + "\n" |
|
357 |
cont += "\t<desc>" + graphToShow.name + "</desc>" + "\n" |
|
358 |
cont += "\t<type>" + graphToShow.type + "</type>" + "\n" |
|
359 |
cont += "\t<device>" + graphToShow.deviceId + "</device>" + "\n" |
|
360 |
cont += "\t<timeframe>" + "\n" |
|
361 |
cont += "\t\t<type>" + graphToShow.timeFrameType + "</type>" + "\n" |
|
362 |
cont += "\t\t<start>" + graphToShow.timeFrameStart + "</start>" + "\n" |
|
363 |
cont += "\t\t<end>" + graphToShow.timeFrameEnd + "</end>" + "\n" |
|
364 |
cont += "\t</timeframe>" + "\n" |
|
365 |
cont += "\t<values>" + "\n" |
|
366 |
detailedData.each { |
|
367 |
cont += "\t\t<inner>" + "\n" |
|
368 |
cont += "\t\t\t<time>" + it[0] + "</time>" + "\n" |
|
369 |
cont += "\t\t\t<value>" + it[1] + "</value>" + "\n" |
|
370 |
cont += "\t\t</inner>" + "\n" |
|
371 |
} |
|
372 |
cont += "\t</values>" + "\n" |
|
373 |
cont += "</report>" + "\n" |
|
374 |
} |
|
375 |
|
|
376 |
render(text: cont,contentType:"text/xml",encoding:"UTF-8") |
|
377 |
|
|
378 |
} |
|
379 |
|
|
380 |
def exportReportCsv = { |
|
381 |
|
|
382 |
def cont = "" |
|
383 |
|
|
384 |
def graphToShow = EneGraph.get(params['reportId']) |
|
385 |
String query |
|
386 |
|
|
387 |
List data = new ArrayList() |
|
388 |
List detailedData = new ArrayList() |
|
389 |
def results |
|
390 |
def innerTimeFrameHalf = 0; |
|
391 |
def db_sql = new Sql(dataSource) |
|
392 |
|
|
393 |
if(graphToShow.timeFrameType == "day") { |
|
394 |
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", { |
|
395 |
data << [it.dt.getTime(), it.vl] |
|
396 |
detailedData << [it.dt.toString().substring(0,10), it.vl] |
|
397 |
} |
|
398 |
} else if (graphToShow.timeFrameType == "month") { |
|
399 |
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", { |
|
400 |
data << [it.dt.getTime(), it.vl] |
|
401 |
detailedData << [it.dt.toString().substring(0,7), it.vl] |
|
402 |
} |
|
403 |
} else if (graphToShow.timeFrameType == "year") { |
|
404 |
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", { |
|
405 |
data << [it.dt.getTime(), it.vl] |
|
406 |
detailedData << [it.dt.toString().substring(0,4), it.vl] |
|
407 |
} |
|
408 |
} else if (graphToShow.timeFrameType == "custom") { |
|
409 |
def innerTimeFrame = (long)((graphToShow.timeFrameEnd.getTime() - graphToShow.timeFrameStart.getTime()) / graphToShow.innerSections) |
|
410 |
innerTimeFrameHalf = (long)(innerTimeFrame/2) |
|
411 |
query = "" |
|
412 |
for(int i = 0; i < graphToShow.innerSections;i++) { |
|
413 |
def currDateMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame) + innerTimeFrame/2 |
|
414 |
def currDate = new Date((long)currDateMillis) |
|
415 |
def currDateStartMillis = graphToShow.timeFrameStart.getTime() + (i*innerTimeFrame) |
|
416 |
def currDateStart = new Date((long)currDateStartMillis) |
|
417 |
def currDateStopMillis = graphToShow.timeFrameStart.getTime() + ((i+1)*innerTimeFrame) |
|
418 |
def currDateStop = new Date((long)currDateStopMillis) |
|
419 |
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 + ")) " |
|
420 |
if (i < (graphToShow.innerSections-1) ) query += "union " |
|
421 |
} |
|
422 |
query += "order by dt asc" |
|
423 |
db_sql.eachRow(query) { |
|
424 |
data << [it.dt.getTime(), it.vl] |
|
425 |
detailedData << [(new Date(it.dt.getTime()-innerTimeFrameHalf)).toTimestamp().toString(),(new Date(it.dt.getTime()+innerTimeFrameHalf)).toTimestamp().toString(), it.vl] |
|
426 |
} |
|
427 |
} |
|
428 |
|
|
429 |
|
|
430 |
if(graphToShow.timeFrameType == 'custom') { |
|
431 |
detailedData.each { |
|
432 |
cont += it[0] + "," + it[1] + "," + it[2] + "\n" |
|
433 |
} |
|
434 |
} else { |
|
435 |
detailedData.each { |
|
436 |
cont += it[0] + "," + it[1] + "\n" |
|
437 |
} |
|
438 |
} |
|
439 |
|
|
440 |
render(text: cont,contentType:"text/csv",encoding:"UTF-8") |
|
441 |
|
|
442 |
} |
|
140 | 443 |
|
141 | 444 |
} |
Also available in: Unified diff