-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
209 lines (195 loc) · 6.27 KB
/
app.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const moment = require('moment-timezone');
const cors = require('cors');
const path = require('path');
require('dotenv').config();
let db_path = process.env.DB_PATH || './'
const db = new sqlite3.Database(db_path + 'temp2.db', sqlite3.OPEN_READONLY)
const app = express();
app.use(cors());
/**
* Checks that the date is a formatted like YYYY-MM-DD
*
* @param {String} date A string representing a date
* @returns {Boolean} True if properly formatted; else false
*/
function isValidDate(date) {
return date.match(/\d{4}-\d{2}-\d{2}/)
}
/**
* Checks that the time if formatted like HH:MM
*
* @param {String} time A string representing the time
* @returns {Boolean} True if properly formatted; else false.
*/
function isValidTime(time) {
return time.match(/\d{2}:\d{2}/)
}
function localEdmontonTimeToISO(date, time) {
const d = new Date(moment.tz(`${date} ${time}`, "America/Edmonton").format());
return d.toISOString();
}
/**
* Gets the current location of all ETS electric buses from the database.
*/
function getElectricBuses() {
const date = moment.tz(moment(), "America/Edmonton");
const least = date.clone().subtract(2, 'minutes');
const electricOnly = true;
return getAllBusesForTimeRange(least, date, electricOnly)
}
/**
*
* Searches the database for locations of buses within a given time range.
*
* @param {moment} least A moment specifying the oldest time to search for buses
* @param {moment} most A moment specifying the newest time to search for buses
* @param {Boolean} electricOnly Whether to return only electric buses
* @returns {Promise} An array of objects, each one representing a bus location.
*/
function getAllBusesForTimeRange(least, most, electricOnly = false) {
return new Promise((res, rej) => {
const sql = `
SELECT *
FROM pos
WHERE timestamp>=strftime('%s', ?)
AND timestamp<=strftime('%s', ?)
AND CASE ? WHEN 1 THEN bus >= 7000 ELSE 1 END
GROUP BY bus;`
db.all(sql, least.format(), most.format(), electricOnly, (err, rows) => {
if (err) {
rej(err);
}
res(rows);
})
});
}
// send the map file background
app.get('/map', (req,res) => res.sendFile('./map2.png', {root: __dirname}))
// electric buses only
app.get('/bus/electric', async (req, res) => {
// get the current location of all electric buses
try {
rows = await getElectricBuses();
res.json(rows);
} catch(error) {
console.error(error)
res.sendStatus(500)
}
});
app.get('/bus/now', (req, res) => {
db.all("SELECT * FROm pos where timestamp > strftime('%s', datetime('now', '-2 minutes')) group by bus", [], (err, rows) => {
if (err) {
// rej(err);
res.send(err);
res.sendStatus(500);
}
res.send(rows);
})
})
app.get('/bus/stats/speed/:bus', (req, res) => {
db.all(`SELECT
avg(speed) as \`speed\`,
max(speed) as \`max\`,
strftime('%Y-%m-%d', datetime(timestamp, 'unixepoch', 'localtime')) as \`date\`
FROM
pos
WHERE
timestamp > strftime('%s', datetime('now', '-4 days'))
AND bus = ?
GROUP BY date;`,
[req.params.bus],
(err, rows) => {
if (err) {
// res.send();
console.error(err);
res.sendStatus(500);
} else {
res.send(rows);
}
}
)
})
app.get('/bus/stats/speed', (req, res) => {
db.all(`SELECT
avg(speed) as \`speed\`,
max(speed) as \`max\`,
strftime('%Y-%m-%d', datetime(timestamp, 'unixepoch', 'localtime')) as \`date\`
FROM
pos
WHERE
timestamp > strftime('%s', datetime('now', '-4 days'))
GROUP BY date;`,
[],
(err, rows) => {
if (err) {
// res.send();
console.error(err);
res.sendStatus(500);
} else {
res.send(rows);
}
}
)
})
app.get('/bus/stats/speed/:bus', (req, res) => {
db.all(`SELECT
avg(speed) as \`speed\`,
max(speed) as \`max\`,
strftime('%Y-%m-%d', datetime(timestamp, 'unixepoch', 'localtime')) as \`date\`
FROM
pos
WHERE
timestamp > strftime('%s', datetime('now', '-4 days'))
GROUP BY date
ORDER BY date desc;`,
[],
(err, rows) => {
if (err) {
// res.send();
console.error(err);
res.sendStatus(500);
} else {
res.send(rows);
}
}
)
})
// flexible API for wall-clock based searching
app.get('/bus/:date/:time/:spread?', async (req,res) => {
//;
try {
let date = req.params.date;
if (!isValidDate(date)) {
throw "Bad date format";
}
let time = req.params.time;
if (!isValidTime(time)) {
throw "Bad time format";
}
const spread = req.params.spread || 2;
const s = (spread % 5);
const d = moment.tz(`${date} ${time}`, "America/Edmonton");
const least = d.clone().subtract(s, 'minutes');
const most = d.clone().add(s, 'minutes');
try {
rows = await getAllBusesForTimeRange(least, most)
res.json(rows)
} catch (err) {
console.error(err)
res.sendStatus(500);
}
} catch (e) {
console.error(err);
res.sendStatus(500);
}
})
// load front-end file
// app.use('/', express.static(path.join(__dirname, 'dist')))
app.use(express.static(path.join(__dirname, 'dist')))
app.get('/*', (req,res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
})
// start the server
app.listen(8081);