-
Notifications
You must be signed in to change notification settings - Fork 3
/
queries.js
148 lines (128 loc) · 4.6 KB
/
queries.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
const moment = require('moment-timezone');
const formatStrAsTime = (str) => {
const parts = str.split(':');
if (parts.length === 0) {
return '00:00';
}
const hours = `0${parts[0]}`.slice(-2);
const minutes = parts[1] ? `0${parts[1]}`.slice(-2) : '00';
return `${hours}:${minutes}`;
};
// get service ids by date from calendar
// each date has one or more associated service ids
// date string must be exactly 8 numbers or it is ignored
// has to be in the format YYYYMMDD
// if date is invalid or missing, it defaults to today
var getServiceIdsByDateSql = (date) => {
let dateStr;
if (/^[\d]{8}$/.test(date)) {
dateStr = moment(date).format("YYYYMMDD");
} else {
dateStr = moment().format("YYYYMMDD");
}
return `SELECT service_id from calendar_dates where date = ${dateStr}`;
};
// get all trips by route with optional params
// params: routeId, serviceDay, directionId, stopId, startTime, endTime
const getTripsByRouteSql = (params) => {
const where = [];
if (!params.routeId) {
return null;
}
where.push(`route_id = ${params.routeId}`);
where.push(`service_id IN (${getServiceIdsByDateSql(params.serviceDay)})`);
if (params.stopId) {
where.push(`stops.stop_id = ${params.stopId}`);
} else {
where.push('stop_times.stop_sequence = 1'); // default to 1, the first stop
}
if (params.directionId) {
where.push(`direction_id = ${params.directionId}`);
}
if (params.startTime) {
where.push(`departure_time >= '${formatStrAsTime(params.startTime)}'`);
}
if (params.endTime) {
where.push(`departure_time <= '${formatStrAsTime(params.endTime)}'`);
}
return `SELECT trips.trip_id, trips.trip_headsign, departure_time, stops.stop_id, stop_name, direction_id from trips
LEFT JOIN stop_times on stop_times.trip_id = trips.trip_id
LEFT JOIN stops on stop_times.stop_id = stops.stop_id
WHERE ${where.join(' AND ')}
ORDER BY direction_id, departure_time;`;
};
// get all trips by route with optional params
// params: routeId, serviceDay, directionId, stopId, startTime, endTime
const getTripsByStopIdSql = (params) => {
const where = [];
if (!params.stopId) {
return null;
}
where.push(`stops.stop_id = ${params.stopId}`);
where.push(`service_id IN (${getServiceIdsByDateSql(params.serviceDay)})`);
if (params.routeId) {
where.push(`stops.route_id = ${params.routeId}`);
}
if (params.directionId) {
where.push(`direction_id = ${params.directionId}`);
}
if (params.startTime) {
where.push(`departure_time >= '${formatStrAsTime(params.startTime)}'`);
}
if (params.endTime) {
where.push(`departure_time <= '${formatStrAsTime(params.endTime)}'`);
}
return `select trips.trip_id, service_id, arrival_time, departure_time,
stop_name, stops.stop_id, route_id, trips.trip_headsign from stop_times
left join stops on stop_times.stop_id = stops.stop_id
left join trips on stop_times.trip_id = trips.trip_id
WHERE ${where.join(' AND ')}
ORDER BY departure_time;`;
};
// get all stops for a particular route with optional params
// params: routeId, serviceDay, directionId
const getStopsByRouteIdSql = (params) => {
const where = [];
if (!params.routeId) {
return null;
}
where.push(`trips.route_id = ${params.routeId}`);
where.push(`service_id IN (${getServiceIdsByDateSql(params.serviceDay)})`);
if (params.directionId) {
where.push(`trips.direction_id = ${params.directionId}`);
}
return `select distinct stop_times.stop_id, stop_name, stop_lat, stop_lon from stop_times
left join stops on stops.stop_id = stop_times.stop_id
where stop_times.trip_id in
(select trips.trip_id from trips WHERE ${where.join(' AND ')})
order by stop_name;`;
};
// all stops on a particular trip
const getStopsByTripSql = (tripId) => {
return `SELECT stops.stop_id, stop_name, departure_time, arrival_time, stop_sequence, stop_lat, stop_lon
FROM stop_times
LEFT JOIN stops on stops.stop_id = stop_times.stop_id
WHERE trip_id = ${tripId}
ORDER by stop_sequence;`;
};
// get all routes associated with a particular stop
const getRoutesByStopSql = (stopId) => {
return `SELECT stop_id, route_id, count(trips.trip_id)
FROM stop_times
LEFT JOIN trips on stop_times.trip_id = trips.trip_id
WHERE stop_id = ${stopId}
GROUP BY route_id;`;
};
const getTripScheduleSql = (tripId) => {
return `SELECT stop_id,
arrival_time as arrival,
departure_time as departure FROM stop_times WHERE trip_id = ${tripId} ORDER BY stop_sequence;`;
};
module.exports = {
getTripsByRouteSql,
getStopsByTripSql,
getRoutesByStopSql,
getTripScheduleSql,
getTripsByStopIdSql,
getStopsByRouteIdSql
};