-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
402 lines (337 loc) · 10.3 KB
/
main.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
package main
import (
"context"
"database/sql"
"encoding/json"
"flag"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/transitreport/gooctranspoapi"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
type calendarrow struct {
service_id string
monday string
tuesday string
wednesday string
thursday string
friday string
saturday string
sunday string
start_date string
end_date string
}
type calendadaterow struct {
service_id string
date string
exception_type string
}
type RouteTime struct {
RouteShortName string
RouteID string
DirectionID string
TripID string
TripHeadsign string
ArrivalTime string
StopCode string
StopLat string
StopLon string
}
type Export struct {
Routetime *RouteTime
NextTripsForStop *gooctranspoapi.NextTripsForStop
RequestedAt time.Time
}
const (
// EnvPrefix is the prefix for the environment variables.
EnvPrefix string = "EXAMINER_"
)
var (
id = flag.String("id", "", "Application ID")
key = flag.String("key", "", "Application Key")
db = flag.String("db", "gtfs.db", "The file path for the sqlite3 db of GTFS data")
v = flag.Bool("v", false, "Verbose output")
// A version flag, which should be overwritten when building using ldflags.
version = "devel"
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Examiner\nVersion %v\n\n", version)
fmt.Fprintln(os.Stderr, "A tool for analyzing transit arrival times vs scheduled arrival times.")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, " The possible environment variables:")
flag.VisitAll(func(f *flag.Flag) {
uppercaseName := strings.ToUpper(f.Name)
fmt.Fprintf(os.Stderr, " %v%v\n", EnvPrefix, uppercaseName)
})
}
}
func main() {
// Process the flags.
flag.Parse()
// If any flags have not been set, see if there are
// environment variables that set them.
overrideUnsetFlagsFromEnvironmentVariables()
// If any of the required flags are not set, exit.
if *id == "" {
log.Fatal("FATAL: An Application ID is required.")
} else if *key == "" {
log.Fatal("FATAL: An Application Key is required.")
}
wd, err := os.Getwd()
if err != nil {
log.Fatalf("Could not find working directory, %v", err)
}
if !filepath.IsAbs(*db) {
*db = filepath.Join(wd, *db)
}
stopTimes, err := getStopTimes(*db)
if err != nil {
log.Fatalf("Error querying for stop times, %v", err)
}
c := gooctranspoapi.NewConnection(*id, *key)
var wg sync.WaitGroup
addedToQueue := 0
for _, stopTime := range stopTimes {
arrivalDay := time.Now()
hourAsInt, err := strconv.Atoi(stopTime.ArrivalTime[:2])
if err != nil {
log.Fatal(err)
}
if hourAsInt > 23 {
hourAsInt = hourAsInt - 24
stopTime.ArrivalTime = fmt.Sprintf("0%v:%v", hourAsInt, stopTime.ArrivalTime[3:])
arrivalDay = arrivalDay.AddDate(0, 0, 1)
}
zone, _ := arrivalDay.Zone()
arrival, err := time.Parse("2006-01-02 15:04:05 MST", arrivalDay.Format("2006-01-02 ")+stopTime.ArrivalTime+" "+zone)
if err != nil {
log.Fatal(err)
}
checkAt := arrival.Add(-time.Minute * 5)
if *v {
log.Printf("%v, %v, %v, %v, check at %v\n", stopTime.RouteShortName, stopTime.TripHeadsign, stopTime.StopCode, stopTime.ArrivalTime, checkAt)
}
if time.Now().Before(checkAt) {
addedToQueue++
wg.Add(1)
// Launch a goroutine to fetch the URL.
go func(waitUntil time.Time, stopTime RouteTime) {
defer wg.Done()
waitDur := waitUntil.Sub(time.Now())
time.Sleep(waitDur)
requestedAt := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
nextTrips, err := c.GetNextTripsForStop(ctx, stopTime.RouteShortName, stopTime.StopCode)
if err != nil {
log.Printf("API Error: %v\n", err)
log.Printf("API Error: StopTime - %#v\n", stopTime)
return
}
ex := Export{
Routetime: &stopTime,
NextTripsForStop: nextTrips,
RequestedAt: requestedAt,
}
json, err := json.MarshalIndent(ex, "", " ")
if err != nil {
log.Printf("JSON Marshal Error: %v\n", err)
return
}
err = ioutil.WriteFile(fmt.Sprintf("%v_%v_%v.json", requestedAt.Format("2006-01-02-150405"), stopTime.RouteShortName, stopTime.StopCode), json, 0644)
if err != nil {
log.Printf("WriteFile Error: %v\n", err)
}
}(checkAt, stopTime)
}
if addedToQueue >= 10000 {
break
}
}
if *v {
log.Printf("%v future API calls added to queue...\n", addedToQueue)
}
wg.Wait()
}
func getStopTimes(dbfilepath string) ([]RouteTime, error) {
routetimes := []RouteTime{}
todayServices, err := getTodaysServices(dbfilepath)
if err != nil {
return routetimes, err
}
db, err := sql.Open("sqlite3", dbfilepath)
if err != nil {
return routetimes, err
}
defer db.Close()
ts := make([]interface{}, len(todayServices))
for i, v := range todayServices {
ts[i] = v
}
routetimequery := `
SELECT routes.route_short_name, trips.route_id, trips.direction_id, trips.trip_id, trips.trip_headsign, stop_times.arrival_time, stops.stop_code, stops.stop_lat, stops.stop_lon
FROM trips
LEFT JOIN stop_times
ON stop_times.trip_id = trips.trip_id
LEFT JOIN routes
ON routes.route_id = trips.route_id
LEFT JOIN stops
ON stops.stop_id = stop_times.stop_id
WHERE trips.service_id IN ` + "(?" + strings.Repeat(",?", len(ts)-1) + ")" + `
AND stop_times.stop_sequence != 1
AND stop_times.pickup_type == 0
ORDER BY RANDOM()`
routetimerows, err := db.Query(routetimequery, ts...)
if err != nil {
return routetimes, err
}
defer routetimerows.Close()
for routetimerows.Next() {
var curRow RouteTime
err = routetimerows.Scan(&curRow.RouteShortName, &curRow.RouteID, &curRow.DirectionID, &curRow.TripID, &curRow.TripHeadsign, &curRow.ArrivalTime, &curRow.StopCode, &curRow.StopLat, &curRow.StopLon)
if err != nil {
return routetimes, err
}
routetimes = append(routetimes, curRow)
}
err = routetimerows.Err()
if err != nil {
return routetimes, err
}
return routetimes, nil
}
func getTodaysServices(dbfilepath string) ([]string, error) {
db, err := sql.Open("sqlite3", dbfilepath)
if err != nil {
return []string{}, err
}
defer db.Close()
today := time.Now()
weekday := today.Weekday()
todayServices := []string{}
calRows, err := db.Query("SELECT service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date FROM calendar")
if err != nil {
return []string{}, err
}
defer calRows.Close()
for calRows.Next() {
var curRow calendarrow
err = calRows.Scan(&curRow.service_id, &curRow.monday, &curRow.tuesday, &curRow.wednesday, &curRow.thursday, &curRow.friday, &curRow.saturday, &curRow.sunday, &curRow.start_date, &curRow.end_date)
if err != nil {
return []string{}, err
}
start, err := time.ParseInLocation("20060102", curRow.start_date, today.Location())
if err != nil {
return []string{}, err
}
end, err := time.ParseInLocation("20060102", curRow.end_date, today.Location())
if err != nil {
return []string{}, err
}
end = end.Add(time.Hour * 23)
end = end.Add(time.Minute * 59)
end = end.Add(time.Second * 59)
if today.After(start) && today.Before(end) {
switch weekday {
case time.Monday:
if curRow.monday == "1" {
todayServices = append(todayServices, curRow.service_id)
}
case time.Tuesday:
if curRow.tuesday == "1" {
todayServices = append(todayServices, curRow.service_id)
}
case time.Wednesday:
if curRow.wednesday == "1" {
todayServices = append(todayServices, curRow.service_id)
}
case time.Thursday:
if curRow.thursday == "1" {
todayServices = append(todayServices, curRow.service_id)
}
case time.Friday:
if curRow.friday == "1" {
todayServices = append(todayServices, curRow.service_id)
}
case time.Saturday:
if curRow.saturday == "1" {
todayServices = append(todayServices, curRow.service_id)
}
case time.Sunday:
if curRow.sunday == "1" {
todayServices = append(todayServices, curRow.service_id)
}
}
}
}
err = calRows.Err()
if err != nil {
return []string{}, err
}
exceptions, err := db.Query("SELECT service_id,date,exception_type FROM calendar_dates WHERE date = ?", today.Format("20060102"))
if err != nil {
return []string{}, err
}
defer exceptions.Close()
for exceptions.Next() {
var curRow calendadaterow
err = exceptions.Scan(&curRow.service_id, &curRow.date, &curRow.exception_type)
if err != nil {
return []string{}, err
}
if curRow.exception_type == "2" {
for i, service_id := range todayServices {
if service_id == curRow.service_id {
todayServices = append(todayServices[:i], todayServices[i+1:]...)
break
}
}
}
}
err = exceptions.Err()
if err != nil {
return []string{}, err
}
return todayServices, nil
}
// If any flags are not set, use environment variables to set them.
func overrideUnsetFlagsFromEnvironmentVariables() {
// A map of pointers to unset flags.
listOfUnsetFlags := make(map[*flag.Flag]bool)
// flag.Visit calls a function on "only those flags that have been set."
// flag.VisitAll calls a function on "all flags, even those not set."
// No way to ask for "only unset flags". So, we add all, then
// delete the set flags.
// First, visit all the flags, and add them to our map.
flag.VisitAll(func(f *flag.Flag) { listOfUnsetFlags[f] = true })
// Then delete the set flags.
flag.Visit(func(f *flag.Flag) { delete(listOfUnsetFlags, f) })
// Loop through our list of unset flags.
// We don't care about the values in our map, only the keys.
for k := range listOfUnsetFlags {
// Build the corresponding environment variable name for each flag.
uppercaseName := strings.ToUpper(k.Name)
environmentVariableName := fmt.Sprintf("%v%v", EnvPrefix, uppercaseName)
// Look for the environment variable name.
// If found, set the flag to that value.
// If there's a problem setting the flag value,
// there's a serious problem we can't recover from.
environmentVariableValue := os.Getenv(environmentVariableName)
if environmentVariableValue != "" {
err := k.Value.Set(environmentVariableValue)
if err != nil {
log.Fatalf("FATAL: Unable to set configuration option %v from environment variable %v, "+
"which has a value of \"%v\"",
k.Name, environmentVariableName, environmentVariableValue)
}
}
}
}