/
trip.go
371 lines (328 loc) · 10.9 KB
/
trip.go
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package types
import (
"errors"
"fmt"
"time"
"github.com/gbl08ma/sqalx"
sq "github.com/Masterminds/squirrel"
"github.com/lib/pq"
"github.com/satori/go.uuid"
)
// Trip represents a user-submitted subway trip
type Trip struct {
ID string
StartTime time.Time
EndTime time.Time
Submitter *APIPair
SubmitTime time.Time
EditTime time.Time
Edited bool
UserConfirmed bool
StationUses []*StationUse
}
// GetTrips returns a slice with all registered trips
func GetTrips(node sqalx.Node) ([]*Trip, error) {
s := sdb.Select().
OrderBy("start_time ASC")
return getTripsWithSelect(node, s)
}
// GetTripsForSubmitter returns a slice with all trips submitted by the specified submitter
func GetTripsForSubmitter(node sqalx.Node, submitter *APIPair) ([]*Trip, error) {
s := sdb.Select().
Where(sq.Eq{"submitter": submitter.Key}).
OrderBy("start_time ASC")
return getTripsWithSelect(node, s)
}
// GetTripsForSubmitterBetween returns a slice with trips submitted by the specified submitter made in the specified interval
func GetTripsForSubmitterBetween(node sqalx.Node, submitter *APIPair, start time.Time, end time.Time) ([]*Trip, error) {
s := sdb.Select().
Where(sq.Eq{"submitter": submitter.Key}).
Where(sq.Or{
sq.Expr("start_time BETWEEN ? AND ?",
start, end),
sq.Expr("end_time BETWEEN ? AND ?",
start, end),
}).
OrderBy("start_time ASC")
return getTripsWithSelect(node, s)
}
// getTripsWithSelect returns a slice with all trips that match the conditions in sbuilder
func getTripsWithSelect(node sqalx.Node, sbuilder sq.SelectBuilder) ([]*Trip, error) {
trips := []*Trip{}
tx, err := node.Beginx()
if err != nil {
return trips, err
}
defer tx.Commit() // read-only tx
rows, err := sbuilder.Columns("trip.id", "trip.start_time", "trip.end_time",
"trip.submitter", "trip.submit_time", "trip.edit_time", "trip.user_confirmed").
From("trip").
RunWith(tx).Query()
if err != nil {
return trips, fmt.Errorf("getTripsWithSelect: %s", err)
}
submitters := []string{}
for rows.Next() {
var trip Trip
var timeEdit pq.NullTime
var submitter string
err := rows.Scan(
&trip.ID,
&trip.StartTime,
&trip.EndTime,
&submitter,
&trip.SubmitTime,
&timeEdit,
&trip.UserConfirmed)
if err != nil {
rows.Close()
return trips, fmt.Errorf("getTripsWithSelect: %s", err)
}
trip.EditTime = timeEdit.Time
trip.Edited = timeEdit.Valid
trips = append(trips, &trip)
submitters = append(submitters, submitter)
}
if err := rows.Err(); err != nil {
rows.Close()
return trips, fmt.Errorf("getTripsWithSelect: %s", err)
}
rows.Close()
for i := range trips {
trips[i].Submitter, err = GetPair(tx, submitters[i])
if err != nil {
return trips, fmt.Errorf("getTripsWithSelect: %s", err)
}
s := sdb.Select().
Where(sq.Eq{"trip_id": trips[i].ID}).
OrderBy("entry_time, leave_time ASC")
trips[i].StationUses, err = getStationUsesWithSelect(tx, s)
if err != nil {
return trips, fmt.Errorf("getTripsWithSelect: %s", err)
}
}
return trips, nil
}
// GetTrip returns the Trip with the given ID
func GetTrip(node sqalx.Node, id string) (*Trip, error) {
s := sdb.Select().
Where(sq.Eq{"id": id})
trips, err := getTripsWithSelect(node, s)
if err != nil {
return nil, err
}
if len(trips) == 0 {
return nil, errors.New("Trip not found")
}
return trips[0], nil
}
// getTripIDsWithSelect returns a slice with the IDs of all trips that match the conditions in sbuilder
func getTripIDsWithSelect(node sqalx.Node, sbuilder sq.SelectBuilder) ([]string, error) {
tx, err := node.Beginx()
if err != nil {
return []string{}, err
}
defer tx.Commit() // read-only tx
rows, err := sbuilder.Columns("trip.id").
From("trip").
RunWith(tx).Query()
if err != nil {
return []string{}, fmt.Errorf("GetTripIDs: %s", err)
}
ids := []string{}
for rows.Next() {
var id string
err := rows.Scan(&id)
if err != nil {
rows.Close()
return ids, fmt.Errorf("GetTripIDs: %s", err)
}
ids = append(ids, id)
}
if err := rows.Err(); err != nil {
rows.Close()
return ids, fmt.Errorf("GetTripIDs: %s", err)
}
rows.Close()
return ids, nil
}
// GetTripIDs returns a slice containing the IDs of all the trips in the database
func GetTripIDs(node sqalx.Node) ([]string, error) {
s := sdb.Select().
OrderBy("start_time ASC")
return getTripIDsWithSelect(node, s)
}
// GetTripIDsBetween returns a slice containing the IDs of the trips in the specified interval
func GetTripIDsBetween(node sqalx.Node, start time.Time, end time.Time) ([]string, error) {
s := sdb.Select().
Where(sq.Or{
sq.Expr("start_time BETWEEN ? AND ?",
start, end),
sq.Expr("end_time BETWEEN ? AND ?",
start, end),
}).
OrderBy("start_time ASC")
return getTripIDsWithSelect(node, s)
}
// CountTripsByDay counts trips by day between the specified dates
func CountTripsByDay(node sqalx.Node, start time.Time, end time.Time) ([]time.Time, []int, []int, error) {
tx, err := node.Beginx()
if err != nil {
return []time.Time{}, []int{}, []int{}, err
}
defer tx.Commit() // read-only tx
rows, err := tx.Query("SELECT curd, COUNT(id) FILTER (WHERE user_confirmed = false), COUNT(id) FILTER (WHERE user_confirmed = true) "+
"FROM generate_series(($2 at time zone $1)::date, ($3 at time zone $1)::date, '1 day') AS curd "+
"LEFT OUTER JOIN trip ON curd = (start_time at time zone $1)::date "+
"GROUP BY curd ORDER BY curd;",
start.Location().String(), start, end)
if err != nil {
return []time.Time{}, []int{}, []int{}, fmt.Errorf("CountTripsByDay: %s", err)
}
defer rows.Close()
var dates []time.Time
var unconfirmedCounts []int
var confirmedCounts []int
for rows.Next() {
var date time.Time
var count, confirmedCount int
err := rows.Scan(&date, &count, &confirmedCount)
if err != nil {
return dates, unconfirmedCounts, confirmedCounts, fmt.Errorf("CountTripsByDay: %s", err)
}
dates = append(dates, date)
unconfirmedCounts = append(unconfirmedCounts, count)
confirmedCounts = append(confirmedCounts, confirmedCount)
}
if err := rows.Err(); err != nil {
return dates, unconfirmedCounts, confirmedCounts, fmt.Errorf("CountTripsByDay: %s", err)
}
return dates, unconfirmedCounts, confirmedCounts, nil
}
// AverageSpeed computes the average speed (km/h), total distance (m) and total duration of this trip
func (trip *Trip) AverageSpeed(node sqalx.Node) (speed float64, totalDistance int64, totalDuration time.Duration, err error) {
tx, err := node.Beginx()
if err != nil {
return 0, 0, 0, err
}
defer tx.Commit() // read-only tx
if len(trip.StationUses) <= 1 {
// station visit or invalid trip
// can't extract any data about connections
return 0, 0, 0, fmt.Errorf("Trip not suitable for computing average speed")
}
var startTime, endTime time.Time
for useIdx := 0; useIdx < len(trip.StationUses)-1; useIdx++ {
sourceUse := trip.StationUses[useIdx]
if sourceUse.Manual {
// manual path extensions don't contain valid time data
// skip
continue
}
if sourceUse.Type == Interchange ||
sourceUse.Type == Visit {
continue
}
targetUse := trip.StationUses[useIdx+1]
if targetUse.Manual {
// manual path extensions don't contain valid time data
// skip
continue
}
connection, err := GetConnection(tx, sourceUse.Station.ID, targetUse.Station.ID, true)
if err != nil {
// connection might no longer exist (closed stations, etc.)
// move on
return 0, 0, 0, fmt.Errorf("Trip contains now-invalid connection from %s to %s", sourceUse.Station.ID, targetUse.Station.ID)
}
totalDistance += int64(connection.WorldLength)
if startTime.IsZero() {
startTime = sourceUse.LeaveTime
}
endTime = targetUse.EntryTime
}
totalDuration = endTime.Sub(startTime)
km := float64(totalDistance) / 1000
return km / totalDuration.Hours(), totalDistance, totalDuration, nil
}
// SimultaneousTrips returns a slice containing trips that took place alongside this one
func (trip *Trip) SimultaneousTrips(node sqalx.Node, excludeLongerThan time.Duration) ([]*Trip, error) {
s := sdb.Select().
Where(sq.NotEq{"id": trip.ID}).
Where(sq.LtOrEq{"end_time - start_time": Duration(excludeLongerThan)}).
Where(sq.Expr("(start_time, end_time) OVERLAPS (?, ?)", trip.StartTime, trip.EndTime)).
OrderBy("start_time ASC")
return getTripsWithSelect(node, s)
}
// SimultaneousTripIDs returns a slice containing IDs of the trips that took place alongside this one
func (trip *Trip) SimultaneousTripIDs(node sqalx.Node, excludeLongerThan time.Duration) ([]string, error) {
s := sdb.Select().
Where(sq.NotEq{"id": trip.ID}).
Where(sq.LtOrEq{"end_time - start_time": Duration(excludeLongerThan)}).
Where(sq.Expr("(start_time, end_time) OVERLAPS (?, ?)", trip.StartTime, trip.EndTime)).
OrderBy("start_time ASC")
return getTripIDsWithSelect(node, s)
}
// Update adds or updates the trip
func (trip *Trip) Update(node sqalx.Node) error {
if _, err := uuid.FromString(trip.ID); err != nil || len(trip.ID) != 36 {
return errors.New("AddTrip: invalid trip ID")
}
if len(trip.StationUses) == 1 && trip.StationUses[0].Type != Visit {
return errors.New("AddTrip: trip only has one station use, but is not a visit")
}
tx, err := node.Beginx()
if err != nil {
return err
}
defer tx.Rollback()
timeEdit := pq.NullTime{
Time: trip.EditTime,
Valid: trip.Edited,
}
_, err = sdb.Insert("trip").
Columns("id", "start_time", "end_time", "submitter", "submit_time", "edit_time", "user_confirmed").
Values(trip.ID, trip.StartTime, trip.EndTime, trip.Submitter.Key, trip.SubmitTime, timeEdit, trip.UserConfirmed).
Suffix("ON CONFLICT (id) DO UPDATE SET start_time = ?, end_time = ?, submitter = ?, submit_time = ?, edit_time = ?, user_confirmed = ?",
trip.StartTime, trip.EndTime, trip.Submitter.Key, trip.SubmitTime, timeEdit, trip.UserConfirmed).
RunWith(tx).Exec()
if err != nil {
return errors.New("AddTrip: " + err.Error())
}
var nextAdmissibleEntryTime time.Time
for i := range trip.StationUses {
// fix invalid times produced by old app versions as best as we can
if i != 0 && trip.StationUses[i].EntryTime.Before(nextAdmissibleEntryTime) {
trip.StationUses[i].EntryTime = nextAdmissibleEntryTime
}
if trip.StationUses[i].LeaveTime.Before(trip.StationUses[i].EntryTime) {
trip.StationUses[i].LeaveTime = trip.StationUses[i].EntryTime
}
err = trip.StationUses[i].Update(tx, trip.ID)
if err != nil {
return errors.New("AddTrip: " + err.Error())
}
nextAdmissibleEntryTime = trip.StationUses[i].LeaveTime
}
return tx.Commit()
}
// Delete deletes the trip
func (trip *Trip) Delete(node sqalx.Node) error {
tx, err := node.Beginx()
if err != nil {
return err
}
defer tx.Rollback()
for i := range trip.StationUses {
err = trip.StationUses[i].Delete(tx, trip.ID)
if err != nil {
return errors.New("RemoveTrip: " + err.Error())
}
}
_, err = sdb.Delete("trip").
Where(sq.Eq{"id": trip.ID}).RunWith(tx).Exec()
if err != nil {
return fmt.Errorf("RemoveTrip: %s", err)
}
return tx.Commit()
}