Replace GET / with postgres code, had to account for all lower-case object naming in postgres, so column names in view/index.hbs had to match also.
This commit is contained in:
parent
fbfbdd0835
commit
f7a68e8823
85
server.js
85
server.js
@ -36,45 +36,6 @@ var db = new sqlite3.Database(file);
|
|||||||
var pg = require("pg");
|
var pg = require("pg");
|
||||||
var conString = "postgres://alertmon:alertmon@db/alertmon";
|
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.json()); // Needed for JSON POST requests from Particle Cores.
|
||||||
app.use(bodyParser.urlencoded({ extended: false })); // Needed for web POST requests from edit form.
|
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){
|
app.get('/', function(req, res){
|
||||||
var d = new Date();
|
var d = new Date();
|
||||||
console.log("GET /, " + JSON.stringify(d, 4));
|
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 " +
|
pg.connect(conString, function(err, client, done) {
|
||||||
"from Alerts al " +
|
if(err) {
|
||||||
"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 " +
|
return console.error('error fetching client from pool', err);
|
||||||
"left join Cores co on al.coreId = co.coreId " +
|
}
|
||||||
"group by al.coreId ";
|
var devIndexQry =
|
||||||
db.all(devIndexQry, function(err, rows){
|
"select al.coreid, max(co.corename) as corename, max(co.locationdesc) as locationdesc, mpub.maxpub, al.status as maxstatus " +
|
||||||
if(err !== null) {
|
"from alerts al " +
|
||||||
console.log(err);
|
"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 " +
|
||||||
} else {
|
"left join cores co on al.coreid = co.coreid " +
|
||||||
//console.log(rows);
|
"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.
|
// Loop over elements in rows array, convert ugly UTC times to pretty local times.
|
||||||
rows.forEach(function(row){
|
result.rows.forEach(function(row){
|
||||||
row.MaxPubDate = alertmonUtils.getLocDateFromUTC(row.MaxPub);
|
row.maxpubdate = alertmonUtils.getLocDateFromUTC(row.maxpub);
|
||||||
row.MaxPubTime = alertmonUtils.getLocTimeFromUTC(row.MaxPub);
|
row.maxpubtime = alertmonUtils.getLocTimeFromUTC(row.maxpub);
|
||||||
|
|
||||||
if(row.MaxStatus.toLowerCase().indexOf('alert') > -1){
|
if(row.maxstatus.toLowerCase().indexOf('alert') > -1){
|
||||||
row.rowClass = 'alert-row';
|
row.rowclass = 'alert-row';
|
||||||
} else { row.rowClass = 'non-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) {
|
if(err !== null) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
} else {
|
} else {
|
||||||
res.send(html);
|
res.send(html);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -13,20 +13,20 @@
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{{#each cores}}
|
{{#each cores}}
|
||||||
<tr class="{{this.rowClass}}">
|
<tr class="{{this.rowclass}}">
|
||||||
<td>
|
<td>
|
||||||
{{#if this.coreName}}
|
{{#if this.corename}}
|
||||||
<a href="https://particle.kaplon.us/core/{{this.coreId}}">{{this.coreName}}</a>
|
<a href="https://particle.kaplon.us/core/{{this.coreId}}">{{this.corename}}</a>
|
||||||
{{else}}
|
{{else}}
|
||||||
<a href="https://particle.kaplon.us/core/{{this.coreId}}"># No Name #</a>
|
<a href="https://particle.kaplon.us/core/{{this.coreid}}"># No Name #</a>
|
||||||
</br>
|
</br>
|
||||||
</br>
|
</br>
|
||||||
<a href="https://particle.kaplon.us/core/edit/{{this.coreId}}" class="btn btn-primary">Edit Details</a>
|
<a href="https://particle.kaplon.us/core/edit/{{this.coreid}}" class="btn btn-primary">Edit Details</a>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{{#if this.locationDesc}}
|
{{#if this.locationdesc}}
|
||||||
{{this.locationDesc}}
|
{{this.locationdesc}}
|
||||||
{{else}}
|
{{else}}
|
||||||
# No Location #
|
# No Location #
|
||||||
</br>
|
</br>
|
||||||
@ -34,9 +34,9 @@
|
|||||||
<a href="https://particle.kaplon.us/core/edit/{{this.coreId}}" class="btn btn-primary">Edit Details</a>
|
<a href="https://particle.kaplon.us/core/edit/{{this.coreId}}" class="btn btn-primary">Edit Details</a>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
</td>
|
</td>
|
||||||
<td>{{this.MaxPubDate}}</td>
|
<td>{{this.maxpubdate}}</td>
|
||||||
<td>{{this.MaxPubTime}}</td>
|
<td>{{this.maxpubtime}}</td>
|
||||||
<td>{{this.MaxStatus}}</td>
|
<td>{{this.maxstatus}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{/each}}
|
{{/each}}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
Loading…
Reference in New Issue
Block a user