All DB functions transferred to Postgres; tested ok, but haven't checked new device data going into alerts table, or core-edit when device not yet in cores table.

This commit is contained in:
jkaplon 2015-10-11 16:11:32 -04:00
parent 45a9a42f2e
commit 336bc8a844
2 changed files with 74 additions and 74 deletions

110
server.js
View File

@ -123,40 +123,29 @@ app.post('/core/edit/:id', function(req, res){
if (!req.body) { if (!req.body) {
return res.sendStatus(400); return res.sendStatus(400);
} else { } else {
// check existence of row in Cores table, if there update, otherwise insert. pg.connect(conString, function(err, client, done) {
var existQry = "SELECT coreId FROM Cores WHERE coreId = ?;" if(err) { return console.error('error fetching client from pool', err); }
db.get(existQry, coreId, function(err, row){ // check existence of row in Cores table, if not there insert, otherwise update.
if(err) throw err; client.query("SELECT coreid FROM cores WHERE coreid = ($1);", [coreId], function(err, result) {
console.log(typeof row); done();
console.log(row);
if(typeof row == "undefined") { if(err) { return console.error('error running query', err); }
var insStmt = db.prepare("INSERT INTO Cores (coreId, coreName, locationDesc) VALUES (?, ?, ?);"); if(typeof result == 'undefined') {
insStmt.run( client.query(
coreId, "INSERT INTO cores (coreid, corename, locationdesc) VALUES ($1, $2, $3);",
req.body.deviceName, [coreId, req.body.deviceName, req.body.locationDesc]
req.body.locationDesc
); );
insStmt.finalize();
} else { } else {
if (req.body.deviceName !== "") { if (req.body.deviceName !== "") {
var stmt = db.prepare("UPDATE Cores SET coreName = ? WHERE coreId = ?"); client.query("UPDATE cores SET corename =($1) WHERE coreid =($2)", [req.body.deviceName, coreId]);
stmt.run(
req.body.deviceName,
coreId
);
stmt.finalize();
} }
if (req.body.locationDesc !== "") { if (req.body.locationDesc !== "") {
var stmt = db.prepare("UPDATE Cores SET locationDesc = ? WHERE coreId = ?"); client.query("UPDATE cores SET locationdesc =($1) WHERE coreid =($2)", [req.body.locationDesc, coreId]);
stmt.run(
req.body.locationDesc,
coreId
);
stmt.finalize();
} }
} }
}); });
});
res.redirect('https://particle.kaplon.us/'); res.redirect('https://particle.kaplon.us/');
} }
@ -166,31 +155,40 @@ app.get('/core/:id', function(req, res){
var d = new Date(); var d = new Date();
var coreId = req.params.id; var coreId = req.params.id;
console.log("GET /core/" + coreId + ", " + JSON.stringify(d, 4)); console.log("GET /core/" + coreId + ", " + JSON.stringify(d, 4));
var coreMsgQry = "SELECT al.coreId, al.published_at, al.status, co.coreName " +
"FROM Alerts al left join Cores co on al.coreId = co.coreId " + pg.connect(conString, function(err, client, done) {
"WHERE al.coreId = ? ORDER BY al.published_at DESC LIMIT 30;" if(err) { return console.error('error fetching client from pool', err); }
db.all(coreMsgQry, coreId, function(err, rows){
var coreMsgQry = "SELECT al.coreid, al.published_at, al.status, co.corename " +
"FROM alerts al left join cores co on al.coreid = co.coreid " +
"WHERE al.coreid = ($1) ORDER BY al.published_at DESC LIMIT 30;"
client.query(coreMsgQry, [coreId], 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.
result.rows.forEach(function(row){
row.pubdate = alertmonUtils.getLocDateFromUTC(row.published_at);
row.pubtime = alertmonUtils.getLocTimeFromUTC(row.published_at);
if(row.status.toLowerCase().indexOf('alert') > -1){
row.rowclass = 'alert-row';
} else { row.rowclass = 'non-alert-row'; }
});
res.render('core', {alerts: result.rows}, function(err, html) {
if(err !== null) { if(err !== null) {
console.log(err); console.log(err);
} else { } else {
//console.log("SELECT coreId, published_at FROM Alerts WHERE coreId = '" + coreId + "' ORDER BY published_at DESC LIMIT 30;");
//console.log(rows);
// Loop over elements in rows array, convert ugly UTC times to pretty local times.
rows.forEach(function(row){
row.pubDate = alertmonUtils.getLocDateFromUTC(row.published_at);
row.pubTime = alertmonUtils.getLocTimeFromUTC(row.published_at);
if(row.status.toLowerCase().indexOf('alert') > -1){
row.rowClass = 'alert-row';
} else { row.rowClass = 'non-alert-row'; }
});
res.render('core', {alerts: rows}, function(err, html) {
res.send(html); res.send(html);
});
} }
}); });
}); });
});
});
app.post('/', function(req, res){ app.post('/', function(req, res){
var postEvent = req.body.postEvent; var postEvent = req.body.postEvent;
@ -201,14 +199,14 @@ app.post('/', function(req, res){
var status = JSON.stringify(innerDataJSON.status, null, 4).slice(1,-1); var status = JSON.stringify(innerDataJSON.status, null, 4).slice(1,-1);
var coreid = JSON.stringify(req.body.coreid, null, 4).slice(1,-1); var coreid = JSON.stringify(req.body.coreid, null, 4).slice(1,-1);
var pubAt = JSON.stringify(req.body.published_at, null, 4).slice(1,-1); var pubAt = JSON.stringify(req.body.published_at, null, 4).slice(1,-1);
var stmt = db.prepare("INSERT INTO Alerts (OrigJSON, coreid, published_at, status) VALUES (?, ?, ?, ?)");
stmt.run( pg.connect(conString, function(err, client, done) {
JSON.stringify(req.body, null, 4), if(err) { return console.error('error fetching client from pool', err); }
coreid, client.query(
pubAt, "INSERT INTO alerts (origjson, coreid, published_at, status) VALUES ($1, $2, $3, $4);",
status [JSON.stringify(req.body, null, 4), coreid, pubAt, status]
); );
stmt.finalize(); });
// Send emails on alerts only // Send emails on alerts only
if(status.toLowerCase().indexOf('alert') > -1){ if(status.toLowerCase().indexOf('alert') > -1){
@ -216,13 +214,14 @@ app.post('/', function(req, res){
mailOptions.text = mailOptions.text + 'Status message, ' + status + '\n'; mailOptions.text = mailOptions.text + 'Status message, ' + status + '\n';
mailOptions.text = mailOptions.text + 'Published at, ' + alertmonUtils.getLocDateFromUTC(pubAt) + ' ' + alertmonUtils.getLocTimeFromUTC(pubAt) + '\n'; mailOptions.text = mailOptions.text + 'Published at, ' + alertmonUtils.getLocDateFromUTC(pubAt) + ' ' + alertmonUtils.getLocTimeFromUTC(pubAt) + '\n';
//mailOptions.text = mailOptions.text + 'From device, ' + alertmonUtils.getCoreNameFromCoreId(db, coreid) + '\n'; //mailOptions.text = mailOptions.text + 'From device, ' + alertmonUtils.getCoreNameFromCoreId(db, coreid) + '\n';
var nameQry = 'SELECT coreName FROM Cores WHERE coreId = ?;' pg.connect(conString, function(err, client, done) {
db.get(nameQry, coreid, function(err, row){ if(err) { return console.error('error fetching client from pool', err); }
if ((err) || (typeof row == undefined)) { client.query('SELECT corename FROM cores WHERE coreid = $1;', [coreid], function(err, result) {
if ((err) || (typeof result == undefined)) {
// Don't care about this error or empty result set, still need to send email. // Don't care about this error or empty result set, still need to send email.
row.coreName = '# No Name #'; result.rows[0].corename = '# No Name #';
} }
mailOptions.text = mailOptions.text + 'From device, ' + row.coreName + '\n'; mailOptions.text = mailOptions.text + 'From device, ' + result.rows[0].corename + '\n';
transporter.sendMail(mailOptions, function(error, info){ transporter.sendMail(mailOptions, function(error, info){
if(error){ if(error){
@ -232,6 +231,7 @@ app.post('/', function(req, res){
} }
}); });
}); });
});
} }
//res.send(JSON.stringify(req.body, null, 4)); //res.send(JSON.stringify(req.body, null, 4));
}); });

View File

@ -3,14 +3,14 @@
<p> <p>
<b> <b>
{{! Grab 1st coreName value (it will be same value for entire set).}} {{! Grab 1st coreName value (it will be same value for entire set).}}
{{#if alerts.0.coreName}} {{#if alerts.0.corename}}
Alerts from Particle Core: {{alerts.0.coreName}} Alerts from Particle Core: {{alerts.0.corename}}
{{else}} {{else}}
Alerts from Particle Core: # No Name # Alerts from Particle Core: # No Name #
{{/if}} {{/if}}
</b> </b>
<a href="https://particle.kaplon.us/core/edit/{{alerts.0.coreId}}" class="btn btn-primary">Edit Details</a> <a href="https://particle.kaplon.us/core/edit/{{alerts.0.coreid}}" class="btn btn-primary">Edit Details</a>
<a href="https://particle.kaplon.us" class="btn btn-primary">Back to Device Listing</a> <a href="https://particle.kaplon.us" class="btn btn-primary">Back to Device Listing</a>
</p> </p>
@ -24,9 +24,9 @@
</thead> </thead>
<tbody> <tbody>
{{#each alerts}} {{#each alerts}}
<tr class="{{this.rowClass}}"> <tr class="{{this.rowclass}}">
<td>{{this.pubDate}}</td> <td>{{this.pubdate}}</td>
<td>{{this.pubTime}}</td> <td>{{this.pubtime}}</td>
<td>{{this.status}}</td> <td>{{this.status}}</td>
</tr> </tr>
{{/each}} {{/each}}