Index: StatisticsController.groovy
===================================================================
--- StatisticsController.groovy	(revision 41)
+++ StatisticsController.groovy	(revision 42)
@@ -1,7 +1,123 @@
+
 package com.eneraptor.hci
 
+import groovy.sql.Sql
+
 class StatisticsController {
-
+
+	def dataSource
+	
     def main = { }
+	
+	def graphs = {
+	
+		List savedGraphs = new ArrayList()
+		
+		savedGraphs = EneGraph.list()
+			
+		[savedGraphs: savedGraphs]
+			
+	}
+	
+	def reports = { }
+	
+	def newGraph = {
+		
+		List graphTypes = ["min","max","avg","sum"]
+		List graphTypesFriendly = ["Minimum value","Maximum value","Average value","Cumulative"]
+		
+		List devices = new ArrayList()
+		List devicesFriendly = new ArrayList()
+		
+		DeviceInfo.list().each {
+			devices << it.deviceId
+			devicesFriendly << it.friendlyName
+		}
+		
+		[graphTypes:graphTypes,graphTypesFriendly:graphTypesFriendly, devices:devices, devicesFriendly:devicesFriendly]
+		
+	}
+	
+	def newGraphDo = {
+		
+		String errors = ""
+		Date chosenTimeFrameStart = null
+		Date chosenTimeFrameEnd = null
+		
+		if(params['timeFrameType'] == "day") {
+			chosenTimeFrameStart = params['dayMainTimeFrameStart']
+			chosenTimeFrameEnd = params['dayMainTimeFrameEnd']
+		} else if(params['timeFrameType'] == "month") {
+			chosenTimeFrameStart = params['monthMainTimeFrameStart']
+			chosenTimeFrameEnd = params['monthMainTimeFrameEnd']
+		} else if(params['timeFrameType'] == "year") {
+			chosenTimeFrameStart = params['yearMainTimeFrameStart']
+			chosenTimeFrameEnd = params['yearMainTimeFrameEnd']
+		} else if(params['timeFrameType'] == "custom") {
+			chosenTimeFrameStart = params['customMainTimeFrameStart']
+			chosenTimeFrameEnd = params['customMainTimeFrameEnd']
+		} else {
+			errors += "<p>Time frame type invalid!</p>"
+		}
+		
+		def newEneGraph = new EneGraph(
+			name: params['graphName'],
+			type: params['graphType'],
+			timeFrameType: params['timeFrameType'],
+			timeFrameStart: chosenTimeFrameStart,
+			timeFrameEnd: chosenTimeFrameEnd,
+			innerSections: params['customMainTimeFrameInnerSections'],
+			advHoursTimeFrameStart: params['advHoursStart'],
+			advHoursTimeFrameEnd: params['advHoursEnd'],
+			advDayTimeFrameStart: params['advDayStart'],
+			advDayTimeFrameEnd: params['advDayEnd'],
+			advMonthTimeFrameStart: params['advMonthStart'],
+			advMonthTimeFrameEnd: params['advMonthEnd'],
+			deviceId: params['deviceId']
+		)
+		
+		if(!newEneGraph.validate()) {
+			errors += "<p>Graph could not be created - there were some input errors. Please check if all input values are valid.</p>"
+			flash['errors'] = errors
+			redirect(action:'newGraph')
+		} else {
+			if(newEneGraph.save()){
+				flash['confirms'] = "<p>Graph created succesfully!</p>"
+				redirect(action:'newGraph')
+			} else {
+				flash['errors'] = "<p>There was an error while saving graph.</p>"
+				redirect(action:'newGraph')
+			}
+		}
+		
+		return true
+		
+	}
+	
+	def showGraph = {
+		
+		def graphToShow = EneGraph.get(params['graphId'])
+		
+		List data = new ArrayList()
+		def results
+		def db_sql = new Sql(dataSource)
+		
+		if(graphToShow.timeFrameType == "day") {
+			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 (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", {
+				data << [it.dt.getTime(), it.vl]
+			}
+		} else if (graphToShow.timeFrameType == "month") {
+			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 (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", {
+				data << [it.dt.getTime(), it.vl]
+			}
+		} else if (graphToShow.timeFrameType == "year") {
+			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 (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", {
+				data << [it.dt.getTime(), it.vl]
+			}
+		}
+		
+		[data:data]
+		
+	}
 	
 }
