Fix field names on index.hbs; removed rows w/NULL published_at values from DB (showed year=1969 on home page); update email code to be postgres-compliant.
This commit is contained in:
		
							parent
							
								
									11b4c05830
								
							
						
					
					
						commit
						92adabcd47
					
				
							
								
								
									
										54
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								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);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,15 +1,15 @@
 | 
			
		||||
{{!< default}}
 | 
			
		||||
 | 
			
		||||
{{! Index-0 is the most recent message. }}
 | 
			
		||||
<div class="jumbotron {{values.0.statusClass}}">
 | 
			
		||||
<div class="jumbotron {{values.0.statusclass}}">
 | 
			
		||||
    <h1 class="text-center"><b>{{values.0.status}}</b></h1>
 | 
			
		||||
</div>
 | 
			
		||||
<div>
 | 
			
		||||
    <h3 class="text-center">Update received: {{values.0.pubDate}}, {{values.0.pubTime}}</h3>
 | 
			
		||||
    <h3 class="text-center">Update received: {{values.0.pubdate}}, {{values.0.pubtime}}</h3>
 | 
			
		||||
</div>
 | 
			
		||||
<hr></hr>
 | 
			
		||||
<div>
 | 
			
		||||
    <h4 class="text-center">Prior update was: {{values.1.status}} at {{values.1.pubDate}}, {{values.1.pubTime}}</h4>
 | 
			
		||||
    <h4 class="text-center">Prior update was: {{values.1.status}} at {{values.1.pubdate}}, {{values.1.pubtime}}</h4>
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
<hr></hr>
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user