-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
315 lines (270 loc) · 8.46 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
const express = require("express");
const path = require("path");
const knex = require("knex");
const axios = require("axios");
const bodyParser = require("body-parser");
require("dotenv").config();
const { DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD } = process.env;
const database = knex({
client: "pg",
connection: {
host: DB_HOST,
port: DB_PORT,
user: DB_USER,
password: DB_PASSWORD,
database: DB_NAME
}
});
module.exports = express()
.use(express.static(path.join(__dirname, "public")))
.use(bodyParser.json())
.set("view engine", "ejs")
.set("views", path.join(__dirname, "views"))
.get("/", asyncHandler(renderMap))
.get("/api/:locationID", asyncHandler(getInfo))
.post("/api/search", asyncHandler(search))
.use(errorHandler);
// Send any uncaught error to the default error handler
function asyncHandler(fn) {
return function(req, res, next) {
return Promise.resolve(fn(req, res, next).catch(next));
};
}
// Fetches the locations, converts it to GeoJSON and renders the home view
async function renderMap(req, res) {
// The only things we need to render the dots on the map is the lat long and
// an identifier. We can later use this identifier to fetch the full info of a
// point
const locations = await database
.select("id", "latitude", "longitude")
.from("locations");
// Convert the raw locations to GeoJSON. Mapbox on the front-end will use this as
// the initial dataset
const geojson = {
type: "FeatureCollection",
features: locations.map(l => ({
id: l.id,
type: "Feature",
geometry: {
type: "Point",
coordinates: [l.longitude, l.latitude]
}
}))
};
res.render("index", { locations: geojson });
}
// Get the info for a single point on the map by identifier
async function getInfo(req, res) {
const locationID = req.params.locationID;
// Fetch the full location data
const locations = await database
.select([
"name",
"line1",
"line2",
"zip",
"city",
"state",
"wheelchair_accessible",
"latitude",
"longitude"
])
.from("locations")
.where({ id: locationID });
const location = locations[0];
// Get all the meetings that are held at this location
const meetings = await database
.select(["id", "title", "details"])
.from("meetings")
.where({ location_id: locationID });
// For every one of these meetings, fetch the individual meetings
const hours = await database
.select([
"meeting_hours.day",
"meeting_hours.start_time",
"meeting_hours.end_time",
"meeting_hours.special_interest",
"meeting_hours.meeting_id",
"meeting_types.name AS type"
])
.from("meeting_hours")
.whereIn("meeting_id", meetings.map(h => h.id))
.leftJoin(
"meeting_types",
"meeting_hours.meeting_type_id",
"meeting_types.id"
);
// How to sort the days of the week
const sorter = {
Monday: 1,
Tuesday: 2,
Wednesday: 3,
Thursday: 4,
Friday: 5,
Saturday: 6,
Sunday: 7
};
// Combine hours and meetings into a single object
const result = {
location,
meetings: meetings
.map(({ id, title, details }) => {
return {
title,
details,
hours: hours
// Only nest the hours of the meeting we're working on
.filter(h => h.meeting_id === id)
// Strip out the fields we don't need
.map(({ day, start_time, end_time, special_interest, type }) => ({
day: getDayName(day),
start_time: formatTime(start_time),
end_time: formatTime(end_time),
special_interest,
type
}))
// Sort by day of the week
.sort((a, b) => {
return sorter[a.day] > sorter[b.day] ? 1 : -1;
})
};
})
// Sort the top level meetings by title alphabetically
.sort((a, b) => (a.title > b.title ? 1 : -1))
};
return res.json(result);
}
// Send the search query to NLP, use result to search for IDs of locations so the
// application can show just those locations
async function search(req, res) {
const q = req.body.q;
const { data } = await axios.get("https://api.wit.ai/message", {
params: {
q,
v: 20181205
},
headers: {
Authorization: `Bearer ${process.env.WIT_KEY}`
}
});
const info = {};
let rows = await database
.select("id")
.from("locations")
.map(row => row.id);
if (data.entities) {
await Promise.all(
Object.keys(data.entities).map(async entityName => {
const entity = data.entities[entityName][0];
console.log(entity);
if (entity.confidence < 0.9) return;
if (entityName === "neighborhood") {
const center = {
lat: +entity.metadata.split(",")[0],
lon: +entity.metadata.split(",")[1]
};
info.center = center;
const locationIDs = await database
.select("id")
.from("locations")
.whereRaw(
`
(
3959 *
acos(
cos(radians(${center.lat})) *
cos(radians(latitude)) *
cos(
radians(longitude) - radians(${center.lon})
) +
sin(radians(${center.lat})) *
sin(radians(latitude))
)
) < 0.75
`
)
.map(row => row.id);
rows = rows.filter(id => locationIDs.includes(id));
}
// Specific time or day "tomorrow", "in 2 days", "now", "tomorrow at 3"
if (entityName === "datetime" && entity.value) {
const days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
const date = new Date(entity.value);
const day = days[date.getDay()];
const query = database
.select("locations.id")
.from("meeting_hours")
.leftJoin("meetings", "meeting_hours.meeting_id", "meetings.id")
.leftJoin("locations", "meetings.location_id", "locations.id")
.where({ day });
if (entity.grain === "hour" || entity.grain === "minute" || entity.grain === "second") {
query.where({ start_time: date.toLocaleTimeString("en-GB") });
}
query.groupBy("locations.id");
const dayIDs = await query.map(row => row.id);
rows = rows.filter(id => dayIDs.includes(id));
info.day = day;
}
// Time range "before 3pm", "after 10a", "between 12 and 4"
// Because the meetings are a recurring event, it doesn't necessarily make
// sense to have "After monday", as it will wrap around indefinitely
if (entityName === "datetime" && (entity.to || entity.from)) {
const query = database
.select("locations.id")
.from("meeting_hours")
.leftJoin("meetings", "meeting_hours.meeting_id", "meetings.id")
.leftJoin("locations", "meetings.location_id", "locations.id");
if (entity.from) {
const date = new Date(entity.from.value);
query.where("start_time", ">", date.toLocaleTimeString("en-GB"));
info.from = date;
}
if (entity.to) {
const date = new Date(entity.to.value);
query.where("start_time", "<", date.toLocaleTimeString("en-GB"));
info.to = date;
}
query.groupBy("locations.id");
const dayIDs = await query.map(row => row.id);
rows = rows.filter(id => dayIDs.includes(id));
}
})
);
}
return res.json({
ids: rows,
info
});
}
// Console log any uncaught errors and return a 500 error
function errorHandler(error, req, res, next) {
console.log("\n" + JSON.stringify(error, null, 2) + "\n");
res.status(500).end();
}
// Convert a day shorthand to the full name
function getDayName(day) {
switch (day) {
case "mon":
return "Monday";
case "tue":
return "Tuesday";
case "wed":
return "Wednesday";
case "thu":
return "Thursday";
case "fri":
return "Friday";
case "sat":
return "Saturday";
case "sun":
return "Sunday";
}
}
// Convert a timestring YYYY-MM-DDTHH:MM:SS to 12h format
// 2018-12-10T15:36:00 => 3:36PM
function formatTime(timeString) {
const H = +timeString.substr(0, 2);
const h = H % 12 || 12;
const ampm = H < 12 ? "AM" : "PM";
return h + timeString.substr(2, 3) + ampm;
}