-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (106 loc) · 2.84 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"sync"
"time"
"github.com/charmbracelet/log"
)
var (
_config string
_transfer string
waitForSummons sync.WaitGroup
waitForVoters sync.WaitGroup
)
type (
Ranges struct {
Responded float32 `json:"responded"`
Excused float32 `json:"excused"`
Deferred float32 `json:"deferred"`
Disqualified float32 `json:"disqualified"`
Undeliverable float32 `json:"undeliverable"`
}
Trials struct {
Rooms int `json:"rooms"`
Judges int `json:"judges"`
Total int `json:"total"`
}
Config struct {
LocCode []string `json:"location_code"`
PostCodes map[string]string `json:"postcodes"`
Pools int `json:"pools"`
Voters int `json:"voters"`
Reset bool `json:"reset"`
VotersPerPool int `json:"voters_per_pool"`
Summon bool `json:"summon"`
AddResponses bool `json:"add_responses"`
DaysToAdd int `json:"days_to_add"`
Ranges Ranges `json:"ranges"`
Trials Trials `json:"trials"`
}
)
func main() {
start := time.Now()
flag.StringVar(&_config, "c", "", "Use a config file")
flag.StringVar(&_transfer, "t", "", "Transfer pools or jurors")
flag.Parse()
_ = log.New(os.Stderr)
log.SetLevel(log.DebugLevel)
if _config == "" {
log.Error("No config file provided")
os.Exit(1)
}
c, err := os.ReadFile(_config)
if err != nil {
log.Error("Could not read config file")
os.Exit(1)
}
log.Info("Starting the data gen")
// do health check
// if health check fails, exit
if _, err := request("GET", healthCheckUrl.String(), nil, false); err != nil {
log.Errorf("Errored out on health check: %s", err.Error())
os.Exit(1)
}
config := Config{}
json.Unmarshal(c, &config)
if (config.Ranges.Responded + config.Ranges.Excused + config.Ranges.Deferred + config.Ranges.Disqualified + config.Ranges.Undeliverable) > 1.01 {
log.Error("Response cannot total over 100%")
os.Exit(1)
}
for _, locCode := range config.LocCode {
if len(locCode) != 3 {
log.Error("All location codes must be 3 characters")
os.Exit(1)
}
}
db := New(config.Reset)
if config.Voters > 0 {
for _, locCode := range config.LocCode {
waitForVoters.Add(1)
v := voters(db.db, locCode, config.Voters, config.PostCodes[locCode])
go v.insert()
}
waitForVoters.Wait()
}
if config.Pools > 0 {
p := pools(db.db, &config)
p.request()
if config.Summon {
for _, locCode := range config.LocCode {
waitForSummons.Add(1)
s := summon(db.db, &config)
go s.summon(locCode)
}
waitForSummons.Wait()
}
}
// always try to create rooms and judges
t := trials(db.db, &config)
t.rooms()
t.judges()
t.createTrials()
fmt.Printf("\nFinished in: %s\n", time.Since(start))
}