-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopCtrl.go
164 lines (112 loc) · 5.21 KB
/
stopCtrl.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
package controllers
////////////////////////////////////////////////////////////////////////////////////////////////
/// Imports
////////////////////////////////////////////////////////////////////////////////////////////////
import (
"log"
"time"
"sort"
"net/http"
"encoding/json"
"github.com/gorilla/mux"
"github.com/fatih/stopwatch"
"github.com/helyx-io/commute-api/utils"
"github.com/helyx-io/commute-api/models"
"github.com/helyx-io/commute-api/services"
)
////////////////////////////////////////////////////////////////////////////////////////////////
/// Structures
////////////////////////////////////////////////////////////////////////////////////////////////
type StopController struct {
ss *services.StopService
}
////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructor
////////////////////////////////////////////////////////////////////////////////////////////////
func NewStopController(ss *services.StopService, r *mux.Router) *StopController {
sc := StopController{ss}
// Init Router
r.HandleFunc("/{date}/nearest", sc.NearestStops)
r.HandleFunc("/{date}/{stopId}", sc.StopById)
return &sc
}
////////////////////////////////////////////////////////////////////////////////////////////////
/// Controller functions
////////////////////////////////////////////////////////////////////////////////////////////////
func (sc *StopController) NearestStops(w http.ResponseWriter, r *http.Request) {
defer utils.RecoverFromError(w)
sw := stopwatch.Start(0)
params := mux.Vars(r)
agencyKey := params["agencyKey"]
date := params["date"]
lat := r.URL.Query().Get("lat")
lon := r.URL.Query().Get("lon")
distance := r.URL.Query().Get("distance")
if len(distance) <= 0 {
distance = "1000"
}
timeOfDay := time.Now().Format("15:04:05")
// log.Printf("Agency Key: %s", agencyKey)
// log.Printf("Lat: %s", lat)
// log.Printf("Lon: %s", lon)
// log.Printf("Distance: %s", distance)
// log.Printf("Date: %s", date)
log.Printf("Fetching stops by date ...")
stops := sc.ss.FetchStopsByDate(agencyKey, date, timeOfDay, lat, lon, distance, 3)
log.Printf("Stops[%d] %v", len(stops), stops)
log.Printf("Extracting Trip Ids ...")
tripIds := sc.ss.ExtractTripIds(stops)
log.Printf("TripIds[%d] %v", len(tripIds), tripIds)
log.Printf("Fetching First And Last StopNames By Trip Ids ...")
flStopNamesByTripId := sc.ss.FetchFirstAndLastStopNamesByTripIds(agencyKey, tripIds)
log.Printf("Merge First and Last StopNames By TripId With Stop Routes ...")
sc.ss.MergeFlStopNamesByTripIdWithStopRoutes(&stops, flStopNamesByTripId)
stopGroups := stops.ToStopGroups()
sort.Sort(models.StopGroupByDistance(stopGroups))
jsm, err := json.Marshal(stopGroups.ToJsonStopGroups())
utils.FailOnError(err, "Failed on marshalling json stops")
w.Header().Set("Content-Type", "application/json");
w.Header().Set("X-Response-Time", sw.ElapsedTime().String())
w.Write(jsm)
log.Printf("-----------------------------------------------------------------------------------")
log.Printf("--- Nearest stops. ElapsedTime: %v", sw.ElapsedTime())
log.Printf("-----------------------------------------------------------------------------------")
}
func (sc *StopController) StopById(w http.ResponseWriter, r *http.Request) {
defer utils.RecoverFromError(w)
sw := stopwatch.Start(0)
params := mux.Vars(r)
agencyKey := params["agencyKey"]
date := params["date"]
stopId := params["stopId"]
limit := r.URL.Query().Get("limit")
if len(limit) <= 0 {
limit = "3"
}
timeOfDay := time.Now().Format("15:04:05")
// log.Printf("Agency Key: %s", agencyKey)
// log.Printf("Lat: %s", lat)
// log.Printf("Lon: %s", lon)
// log.Printf("Distance: %s", distance)
// log.Printf("Date: %s", date)
// log.Printf("Fetching stops by date ...")
stops := sc.ss.FetchStopById(agencyKey, date, timeOfDay, stopId, 3)
// log.Printf("Stops[%d] %v", len(stops), stops)
// log.Printf("Extracting Trip Ids ...")
tripIds := sc.ss.ExtractTripIds(stops)
// log.Printf("TripIds[%d] %v", len(tripIds), tripIds)
// log.Printf("Fetching First And Last StopNames By Trip Ids ...")
flStopNamesByTripId := sc.ss.FetchFirstAndLastStopNamesByTripIds(agencyKey, tripIds)
// log.Printf("Merge First and Last StopNames By TripId With Stop Routes ...")
sc.ss.MergeFlStopNamesByTripIdWithStopRoutes(&stops, flStopNamesByTripId)
stopGroups := stops.ToStopGroups()
sort.Sort(models.StopGroupByDistance(stopGroups))
jsm, err := json.Marshal(stopGroups.ToJsonStopGroups())
utils.FailOnError(err, "Failed on marshalling json stops")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Response-Time", sw.ElapsedTime().String())
w.Write(jsm)
log.Printf("-----------------------------------------------------------------------------------")
log.Printf("--- Stop by Id: '%s'. ElapsedTime: %v", stopId, sw.ElapsedTime())
log.Printf("-----------------------------------------------------------------------------------")
}