-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (142 loc) · 3.81 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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/benjaminkomen/SchipholFlightAnalyser/outbound/schipholClient"
"github.com/go-echarts/go-echarts/charts"
"log"
"net/http"
"os"
"sort"
"time"
)
func main() {
http.HandleFunc("/api/flights", flightHandler)
http.HandleFunc("/", flightChartHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
srv := &http.Server{
Addr: fmt.Sprintf("127.0.0.1:%s", port),
WriteTimeout: 600 * time.Second,
ReadTimeout: 600 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
func flightChartHandler(w http.ResponseWriter, r *http.Request) {
data, err := getFlightChartData(r.Context())
log.Printf("Obtained chart data")
sortedData := sortMapByKey(data)
log.Printf("Sorted chart data")
if err != nil {
log.Printf("error getting flight chart data: %s", err)
w.WriteHeader(500)
return
}
var nameItems []int
var departingFlights []int
var arrivingFlights []int
for _, dataEntry := range sortedData {
nameItems = append(nameItems, dataEntry.Key)
departingFlights = append(departingFlights, dataEntry.Value.departCount)
arrivingFlights = append(arrivingFlights, dataEntry.Value.arrivingCount)
}
log.Printf("Start drawing chart")
bar := charts.NewBar()
bar.SetGlobalOptions(charts.TitleOpts{Title: "Flights per hour of the day"})
bar.AddXAxis(nameItems)
bar.AddYAxis("departing", departingFlights, charts.BarOpts{}).
AddYAxis("arriving", arrivingFlights)
f, err := os.Create("html/bar.html")
if err != nil {
log.Println(err)
}
log.Printf("Wrote chart data to html")
err = bar.Render(w, f)
if err != nil {
log.Printf("error rendering chart: %s", err)
w.WriteHeader(500)
return
}
}
type Pair struct {
Key int
Value FlightData
}
type PairList []Pair
func (p PairList) Len() int { return len(p) }
func (p PairList) Less(i, j int) bool { return p[i].Key < p[j].Key }
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func sortMapByKey(data map[int]FlightData) PairList {
pairList := make(PairList, len(data))
i := 0
for k, v := range data {
pairList[i] = Pair{k, v}
i++
}
sort.Sort(pairList)
return pairList
}
func flightHandler(w http.ResponseWriter, r *http.Request) {
var client = schipholClient.New()
flights, err := client.GetFlights(r.Context(), "D", "2019-11-22")
if err != nil {
log.Printf("error obtaining flights: %s", err)
w.WriteHeader(500)
return
}
err = json.NewEncoder(w).Encode(flights)
if err != nil {
log.Printf("error encoding flights to json: %s", err)
w.WriteHeader(500)
return
}
}
type FlightData struct {
departCount int
arrivingCount int
}
func (fd *FlightData) append(departCount int, arrivingCount int) {
if departCount != 0 {
fd.departCount += 1
}
if arrivingCount != 0 {
fd.arrivingCount += 1
}
}
func getFlightChartData(c context.Context) (map[int]FlightData, error) {
results := make(map[int]FlightData, 24)
var client = schipholClient.New()
departingFlights, err := client.GetFlights(c, "D", "2019-11-22")
if err != nil {
log.Printf("error obtaining departing flights: %s", err)
return nil, err
}
for _, flight := range departingFlights {
if flight.ActualOffBlockTime.IsZero() {
continue // we skip results with no reliable data
}
hour := flight.ActualOffBlockTime.Hour()
data := results[hour]
data.append(hour, 0)
results[hour] = data
}
arrivingFlights, err := client.GetFlights(c, "A", "2019-11-22")
if err != nil {
log.Printf("error obtaining arriving flights: %s", err)
return nil, err
}
for _, flight := range arrivingFlights {
if flight.ActualLandingTime.IsZero() {
continue // we skip results with no reliable data
}
hour := flight.ActualLandingTime.Hour()
data := results[hour]
data.append(0, hour)
results[hour] = data
}
return results, nil
}