diff --git a/app/lib/dataServer.js b/app/lib/dataServer.js index 245054d..ccf08e2 100644 --- a/app/lib/dataServer.js +++ b/app/lib/dataServer.js @@ -19,10 +19,32 @@ dataServer.prototype = { self.getReports( res, req.params.site ) } ); - this.app.get( '/sites/:site/reports', function( req, res ) { - res.writeHead( 200 ); + this.app.get( path + '/monitors/:gsmid/clear', function( req, res ) { + console.log( "Deleting all reports for site " + req.params.monitors + "." ); + var monitorid = null; + self.db.core.query( { + text: "SELECT ID FROM monitors WHERE GSMID=$1", + values: [req.params.gsmid] + }) + .on( 'row', function( row ) { + monitorid = row.id; + } ) + .on( 'end', function() { + if ( monitorid === null) + res.send( 404, "Monitor not found." ); - res.end(); + self.db.core.query( { + text: "DELETE FROM aggregate_reports r USING monitors m WHERE r.MonitorID=m.ID and m.GSMID=$1", + values: [req.params.gsmid] }) + .on( 'error', function( err ) { + console.log( err ); + res.send( 500, "Something bad happened." ); + }) + .on( 'end', function() { + serverEmitter.emit( 'monitor/' + monitorid + '/clear' ); + res.send( 200, "All data deleted for monitor ID " + monitorid + " (GSMID " + req.params.gsmid + ")." ); + }) + } ); }) startSocketIO( io ); @@ -102,12 +124,12 @@ dataServer.prototype = { }, getReports: function(res, siteID) { var reports = {}; - var counts = {}; this.db.core.query( "SELECT aggregate_reports.timestamp, aggregate_reports.eventcount, aggregate_reports.monitorid FROM aggregate_reports INNER JOIN monitors ON monitors.id = aggregate_reports.monitorid WHERE monitors.siteid = " + siteID ) .on( 'row', function(row) { row.date = row.timestamp; - if (!reports[row.date]) { reports[row.date] = {}; counts[row.date] = 0; }; - reports[row.date][counts[row.date]++] = row.eventcount; + if (!reports[row.date]) + reports[row.date] = {}; + reports[row.date][row.monitorid] = row.eventcount; }) .on( 'end', function() { var reportArray = []; @@ -141,6 +163,9 @@ function startSocketIO( io ) { monitors = []; monitors.push( monitorid ); serverEmitter.on('monitor/' + monitorid + '/newReport', reportCallback ); + serverEmitter.on('monitor/' + monitorid + '/clear', function() { + socket.emit( 'clear', monitorid ); + } ); socket.set( 'monitors', monitors, function() { console.log( "Socket is watching monitor " + monitorid ); }) diff --git a/app/resources/css/datapage.styl b/app/resources/css/datapage.css similarity index 100% rename from app/resources/css/datapage.styl rename to app/resources/css/datapage.css diff --git a/app/resources/html/templates/datapage.html b/app/resources/html/templates/datapage.html index d13f5c7..e3e7b9e 100644 --- a/app/resources/html/templates/datapage.html +++ b/app/resources/html/templates/datapage.html @@ -22,19 +22,9 @@

{{name}}

- {{health}}/100
- Water Point Health Rating + {{averageVolume}} °C
+ Average Temperature
-
- Average Water Consumption: - {{averageVolume}} - L/Day
- - Served Population: - {{population}} - people
- - {{volumePerCapita}} L / Person / Day
\ No newline at end of file diff --git a/app/resources/js/app/datapage.js b/app/resources/js/app/datapage.js index 047dc86..0da4d16 100644 --- a/app/resources/js/app/datapage.js +++ b/app/resources/js/app/datapage.js @@ -103,12 +103,26 @@ WD.dataPage.loadDataForVisualization = function( siteID, siteData, callback ) { return; } + WD.dataPage.IDToIndexMap = {}; + for ( var index = 0; index < siteData.monitors.length; ++index ) + { + WD.dataPage.IDToIndexMap[siteData.monitors[index].id] = index; + } + + WD.dataPage.data = []; data.forEach(function(d) { - d.date = new Date(d.date); + var o = {}; + for ( var id in WD.dataPage.IDToIndexMap ) { + var index = WD.dataPage.IDToIndexMap[id]; + if ( d[id] ) + o[index] = d[id]; + else + o[index] = undefined; + } + o.date = new Date(d.date); + WD.dataPage.data.push( o ); }); - WD.dataPage.data = data; - if ( WD.dataPage.socket ) { WD.dataPage.socket.emit( 'clear monitors' ); } else { @@ -120,20 +134,27 @@ WD.dataPage.loadDataForVisualization = function( siteID, siteData, callback ) { WD.dataPage.socket.on('newReport', function (newData) { newData.date = new Date( newData.date ); + var index = WD.dataPage.IDToIndexMap[newData.monitor]; + if ( !index && index !== 0 ) + return; + var i; for ( i = 0; i < WD.dataPage.data.length; ++i ) { if ( WD.dataPage.data[i].date == newData.date ) { - WD.dataPage.data[i][newData.monitor] = newData.eventCount; + WD.dataPage.data[i][index] = newData.eventCount; break; } } if ( i == WD.dataPage.data.length ) { var obj = { date: newData.date }; - obj[newData.monitor] = newData.eventCount; + obj[index] = newData.eventCount; WD.dataPage.data.push( obj ); } WD.dataPage.drawVisualization(); }); + WD.dataPage.socket.on('clear', function(monitorid) { + WD.dataPage.render( siteID ); + }) callback( WD.dataPage.data ); }); @@ -218,7 +239,8 @@ function extendSiteData( siteData, monitorData ) { for (d in dates) { var sum = 0; for ( v in dates[d] ) { - sum += dates[d][v]; + if ( !isNaN( dates[d][v] ) ) + sum += dates[d][v]; } dates[d] = sum / dates[d].length; siteData.runningAverage += dates[d]; @@ -228,7 +250,7 @@ function extendSiteData( siteData, monitorData ) { siteData.runningAverage = siteData.runningAverage.toFixed(2); //siteData.volumePerCapita = siteData.runningAverage * 1.5; // assumes //(Math.random() * 6 + 17).toFixed(2); - siteData.averageVolume = (siteData.runningAverage * 1.5).toFixed(2); //Math.ceil( siteData.volumePerCapita * siteData.population ); + siteData.averageVolume = (siteData.runningAverage * 1).toFixed(2); //Math.ceil( siteData.volumePerCapita * siteData.population ); if ( !siteData.population ) { siteData.population = Math.ceil( Math.random() * 15 ); } @@ -267,4 +289,4 @@ WD.dataPage.render = function( siteID ) { }, function() { alert( "Site not found!" ); } ); -}; \ No newline at end of file +}; diff --git a/app/resources/js/app/routes.js b/app/resources/js/app/routes.js index 7537afc..201a27f 100644 --- a/app/resources/js/app/routes.js +++ b/app/resources/js/app/routes.js @@ -45,9 +45,8 @@ WD.routes = { $(document).ready( function() { WD.router = new Router(WD.routes); - WD.router.init(); loadMap(); WD.data.sites.load( function() { - WD.router.setRoute( "/overview" ); + WD.router.init( '/overview' ); } ); } ); \ No newline at end of file