-
Notifications
You must be signed in to change notification settings - Fork 1
/
winner.go
296 lines (258 loc) · 6.92 KB
/
winner.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
package main
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strconv"
"time"
"log"
)
const (
options string = "OPTIONS"
allowOrigin string = "Access-Control-Allow-Origin"
allowMethods string = "Access-Control-Allow-Methods"
allowHeaders string = "Access-Control-Allow-Headers"
allowCredentials string = "Access-Control-Allow-Credentials"
exposeHeaders string = "Access-Control-Expose-Headers"
credentials string = "true"
origin string = "Origin"
methods string = "POST, GET, OPTIONS, PUT, DELETE, HEAD, PATCH"
// If you want to expose some other headers add it here
headers string = "Accept, Accept-Encoding, Authorization, Content-Length, Content-Type, X-CSRF-Token"
)
// Handler will allow cross-origin HTTP requests
func cors(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set allow origin to match origin of our request or fall back to *
if o := r.Header.Get(origin); o != "" {
w.Header().Set(allowOrigin, o)
} else {
w.Header().Set(allowOrigin, "*")
}
// Set other headers
w.Header().Set(allowHeaders, headers)
w.Header().Set(allowMethods, methods)
w.Header().Set(allowCredentials, credentials)
w.Header().Set(exposeHeaders, headers)
// If this was preflight options request let's write empty ok response and return
if r.Method == options {
w.WriteHeader(http.StatusOK)
w.Write(nil)
return
}
next.ServeHTTP(w, r)
})
}
type event struct {
ID string `json:"id"`
Date start `json:"start"`
}
type start struct {
Utc string `json:"utc"`
}
type lastRequest struct {
Events []event `json:"events"`
}
type profile struct {
LastName string `json:"first_name"`
FirstName string `json:"last_name"`
}
type attendee struct {
Profile profile `json:"profile"`
}
type pagination struct {
PageNumber int `json:"page_number"`
PageCount int `json:"page_count"`
}
type eventAttend struct {
Attendees []attendee `json:"attendees"`
Pagination pagination `json:"pagination"`
}
func isPresent(index int, randoms []int) bool {
for i := 0; i < len(randoms); i++ {
if randoms[i] == index {
return true
}
}
return false
}
func getRandoms(nbWinner int) []int {
count := nbWinner
var randoms = []int{}
for count != 0 {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
index := r1.Intn(nbWinner)
exist := isPresent(index, randoms)
if !exist {
count--
randoms = append(randoms, index)
}
}
return randoms
}
func getWinners(attendees []attendee, nbWinner int) []profile {
var winnersProfile = []profile{}
randoms := getRandoms(len(attendees))
for nbWinner != 0 {
winnersProfile = append(winnersProfile, attendees[randoms[nbWinner-1]].Profile)
nbWinner--
}
return winnersProfile
}
func preFetchWinner(nbWinner int, result []attendee) (string, error) {
winnersProfile := getWinners(result, nbWinner)
podium, _ := json.Marshal(winnersProfile)
return string(podium), nil
}
func winner(chanAttendees chan []attendee) func(http.ResponseWriter, *http.Request) {
var attendees []attendee
var winnerPreFetch = make([]string, 10)
go func() {
for {
attendees = <- chanAttendees
for i := 1; i < 10; i++ {
winnerPreFetch[i], _ = preFetchWinner(i, attendees)
}
}
}()
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
nbWinnerS := r.URL.Query().Get("nb")
if len(nbWinnerS) == 0 {
http.Error(w, "bad Request", http.StatusBadRequest)
return
}
nbWinner, err := strconv.Atoi(nbWinnerS)
if err != nil {
http.Error(w, "bad Request", http.StatusBadRequest)
return
}
if nbWinner < int(0) {
http.Error(w, "request < 0 ", http.StatusBadRequest)
return
}
if nbWinner >= len(attendees) || nbWinner > len(winnerPreFetch) - 1 {
n := nbWinner
if nbWinner >= len(attendees) {
n = len(attendees)
}
winnersProfile := getWinners(attendees, n)
totalattendees, _ := json.Marshal(winnersProfile)
io.WriteString(w, string(totalattendees))
} else {
io.WriteString(w, winnerPreFetch[nbWinner])
}
chanAttendees <- attendees
}
}
func getLastEvent() (*string, *time.Time, error) {
token := os.Getenv("TOKEN")
orgaID := "1464915124"
resp, err := http.Get("https://www.eventbriteapi.com/v3/events/search/?token=" + token + "&organizer.id=" + orgaID)
if err != nil {
return nil,nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil,nil, err
}
var request lastRequest
if err := json.Unmarshal(body, &request); err != nil {
return nil,nil, err
}
if len(request.Events) == 0 {
return nil,nil, errors.New("No event found")
}
date, err := time.Parse(time.RFC3339,request.Events[0].Date.Utc)
log.Println(date)
return &request.Events[0].ID, &date, nil
}
func getAttendees(lastEventID string) ([]attendee, error){
token := os.Getenv("TOKEN")
var i = 1
var result = []attendee{}
for {
log.Println("Fetch attendees")
log.Println("i:"+strconv.Itoa(i))
resp, err := http.Get("https://www.eventbriteapi.com/v3/events/" + lastEventID + "/attendees/?page=" + strconv.Itoa(i) + "&token=" + token)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
res := eventAttend{}
err = json.Unmarshal(body, &res)
if err != nil {
return nil, err
}
result = append(result, res.Attendees...)
log.Println("Page Count:" + strconv.Itoa(res.Pagination.PageCount))
log.Println("PageNumber:" + strconv.Itoa(res.Pagination.PageNumber))
if i == res.Pagination.PageCount {
break
}
i++
}
return result, nil
}
func main() {
mux := http.NewServeMux()
lastEventId,lastEventDate, err := getLastEvent()
if err != nil {
log.Println(err)
}
log.Println("Event id: " + *lastEventId + " / Event date : "+ lastEventDate.String())
newEventID := make(chan string)
chanNewAttendees := make(chan []attendee)
go func () {
var eventID string
for {
var listAttendee []attendee
var err error
select {
case n := <-newEventID:
eventID = n
listAttendee, err = getAttendees(eventID)
if err != nil {
log.Println(err)
}
log.Println("List attendees : ")
log.Println(listAttendee)
case <-time.After(1 * time.Hour):
listAttendee, err = getAttendees(eventID)
if err != nil {
log.Println(err)
}
log.Println("List attendees : ")
log.Println(listAttendee)
}
chanNewAttendees <- listAttendee
}
}()
newEventID <- *lastEventId
go func() {
for range time.Tick(24 * time.Hour){
newLastEventId,lastEventDate, err := getLastEvent()
if err != nil {
log.Println(err)
}
log.Println(*lastEventId + " "+ lastEventDate.String())
if *newLastEventId != *lastEventId {
newEventID <- *newLastEventId
}
}
}()
mux.HandleFunc("/winners", winner(chanNewAttendees))
if err := http.ListenAndServe(":8000", cors(mux)); err != nil {
log.Println(err)
}
}