Skip to content

Commit

Permalink
Fix live updating when monitor IDs don't begin at 0. Add 'clear' func…
Browse files Browse the repository at this point in the history
…tionality for demo purposes. Clean up some nonesense.
  • Loading branch information
amcgee committed Mar 24, 2014
1 parent f80d65f commit 8d5482e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 28 deletions.
37 changes: 31 additions & 6 deletions app/lib/dataServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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 );
})
Expand Down
File renamed without changes.
14 changes: 2 additions & 12 deletions app/resources/html/templates/datapage.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@
<div id="datapage_stats">
<h1 class="datapage-title">{{name}}</h1>
<div class="stats-health">
<span class="stats-health-num">{{health}}</span><span class="stats-health-denom">/100</span><br/>
Water Point Health Rating
<span class="stats-health-num">{{averageVolume}} &deg;C</span><span class="stats-health-denom"></span><br/>
Average Temperature
</div>
<div class="stats">
<span class="stats-caption">Average Water Consumption: </span>
<span class="stats-value">{{averageVolume}}</span>
<span class="stats-caption"> L/Day</span><br/>

<span class="stats-caption">Served Population: </span>
<span class="stats-value">{{population}}</span>
<span class="stats-caption">people</span><br/>

<span class="stats-big-value">{{volumePerCapita}}</span><span class="stats-caption"> L / Person / Day</span>
</div>
</div>
</div>
38 changes: 30 additions & 8 deletions app/resources/js/app/datapage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 );
});
Expand Down Expand Up @@ -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];
Expand All @@ -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 );
}
Expand Down Expand Up @@ -267,4 +289,4 @@ WD.dataPage.render = function( siteID ) {
}, function() {
alert( "Site not found!" );
} );
};
};
3 changes: 1 addition & 2 deletions app/resources/js/app/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
} );
} );

0 comments on commit 8d5482e

Please sign in to comment.