-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
106 lines (95 loc) · 2.93 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// server.js
const express = require('express'); // Add the express framework
let app = express();
const bodyParser = require('body-parser'); // Add the body-parser tool
app.use(bodyParser.json()); // Add support for JSON encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // Add support for URL encoded bodies
const pug = require('pug'); // Add pug
//Create Database Connection
const pgp = require('pg-promise')();
//db config
const dbConfig = {
host: 'localhost',
port: 5432,
database: 'traffic_db',
user: 'postgres',
password: 'Appl3'
};
let db = pgp(dbConfig);
//sets pug as view engine
app.set('view engine', 'pug');
app.use(express.static(__dirname + '/'));
//declare constants
const REFRESH_INTERVAL = "1 hour";
// home page
app.get('/', function(req, res) {
// select all locations and their latest reading
var query = "SELECT DISTINCT ON (l.loc_id) l.loc_id AS id, l.loc_desc AS name, ";
query += "l.loc_hours AS hours, d.volume_db AS volume, d.time AS time ";
query += "FROM locations l LEFT JOIN data d ON d.loc_id = l.loc_id ";
query += "ORDER BY l.loc_id, d.time DESC;";
//console.log(query);
db.any(query)
.then(function (location_status) {
//console.log(location_status);
res.render('pages/home', {
page_title: 'Home',
data: location_status,
display_location: '',
display_data: ''
})
})
.catch(function (err) {
console.log(err);
res.render('pages/home', {
page_title: 'Home',
data: '',
display_location: '',
display_data: ''
})
})
});
// form to select a location to view
app.post('/select_location', function(req, res) {
var query = "SELECT DISTINCT ON (l.loc_id) l.loc_id AS id, l.loc_desc AS name, ";
query += "l.loc_hours AS hours, d.volume_db AS volume, d.time AS time ";
query += "FROM locations l LEFT JOIN data d ON d.loc_id = l.loc_id ";
query += "ORDER BY l.loc_id, d.time DESC;";
var location_to_view = req.body.location;
//console.log("Location: ", location_to_view, "\n");
var location_query = "SELECT loc_id AS id, loc_desc AS name, loc_hours AS hours ";
location_query += "FROM locations WHERE loc_id = '" + location_to_view + "';";
var data_query = "SELECT volume_db AS volume, time FROM data ";
data_query += "WHERE loc_id = '" + location_to_view;
data_query += "' AND time > (NOW() - interval '3 weeks') ";
data_query += "ORDER BY time ASC;";
//console.log(location_query, "\n");
console.log(data_query, "\n");
db.task('get-everything', task => {
return task.batch([
task.any(query),
task.any(location_query),
task.any(data_query)
]);
})
.then(info => {
res.render('pages/home', {
page_title: 'Home',
data: info[0],
display_location: info[1][0],
display_data: info[2]
})
})
.catch(err => {
console.log(err);
res.render('pages/home',{
page_title: 'Home',
data: '',
display_location: '',
display_data: ''
})
})
});
var port = 2048;
app.listen(port);
console.log('listening on port ' + port);