diff --git a/server.js b/server.js index aa3d8dc..3227e56 100644 --- a/server.js +++ b/server.js @@ -36,45 +36,6 @@ var db = new sqlite3.Database(file); var pg = require("pg"); var conString = "postgres://alertmon:alertmon@db/alertmon"; -pg.connect(conString, function(err, client, done) { - if(err) { - return console.error('error fetching client from pool', err); - } - - var devIndexQry = - "select al.coreId, max(co.coreName) as coreName, max(co.locationDesc) as locationDesc, mpub.MaxPub, al.status as MaxStatus " + - "from Alerts al " + - "inner join (select coreId, max(published_at) as MaxPub from Alerts group by coreId) mpub on al.coreId = mpub.coreId and al.published_at = mpub.MaxPub " + - "left join Cores co on al.coreId = co.coreId " + - "group by al.coreId, mpub.MaxPub, al.status "; - client.query(devIndexQry, function(err, result) { - //call `done()` to release the client back to the pool - done(); - - if(err) { - return console.error('error running query', err); - } - console.log(result.rows[0].corename); - }); -}); - -db.serialize(function() { - if(!exists) { - db.run( - "CREATE TABLE Alerts (" + - "origJSON TEXT," + - "coreId TEXT," + - "status TEXT," + - "published_at TEXT)" - ); - db.run( - "CREATE TABLE Cores (" + - "coreId TEXT PRIMARY KEY," + - "coreName TEXT," + - "locationDesc TEXT)" - ); - } -}); app.use(bodyParser.json()); // Needed for JSON POST requests from Particle Cores. app.use(bodyParser.urlencoded({ extended: false })); // Needed for web POST requests from edit form. @@ -89,36 +50,44 @@ app.set('views', __dirname + '/views'); app.get('/', function(req, res){ var d = new Date(); console.log("GET /, " + JSON.stringify(d, 4)); - var devIndexQry = - "select al.coreId, max(co.coreName) as coreName, max(co.locationDesc) as locationDesc, mpub.MaxPub, al.status as MaxStatus " + - "from Alerts al " + - "inner join (select coreId, max(published_at) as MaxPub from Alerts group by coreId) mpub on al.coreId = mpub.coreId and al.published_at = mpub.MaxPub " + - "left join Cores co on al.coreId = co.coreId " + - "group by al.coreId "; - db.all(devIndexQry, function(err, rows){ - if(err !== null) { - console.log(err); - } else { - //console.log(rows); + + pg.connect(conString, function(err, client, done) { + if(err) { + return console.error('error fetching client from pool', err); + } + var devIndexQry = + "select al.coreid, max(co.corename) as corename, max(co.locationdesc) as locationdesc, mpub.maxpub, al.status as maxstatus " + + "from alerts al " + + "inner join (select coreid, max(published_at) as maxpub from alerts group by coreid) mpub on al.coreid = mpub.coreid and al.published_at = mpub.maxpub " + + "left join cores co on al.coreid = co.coreid " + + "group by al.coreid, mpub.maxpub, al.status " + + "order by mpub.maxpub desc, corename asc "; + client.query(devIndexQry, function(err, result) { + //call `done()` to release the client back to the pool + done(); + + if(err) { + return console.error('error running query', err); + } // Loop over elements in rows array, convert ugly UTC times to pretty local times. - rows.forEach(function(row){ - row.MaxPubDate = alertmonUtils.getLocDateFromUTC(row.MaxPub); - row.MaxPubTime = alertmonUtils.getLocTimeFromUTC(row.MaxPub); + result.rows.forEach(function(row){ + row.maxpubdate = alertmonUtils.getLocDateFromUTC(row.maxpub); + row.maxpubtime = alertmonUtils.getLocTimeFromUTC(row.maxpub); - if(row.MaxStatus.toLowerCase().indexOf('alert') > -1){ - row.rowClass = 'alert-row'; - } else { row.rowClass = 'non-alert-row'; } + if(row.maxstatus.toLowerCase().indexOf('alert') > -1){ + row.rowclass = 'alert-row'; + } else { row.rowclass = 'non-alert-row'; } }); - res.render('index', {cores: rows}, function(err, html) { + res.render('index', {cores: result.rows}, function(err, html) { if(err !== null) { console.log(err); } else { res.send(html); } }); - } + }); }); }); diff --git a/views/index.hbs b/views/index.hbs index 6efcb88..dd02427 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -13,20 +13,20 @@ {{#each cores}} - + - {{#if this.coreName}} - {{this.coreName}} + {{#if this.corename}} + {{this.corename}} {{else}} - # No Name # + # No Name #

- Edit Details + Edit Details {{/if}} - {{#if this.locationDesc}} - {{this.locationDesc}} + {{#if this.locationdesc}} + {{this.locationdesc}} {{else}} # No Location #
@@ -34,9 +34,9 @@ Edit Details {{/if}} - {{this.MaxPubDate}} - {{this.MaxPubTime}} - {{this.MaxStatus}} + {{this.maxpubdate}} + {{this.maxpubtime}} + {{this.maxstatus}} {{/each}}