/
typseconds.go
513 lines (454 loc) · 18.3 KB
/
typseconds.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package compute
import (
"math"
"sort"
"time"
"github.com/gbl08ma/sqalx"
"github.com/underlx/disturbancesmlx/types"
)
// UpdateTypicalSeconds calculates and updates the TypicalSeconds
// for all the Connections and Transfers where that can be done using the registered
// Trips from the past month.
// Current TypicalSeconds are ignored and discarded.
func UpdateTypicalSeconds(node sqalx.Node, yieldFor time.Duration) error {
tx, err := node.Beginx()
if err != nil {
return err
}
defer tx.Rollback()
startTime := time.Now().AddDate(0, -1, 0)
tripIDs, err := types.GetTripIDsBetween(tx, startTime, time.Now())
if err != nil {
return err
}
mainLog.Printf("UpdateTypicalSeconds: %d trip IDs\n", len(tripIDs))
// we can use pointers as keys in the following maps because types implements an internal cache
// that ensures the pointers to the transfers stay the same throughout this transaction
// (i.e. only one instance of each transfer is brought into memory)
transferAvgNumerator := make(map[*types.Transfer]float64)
transferAvgDenominator := make(map[*types.Transfer]float64)
// we can use pointers as keys in the following maps because types implements an internal cache
// that ensures the pointers to the transfers stay the same throughout this transaction
// (i.e. only one instance of each connection is brought into memory)
connectionAvgNumerator := make(map[*types.Connection]float64)
connectionAvgDenominator := make(map[*types.Connection]float64)
connectionStopAvgNumerator := make(map[*types.Connection]float64)
connectionStopAvgDenominator := make(map[*types.Connection]float64)
connectionWaitAvgNumerator := make(map[*types.Connection]float64)
connectionWaitAvgDenominator := make(map[*types.Connection]float64)
processTransfer := func(transfer *types.Transfer, use *types.StationUse) error {
seconds := use.LeaveTime.Sub(use.EntryTime).Seconds()
// if going from one line to another took more than 15 minutes,
// probably what really happened was that the client's clock was adjusted
// in the meantime, OR the user decided to go shop or something at the station
if seconds < 15*60 {
transferAvgNumerator[transfer] += seconds - 20
transferAvgDenominator[transfer]++
}
return nil
}
processConnection := func(connection *types.Connection, sourceUse *types.StationUse, targetUse *types.StationUse) error {
seconds := targetUse.EntryTime.Sub(sourceUse.LeaveTime).Seconds()
// if going from one station to another took more than 10 minutes,
// probably what really happened was that the client's clock was adjusted
// in the meantime
if seconds < 10*60 {
connectionAvgNumerator[connection] += seconds + 20
connectionAvgDenominator[connection]++
}
waitSeconds := sourceUse.LeaveTime.Sub(sourceUse.EntryTime).Seconds()
if sourceUse.Type == types.NetworkEntry && waitSeconds < 60*3 {
connectionWaitAvgNumerator[connection] += waitSeconds - 20
connectionWaitAvgDenominator[connection]++
} else if sourceUse.Type == types.GoneThrough && waitSeconds < 60*3 {
connectionStopAvgNumerator[connection] += waitSeconds - 20
connectionStopAvgDenominator[connection]++
}
return nil
}
processTrip := func(trip *types.Trip) error {
if len(trip.StationUses) <= 1 {
// station visit or invalid trip
// can't extract any data about connections
return nil
}
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 this is a transfer, process it
if sourceUse.Type == types.Interchange {
transfer, err := types.GetTransfer(tx, sourceUse.Station.ID, sourceUse.SourceLine.ID, sourceUse.TargetLine.ID)
if err != nil {
// transfer might no longer exist (closed stations, etc.)
// move on
mainLog.Printf("%s: Transfer on %s from %s to %s skipped\n", trip.ID, sourceUse.Station.ID, sourceUse.SourceLine.ID, sourceUse.TargetLine.ID)
return nil
}
if err = processTransfer(transfer, sourceUse); err != nil {
return err
}
}
targetUse := trip.StationUses[useIdx+1]
if targetUse.Manual {
// manual path extensions don't contain valid time data
// skip
continue
}
connection, err := types.GetConnection(tx, sourceUse.Station.ID, targetUse.Station.ID, true)
if err != nil {
// connection might no longer exist (closed stations, etc.)
// move on
mainLog.Printf("%s: Connection from %s to %s skipped\n", trip.ID, sourceUse.Station.ID, targetUse.Station.ID)
continue
}
if useIdx+2 < len(trip.StationUses) && trip.StationUses[useIdx+2].EntryTime.Sub(targetUse.EntryTime) < 1*time.Second {
// this station use is certainly a forced extension to make up for a station the client did not capture correct times for
// skip
continue
}
if err = processConnection(connection, sourceUse, targetUse); err != nil {
return err
}
}
return nil
}
// instantiate each trip from DB individually
// (instead of using types.GetTrips)
// to reduce memory usage
for _, tripID := range tripIDs {
trip, err := types.GetTrip(tx, tripID)
if err != nil {
return err
}
if err = processTrip(trip); err != nil {
return err
}
if yieldFor > 0 {
time.Sleep(yieldFor)
}
}
for connection, denominator := range connectionAvgDenominator {
if denominator < 2 {
// data is not significant enough
continue
}
average := connectionAvgNumerator[connection] / denominator
connection.TypicalSeconds = int(math.Round(average))
mainLog.Printf("Updating connection from %s to %s with %d (%f)\n", connection.From.ID, connection.To.ID, connection.TypicalSeconds, denominator)
err := connection.Update(tx)
if err != nil {
return err
}
}
for connection, denominator := range connectionStopAvgDenominator {
if denominator < 2 {
// data is not significant enough
continue
}
average := connectionStopAvgNumerator[connection] / denominator
connection.TypicalStopSeconds = int(math.Round(average))
mainLog.Printf("Updating connection from %s to %s with stop %d (%f)\n", connection.From.ID, connection.To.ID, connection.TypicalStopSeconds, denominator)
err := connection.Update(tx)
if err != nil {
return err
}
}
for connection, denominator := range connectionWaitAvgDenominator {
if denominator < 2 {
// data is not significant enough
continue
}
average := connectionWaitAvgNumerator[connection] / denominator
connection.TypicalWaitingSeconds = int(math.Round(average))
mainLog.Printf("Updating connection from %s to %s with wait %d (%f)\n", connection.From.ID, connection.To.ID, connection.TypicalWaitingSeconds, denominator)
err := connection.Update(tx)
if err != nil {
return err
}
}
for transfer, denominator := range transferAvgDenominator {
if denominator < 2 {
// data is not significant enough
continue
}
average := transferAvgNumerator[transfer] / denominator
// subtract average of stop times, because the pathfinding algos can't
// deal with edges that have different weights depending on where one
// "comes from"
outgoingConnections := []*types.Connection{}
for connection := range connectionStopAvgDenominator {
if connection.From.ID == transfer.Station.ID {
outgoingConnections = append(outgoingConnections, connection)
}
}
outgoingDestConnections := []*types.Connection{}
for _, connection := range outgoingConnections {
lines, err := connection.To.Lines(tx)
if err != nil {
return err
}
for _, line := range lines {
if line.ID == transfer.To.ID {
outgoingDestConnections = append(outgoingDestConnections, connection)
break
}
}
}
avgStopTime := 0
for _, connection := range outgoingDestConnections {
avgStopTime += connection.TypicalStopSeconds
}
if len(outgoingDestConnections) > 0 {
average -= float64(avgStopTime) / float64(len(outgoingDestConnections))
}
transfer.TypicalSeconds = int(math.Round(average))
mainLog.Printf("Updating transfer from %s to %s with %d (%f)\n", transfer.From.ID, transfer.To.ID, transfer.TypicalSeconds, denominator)
err := transfer.Update(tx)
if err != nil {
return err
}
}
return tx.Commit()
}
// TypicalSecondsEntry makes the items of the result of ComputeTypicalSeconds
type TypicalSecondsEntry struct {
From string
To string
Weekday int
Hour int
Numerator float64
Denominator int64
}
// TypicalSecondsMinMax returns per-connection/transfer max and min of ComputeTypicalSeconds
type TypicalSecondsMinMax struct {
From string
To string
Max int64
Min int64
}
// TypicalSecondsByDowAndHour calculates TypicalSeconds per hour of day and week
func TypicalSecondsByDowAndHour(node sqalx.Node, startTime, endTime time.Time) (
connectionEntries, transferEntries []TypicalSecondsEntry,
connectionMinMax, transferMinMax []TypicalSecondsMinMax,
err error) {
tx, err := node.Beginx()
if err != nil {
return []TypicalSecondsEntry{}, []TypicalSecondsEntry{}, []TypicalSecondsMinMax{}, []TypicalSecondsMinMax{}, err
}
defer tx.Commit() // read-only tx
tripIDs, err := types.GetTripIDsBetween(tx, startTime, endTime)
if err != nil {
return []TypicalSecondsEntry{}, []TypicalSecondsEntry{}, []TypicalSecondsMinMax{}, []TypicalSecondsMinMax{}, err
}
mainLog.Printf("TypicalSecondsByDowAndHour: %d trip IDs\n", len(tripIDs))
// we can use pointers as keys in the following maps because types implements an internal cache
// that ensures the pointers to the transfers stay the same throughout this transaction
// (i.e. only one instance of each transfer is brought into memory)
transferAvgNumerator := make(map[*types.Transfer]map[time.Weekday]map[int]float64)
transferAvgDenominator := make(map[*types.Transfer]map[time.Weekday]map[int]int64)
transferMax := make(map[*types.Transfer]int64)
transferMin := make(map[*types.Transfer]int64)
// we can use pointers as keys in the following maps because types implements an internal cache
// that ensures the pointers to the transfers stay the same throughout this transaction
// (i.e. only one instance of each connection is brought into memory)
connectionAvgNumerator := make(map[*types.Connection]map[time.Weekday]map[int]float64)
connectionAvgDenominator := make(map[*types.Connection]map[time.Weekday]map[int]int64)
connectionMax := make(map[*types.Connection]int64)
connectionMin := make(map[*types.Connection]int64)
processTransfer := func(transfer *types.Transfer, use *types.StationUse) error {
seconds := use.LeaveTime.Sub(use.EntryTime).Seconds()
// if going from one line to another took more than 15 minutes,
// probably what really happened was that the client's clock was adjusted
// in the meantime, OR the user decided to go shop or something at the station
if seconds < 15*60 && seconds > 10 {
if transferAvgNumerator[transfer] == nil {
transferAvgNumerator[transfer] = make(map[time.Weekday]map[int]float64)
transferAvgDenominator[transfer] = make(map[time.Weekday]map[int]int64)
transferMax[transfer] = math.MinInt64
transferMin[transfer] = math.MaxInt64
}
if transferAvgNumerator[transfer][use.EntryTime.Weekday()] == nil {
transferAvgNumerator[transfer][use.EntryTime.Weekday()] = make(map[int]float64)
transferAvgDenominator[transfer][use.EntryTime.Weekday()] = make(map[int]int64)
}
transferAvgNumerator[transfer][use.EntryTime.Weekday()][use.EntryTime.Hour()] += seconds - 20
transferAvgDenominator[transfer][use.EntryTime.Weekday()][use.EntryTime.Hour()]++
if int64(math.Round(seconds-20)) > transferMax[transfer] {
transferMax[transfer] = int64(math.Round(seconds - 20))
}
if int64(math.Round(seconds-20)) < transferMin[transfer] {
transferMin[transfer] = int64(math.Round(seconds - 20))
}
}
return nil
}
processConnection := func(connection *types.Connection, sourceUse *types.StationUse, targetUse *types.StationUse) error {
seconds := targetUse.EntryTime.Sub(sourceUse.LeaveTime).Seconds()
waitSeconds := sourceUse.LeaveTime.Sub(sourceUse.EntryTime).Seconds()
// if going from one station to another took more than 15 minutes,
// probably what really happened was that the client's clock was adjusted
// in the meantime
if seconds < 15*60 && waitSeconds < 15*60 && (seconds > 10 || waitSeconds > 10) {
if connectionAvgNumerator[connection] == nil {
connectionAvgNumerator[connection] = make(map[time.Weekday]map[int]float64)
connectionAvgDenominator[connection] = make(map[time.Weekday]map[int]int64)
connectionMax[connection] = math.MinInt64
connectionMin[connection] = math.MaxInt64
}
if connectionAvgNumerator[connection][sourceUse.EntryTime.Weekday()] == nil {
connectionAvgNumerator[connection][sourceUse.EntryTime.Weekday()] = make(map[int]float64)
connectionAvgDenominator[connection][sourceUse.EntryTime.Weekday()] = make(map[int]int64)
}
connectionAvgNumerator[connection][sourceUse.EntryTime.Weekday()][sourceUse.EntryTime.Hour()] += seconds + waitSeconds
connectionAvgDenominator[connection][sourceUse.EntryTime.Weekday()][sourceUse.EntryTime.Hour()]++
if int64(math.Round(seconds+waitSeconds)) > connectionMax[connection] {
connectionMax[connection] = int64(math.Round(seconds + waitSeconds))
}
if int64(math.Round(seconds+waitSeconds)) < connectionMin[connection] {
connectionMin[connection] = int64(math.Round(seconds + waitSeconds))
}
}
return nil
}
processTrip := func(trip *types.Trip) error {
if len(trip.StationUses) <= 1 {
// station visit or invalid trip
// can't extract any data about connections
return nil
}
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 this is a transfer, process it
if sourceUse.Type == types.Interchange {
transfer, err := types.GetTransfer(tx, sourceUse.Station.ID, sourceUse.SourceLine.ID, sourceUse.TargetLine.ID)
if err != nil {
// transfer might no longer exist (closed stations, etc.)
// move on
mainLog.Printf("%s: Transfer on %s from %s to %s skipped\n", trip.ID, sourceUse.Station.ID, sourceUse.SourceLine.ID, sourceUse.TargetLine.ID)
return nil
}
if err = processTransfer(transfer, sourceUse); err != nil {
return err
}
}
targetUse := trip.StationUses[useIdx+1]
if targetUse.Manual {
// manual path extensions don't contain valid time data
// skip
continue
}
connection, err := types.GetConnection(tx, sourceUse.Station.ID, targetUse.Station.ID, true)
if err != nil {
// connection might no longer exist (closed stations, etc.)
// move on
mainLog.Printf("%s: Connection from %s to %s skipped\n", trip.ID, sourceUse.Station.ID, targetUse.Station.ID)
continue
}
if useIdx+2 < len(trip.StationUses) && trip.StationUses[useIdx+2].EntryTime.Sub(targetUse.EntryTime) < 1*time.Second {
// this station use is certainly a forced extension to make up for a station the client did not capture correct times for
// skip
continue
}
if err = processConnection(connection, sourceUse, targetUse); err != nil {
return err
}
}
return nil
}
// instantiate each trip from DB individually
// (instead of using types.GetTrips)
// to reduce memory usage
for i, tripID := range tripIDs {
trip, err := types.GetTrip(tx, tripID)
if err != nil {
return []TypicalSecondsEntry{}, []TypicalSecondsEntry{}, []TypicalSecondsMinMax{}, []TypicalSecondsMinMax{}, err
}
if err = processTrip(trip); err != nil {
return []TypicalSecondsEntry{}, []TypicalSecondsEntry{}, []TypicalSecondsMinMax{}, []TypicalSecondsMinMax{}, err
}
if i%5000 == 0 {
mainLog.Printf("TypicalSecondsByDowAndHour: processed %d of %d\n", i, len(tripIDs))
}
}
connections := []*types.Connection{}
for connection := range connectionAvgDenominator {
connections = append(connections, connection)
}
sort.Slice(connections, func(i, j int) bool {
if connections[i].From.ID == connections[j].From.ID {
return connections[i].To.ID < connections[j].To.ID
}
return connections[i].From.ID < connections[j].From.ID
})
connectionEntries = []TypicalSecondsEntry{}
connectionMinMax = []TypicalSecondsMinMax{}
for _, connection := range connections {
for weekday := time.Sunday; weekday <= time.Saturday; weekday++ {
if connectionAvgNumerator[connection][weekday] == nil {
connectionAvgNumerator[connection][weekday] = make(map[int]float64)
connectionAvgDenominator[connection][weekday] = make(map[int]int64)
}
for hour := 0; hour < 24; hour++ {
connectionEntries = append(connectionEntries, TypicalSecondsEntry{
From: connection.From.ID,
To: connection.To.ID,
Weekday: int(weekday),
Hour: hour,
Numerator: connectionAvgNumerator[connection][weekday][hour],
Denominator: connectionAvgDenominator[connection][weekday][hour],
})
}
}
connectionMinMax = append(connectionMinMax, TypicalSecondsMinMax{
From: connection.From.ID,
To: connection.To.ID,
Min: connectionMin[connection],
Max: connectionMax[connection],
})
}
transfers := []*types.Transfer{}
for transfer := range transferAvgDenominator {
transfers = append(transfers, transfer)
}
sort.Slice(transfers, func(i, j int) bool {
if transfers[i].From.ID == transfers[j].From.ID {
return transfers[i].To.ID < transfers[j].To.ID
}
return transfers[i].From.ID < transfers[j].From.ID
})
transferEntries = []TypicalSecondsEntry{}
for _, transfer := range transfers {
for weekday := time.Sunday; weekday <= time.Saturday; weekday++ {
if transferAvgNumerator[transfer][weekday] == nil {
transferAvgNumerator[transfer][weekday] = make(map[int]float64)
transferAvgDenominator[transfer][weekday] = make(map[int]int64)
}
for hour := 0; hour < 24; hour++ {
transferEntries = append(transferEntries, TypicalSecondsEntry{
From: transfer.From.ID,
To: transfer.To.ID,
Weekday: int(weekday),
Hour: hour,
Numerator: transferAvgNumerator[transfer][weekday][hour],
Denominator: transferAvgDenominator[transfer][weekday][hour],
})
}
}
transferMinMax = append(transferMinMax, TypicalSecondsMinMax{
From: transfer.From.ID,
To: transfer.To.ID,
Min: transferMin[transfer],
Max: transferMax[transfer],
})
}
return connectionEntries, transferEntries, connectionMinMax, transferMinMax, nil
}