diff --git a/server.js b/server.js index 6979fe5..8512ab5 100644 --- a/server.js +++ b/server.js @@ -57,7 +57,7 @@ app.get('/', function(req, res){ } var devIndexQry = "select status, published_at " + - "from Alerts " + + "from alerts " + "where status in ('Open', 'Closed') " + "order by published_at desc " + "limit 2" @@ -69,11 +69,12 @@ app.get('/', function(req, res){ } // Loop over elements in rows array, convert ugly UTC times to pretty local times. result.rows.forEach(function(row){ - row.pubDate = courtsopenUtils.getLocDateFromUTC(row.published_at); - row.pubTime = courtsopenUtils.getLocTimeFromUTC(row.published_at); + winston.info(row.published_at); + row.pubdate = courtsopenUtils.getLocDateFromUTC(row.published_at); + row.pubtime = courtsopenUtils.getLocTimeFromUTC(row.published_at); if(row.status.toLowerCase().indexOf('closed') > -1){ - row.statusClass = 'closed'; - } else { row.statusClass = 'open'; } + row.statusclass = 'closed'; + } else { row.statusclass = 'open'; } }); res.render('index', {values: result.rows}, function(err, html) { @@ -120,7 +121,7 @@ app.post('/', function(req, res){ }; var received_at = new Date(Date.now()); received_at = received_at.toISOString(); - console.log(statusFromCode + ' ' + received_at); + winston.info(statusFromCode + ' ' + received_at); pg.connect(conString, function(err, client, done) { if(err) { return console.error('error fetching client from pool', err); } client.query( @@ -128,30 +129,37 @@ app.post('/', function(req, res){ [JSON.stringify(req.body, null, 4), deviceid, received_at, statusFromCode] ); }); - } res.status(204).send('POST received'); }); setInterval(function() { // Check every hour to see if GoodMorning or GoodEvening has gone missing. - var deadManQry = "select published_at from Alerts where datetime(published_at) > datetime('now', '-14.5 hours') order by datetime(published_at) limit 1"; - db.get(deadManQry, function(err, row){ - if (err !== null) { winston.error(err); } - else if (typeof row == "undefined") { - mailOptions.text = "It's been too long since the last data transmission from device. \n\n"; - winston.info(mailOptions.text); - // Don't include any other details for now, will need to change DB query to get details on last message received. - //mailOptions.text = mailOptions.text + 'Status message, ' + status + '\n'; - //mailOptions.text = mailOptions.text + 'Published at, ' + courtsopenUtils.getLocDateFromUTC(pubAt) + ' ' + courtsopenUtils.getLocTimeFromUTC(pubAt) + '\n'; - transporter.sendMail(mailOptions, function(error, info){ - if(error){ - winston.info(error); - }else{ - winston.info('Message sent: ' + info.response); - } - }); + pg.connect(conString, function(err, client, done) { + if(err) { + return winston.error('error fetching client from pool', err); } + var deadManQry = "select published_at from alerts where to_timestamp(published_at, 'YYYY-MM-DD HH24:MI:SS') > (now() - interval '14.5 hours') limit 1"; + client.query(deadManQry, function(err, result) { + //call `done()` to release the client back to the pool + done(); + if(err) { + return winston.error('error running query', err); + } else if (typeof result == "undefined") { + mailOptions.text = "It's been too long since the last data transmission from device. \n\n"; + winston.info(mailOptions.text); + // Don't include any other details for now, will need to change DB query to get details on last message received. + //mailOptions.text = mailOptions.text + 'Status message, ' + status + '\n'; + //mailOptions.text = mailOptions.text + 'Published at, ' + courtsopenUtils.getLocDateFromUTC(pubAt) + ' ' + courtsopenUtils.getLocTimeFromUTC(pubAt) + '\n'; + transporter.sendMail(mailOptions, function(error, info){ + if(error){ + winston.info(error); + }else{ + winston.info('Message sent: ' + info.response); + } + }); + } + }); }); }, 60 * 60 * 1000); diff --git a/views/index.hbs b/views/index.hbs index cecd296..e33536d 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -1,15 +1,15 @@ {{!< default}} {{! Index-0 is the most recent message. }} -
+

{{values.0.status}}

-

Update received: {{values.0.pubDate}}, {{values.0.pubTime}}

+

Update received: {{values.0.pubdate}}, {{values.0.pubtime}}


-

Prior update was: {{values.1.status}} at {{values.1.pubDate}}, {{values.1.pubTime}}

+

Prior update was: {{values.1.status}} at {{values.1.pubdate}}, {{values.1.pubtime}}