-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
211 lines (181 loc) · 5.77 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 deprecated
// import (
// "flag"
// "math"
// "os"
// "regexp"
// "strings"
// "sync"
// "time"
// elastic "github.com/olivere/elastic/v7"
// "github.com/brianbroderick/logit"
// "github.com/fatih/color"
// "github.com/joho/godotenv"
// )
// type batch struct {
// minute time.Time
// sha string
// }
// var (
// batchMap = make(map[batch]*query)
// batchDetailsMap = make(map[batch]*queryDetailStats)
// clients = make(map[string]*elastic.Client)
// bulkProc = make(map[string]*elastic.BulkProcessor)
// catIndices = make(map[string]*elastic.CatIndicesService)
// mutex = &sync.Mutex{}
// detailsMutex = &sync.Mutex{}
// redisQueues = make([]string, 0)
// statsQueues = make([]string, 0)
// detailArgs = &queryDetails{}
// // flags
// queuePtr string
// statsPtr string
// redisPtr string
// redisPwPtr string
// elasticPtr string
// detailFragmentPtr string
// detailColumnsPtr string
// // colors for terminal
// blue = color.New(color.FgBlue).SprintFunc()
// cyan = color.New(color.FgCyan).SprintFunc()
// green = color.New(color.FgGreen).SprintFunc()
// magenta = color.New(color.FgMagenta).SprintFunc()
// red = color.New(color.FgRed).SprintFunc()
// yellow = color.New(color.FgYellow).SprintFunc()
// white = color.New(color.FgWhite).SprintFunc()
// suppressedCommandTag = map[string]bool{
// "BIND": true,
// "PARSE": true,
// "BEGIN": true,
// "COMMIT": true,
// }
// )
// func main() {
// flag.StringVar(&queuePtr, "queues", "", "comma separated list of queues that overrides env vars. Can also be set via PLS_REDIS_QUEUES env var")
// flag.StringVar(&statsPtr, "statsQueues", "", "comma separated list of queues for statistics that overrides env vars. Can also be set via PLS_REDIS_STATS_QUEUES env var")
// flag.StringVar(&redisPtr, "redisUrl", "", "Redis URL. Can also set via PLS_REDIS_URL env var")
// flag.StringVar(&redisPwPtr, "redisPassword", "", "Redis password (optional). Can also set via PLS_REDIS_PASSWORD env var")
// flag.StringVar(&elasticPtr, "elasticUrl", "", "Elasticsearch URL. Can also set via PLS_ELASTIC_URL env var")
// flag.StringVar(&detailFragmentPtr, "detailFragment", "", "SQL fragment to look for when parsing out query details")
// flag.StringVar(&detailColumnsPtr, "detailColumn", "", "comma separated list of columns to extract from details and store")
// flag.Parse()
// // set detail flags after they've been parsed
// detailArgs = newQueryDetails(detailFragmentPtr, detailColumnsPtr)
// logit.Info("detailArgs: %v", detailArgs)
// initialSetup()
// SetupElastic()
// defer bulkProc["bulk"].Close()
// defer clients["bulk"].Stop()
// // Flush to bulkProc every 60 seconds
// ticker := time.NewTicker(time.Second * 60)
// go func() {
// for range ticker.C {
// iterOverQueries()
// }
// }()
// for _, queue := range redisQueues {
// go startRedisBatch(queue, "query")
// time.Sleep(42 * time.Millisecond) // stagger threads hitting Redis
// }
// for _, queue := range statsQueues {
// go startRedisBatch(queue, "stats")
// time.Sleep(42 * time.Millisecond) // stagger threads hitting Redis
// }
// // Flush to bulkProc every 60 seconds
// // Do this after other things have spun up to spread load more across the minute.
// detailsTicker := time.NewTicker(time.Second * 60)
// go func() {
// for range detailsTicker.C {
// iterOverDetails()
// }
// }()
// forever := make(chan bool)
// <-forever
// }
// func initialSetup() {
// setupEnv()
// populateRedisQueues(os.Getenv("PLS_REDIS_QUEUES"))
// populateStatsQueues(os.Getenv("PLS_REDIS_STATS_QUEUES"))
// SetupRedis()
// }
// func setupEnv() {
// if os.Getenv("PLATFORM_ENV") == "" {
// os.Setenv("PLATFORM_ENV", "prod")
// }
// platformEnv := os.Getenv("PLATFORM_ENV")
// if platformEnv != "prod" && platformEnv != "stage" {
// filename := ".env_" + platformEnv
// err := godotenv.Load(filename)
// if err != nil {
// // logit.Error("%s file not found", filename)
// }
// }
// err := godotenv.Load(".env")
// if err != nil {
// // logit.Error(".env file not found")
// }
// }
// func populateRedisQueues(queues string) {
// // Override with a flag, if exists
// if queuePtr != "" {
// queues = queuePtr
// }
// if queues == "" {
// redisQueues = append(redisQueues, "postgres")
// } else {
// r := regexp.MustCompile(" ")
// queues = r.ReplaceAllString(queues, "")
// redisQueues = strings.Split(queues, ",")
// }
// uniqueRedisQueues()
// if os.Getenv("PLATFORM_ENV") != "test" {
// logit.Info("Redis Queues: %v", redisQueues)
// }
// }
// func uniqueRedisQueues() {
// keys := make(map[string]bool)
// list := []string{}
// for _, entry := range redisQueues {
// if _, value := keys[entry]; !value {
// keys[entry] = true
// list = append(list, entry)
// }
// }
// redisQueues = list
// }
// func redisKey() string {
// platformEnv := os.Getenv("PLATFORM_ENV")
// if platformEnv != "test" {
// return "postgres"
// }
// return "postgres_test"
// }
// func currentMinute() time.Time {
// return time.Now().UTC().Round(time.Minute)
// }
// func lastMinute() time.Time {
// return currentMinute().Add(-1 * time.Minute)
// }
// func roundToMinute(minute time.Time) time.Time {
// return minute.Round(time.Minute)
// }
// func round(val float64, roundOn float64, places int) float64 {
// pow := math.Pow(10, float64(places))
// digit := pow * val
// _, div := math.Modf(digit)
// var round float64
// if val > 0 {
// if div >= roundOn {
// round = math.Ceil(digit)
// } else {
// round = math.Floor(digit)
// }
// } else {
// if div >= roundOn {
// round = math.Floor(digit)
// } else {
// round = math.Ceil(digit)
// }
// }
// return round / pow
// }