forked from mnishizawa/boss2016-rushmove-votegenerator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
212 lines (169 loc) · 5.43 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
package main
import (
"fmt"
"time"
rd "github.com/Pallinder/go-randomdata"
"encoding/json"
"sync"
"net/http"
"bytes"
)
func main() {
var wg sync.WaitGroup
wg.Add(50)
fmt.Println("Opening Eastern Timezone polls at ", time.Now())
//Eastern
go openPoll("Connecticut", 3592053, 15, &wg)
go openPoll("Delaware", 917060, 25, &wg)
go openPoll("Florida", 19361792, -5, &wg)
go openPoll("Georgia", 9907756, -5, &wg)
go openPoll("Indiana", 6542411, -15, &wg)
go openPoll("Maine", 1328535, 10, &wg)
go openPoll("Maryland", 5887776, 45, &wg)
go openPoll("Massachusetts", 6657291, 35, &wg)
go openPoll("Michigan", 9889024, 5, &wg)
go openPoll("NewHampshire", 1321069, 5, &wg)
go openPoll("NewJersey", 8874374, 25, &wg)
go openPoll("NewYork", 19594330, 30, &wg)
go openPoll("NorthCarolina", 9750405, 15, &wg)
go openPoll("Ohio", 11560380, -5, &wg)
go openPoll("Pennsylvania", 12758729, 10, &wg)
go openPoll("RhodeIsland", 1053252, 25, &wg)
go openPoll("SouthCarolina", 4727273, 0, &wg)
go openPoll("Vermont", 626358, 5, &wg)
go openPoll("Virginia", 8185131, 5, &wg)
go openPoll("WestVirginia", 1853881, -25, &wg)
time.Sleep(time.Second * 120) //sleep, seconds to minutes
fmt.Println("Opening Central Timezone polls at ", time.Now())
//Central
go openPoll("Alabama", 4817678, -10, &wg)
go openPoll("Arkansas", 2947036, -5, &wg)
go openPoll("Illinois", 12868747, 5, &wg)
go openPoll("Iowa", 3078116, -15, &wg)
go openPoll("Kansas", 2882946, -25, &wg)
go openPoll("Kentucky", 4383272, -15, &wg)
go openPoll("Louisiana", 4601049, 0, &wg)
go openPoll("Minnesota", 5383661, -10, &wg)
go openPoll("Mississippi", 2984345, -5, &wg)
go openPoll("Missouri", 6028076, -15, &wg)
go openPoll("Nebraska", 1855617, -10, &wg)
go openPoll("NorthDakota", 704925, -10, &wg)
go openPoll("Oklahoma", 3818851, -10, &wg)
go openPoll("SouthDakota", 834708, -45, &wg)
go openPoll("Tennessee", 6451365, -20, &wg)
go openPoll("Texas", 26092033, -25, &wg)
go openPoll("Wisconsin", 5724692, 20, &wg)
time.Sleep(time.Second * 120) //sleep, seconds to minutes
fmt.Println("Opening Mountain Timezone polls at ", time.Now())
//Mountain
go openPoll("Arizona", 6561516, -5, &wg)
go openPoll("Colorado", 5197580, 5, &wg)
go openPoll("Idaho", 1599464, -10, &wg)
go openPoll("Montana", 1006370, -10, &wg)
go openPoll("NewMexico", 2080085, 0, &wg)
go openPoll("Utah", 2858111, -35, &wg)
time.Sleep(time.Second * 120) //sleep, seconds to minutes
fmt.Println("Opening Pacific Timezone polls at ", time.Now())
//Pacific
go openPoll("California", 38066920, 15, &wg)
go openPoll("Nevada", 2761584, 0, &wg)
go openPoll("Oregon", 3900343, -15, &wg)
go openPoll("Washington", 6899123, 15, &wg)
go openPoll("Wyoming", 575251, -30, &wg)
time.Sleep(time.Second * 120) //sleep, seconds to minutes
fmt.Println("Opening Alaska polls at ", time.Now())
go openPoll("Alaska", 728300, 0, &wg)
time.Sleep(time.Second * 120) //sleep, seconds to minutes
fmt.Println("Opening Hawaii polls at ", time.Now())
go openPoll("Hawaii", 1392704, 0, &wg)
wg.Wait()
}
//run this as a goroutine
func openPoll(location string, population int, weight int, wg *sync.WaitGroup) {
defer wg.Done()
pool := generateVotingPool(weight)
// set participation at 65%
voters := int(float32(population) * .65)
var move, stay int
for i := 0; i < voters; i++ {
randomIdx := rd.Number(0, 99)
choice := pool[randomIdx]
if choice {
move++
} else {
stay++
}
ballot := Ballot{ Choice: choice, Location: location, Time: time.Now().Unix() }
sendToFlume(ballot)
}
movePct := (float32(move) / float32(voters)) * float32(100)
stayPct := (float32(stay) / float32(voters)) * float32(100)
fmt.Println("Time: ", time.Now(),"[", location, "]\n\t",movePct, "% to MOVE\n\t",stayPct, "% to STAY")
}
func sendToFlume(ballot Ballot) error {
url := "http://45.31.163.169:4444/"
ballotJson, _ := json.Marshal(ballot)
jsonEnvelope := JsonBody{Body: string(ballotJson)}
newPayload := [1]JsonBody {jsonEnvelope}
json, err := json.Marshal(newPayload)
if err != nil {
fmt.Println("ERROR generating a ballot")
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(json))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
if rsp, err := client.Do(req); err != nil {
return err
} else {
defer rsp.Body.Close()
if rsp.StatusCode != 200 {
fmt.Println("Failure on request ",rsp.StatusCode, string(json))
}
}
return nil
}
type PollTime struct {
start time.Time
}
func (pt *PollTime) mark() {
current := (time.Now().Sub(pt.start)).Nanoseconds() + 8
fmt.Println("Current time: ", current)
}
func generateVotingPool(weight int) ChoicePool {
choicePool := ChoicePool{}
setTo := weight < 0
if !setTo {
//if we are setting to false, flip all the intial values to true
for i := range choicePool {
choicePool[i] = true
}
// need to reverse sign since we are reversing initial values
weight = weight * -1
}
distribution := 50 + weight
if distribution >= 100 || distribution <= 0 {
return choicePool
}
for i := 0; i < distribution; i++ {
idx := rd.Number(0, 99)
if choicePool[idx] != setTo {
choicePool[idx] = setTo
} else {
//decrement and try again if the value has already been set
i--
}
}
return choicePool;
}
type ChoicePool [100]bool
type JsonBody struct {
Body string `json:"body"`
}
type Ballot struct {
Choice bool `json:"choice"`
Location string `json:"location"`
Time int64 `json:"time"`
}