@@ -17,6 +17,7 @@ import (
17
17
"fmt"
18
18
"io"
19
19
"log"
20
+ "math/rand"
20
21
"net/http"
21
22
"os"
22
23
"sync"
@@ -98,6 +99,12 @@ type KeyConfig struct {
98
99
MaxTime int64 `yaml:"maxTime"`
99
100
}
100
101
102
+ type configState struct {
103
+ keyConfig * KeyConfig
104
+ Err error
105
+ StaticValues []int64 // For storing the static absolute time for the downloaded blocks.
106
+ }
107
+
101
108
func NewQuerier (groupID int , target , prNumber string , qg QueryGroup ) * Querier {
102
109
qtype := qg .Type
103
110
if qtype == "" {
@@ -126,6 +133,18 @@ func NewQuerier(groupID int, target, prNumber string, qg QueryGroup) *Querier {
126
133
}
127
134
}
128
135
136
+ // This function returns an array of length 5 containing instant values between minTime and maxTime.
137
+ // To calculate the values, the formula (maxTime - minTime) / int64(count+1) is used to determine the step size.
138
+ // The step size is then added incrementally to minTime to generate the values.
139
+ func generateStaticValues (minTime , maxTime int64 , count int ) []int64 {
140
+ step := (maxTime - minTime ) / int64 (count + 1 )
141
+ values := make ([]int64 , count )
142
+ for i := 0 ; i < count ; i ++ {
143
+ values [i ] = minTime + step * int64 (i + 1 )
144
+ }
145
+ return values
146
+ }
147
+
129
148
// Function to load `minTime` and `maxTime` from key.yml
130
149
func loadKeyConfig () (* KeyConfig , error ) {
131
150
filePath := "/config/key.yml"
@@ -148,20 +167,33 @@ func loadKeyConfig() (*KeyConfig, error) {
148
167
return & keyConfig , nil
149
168
}
150
169
151
- func (q * Querier ) run (wg * sync.WaitGroup ) {
170
+ func configstate (v * KeyConfig , err error ) * configState {
171
+ var staticValues []int64
172
+ // if there is an error in extracting key.yml file then it means there is no way to query
173
+ // data on downloaded blocks. so keep StateValues slices to be empty.
174
+ if err == nil {
175
+ staticValues = generateStaticValues (v .MinTime , v .MaxTime , 5 )
176
+ }
177
+ return & configState {
178
+ keyConfig : v ,
179
+ Err : err ,
180
+ StaticValues : staticValues ,
181
+ }
182
+ }
183
+
184
+ func (q * Querier ) run (wg * sync.WaitGroup , timeBound * configState ) {
152
185
defer wg .Done ()
153
186
fmt .Printf ("Running querier %s %s for %s\n " , q .target , q .name , q .url )
154
187
time .Sleep (20 * time .Second )
155
188
156
- keyConfig , err := loadKeyConfig ()
157
-
158
189
for {
159
190
start := time .Now ()
160
191
161
192
for _ , query := range q .queries {
162
193
q .query (query .Expr , "current" , nil )
163
- if err == nil {
164
- q .query (query .Expr , "absolute" , keyConfig )
194
+ // if there is an error we can avoid to go on absolute block.
195
+ if timeBound .Err == nil {
196
+ q .query (query .Expr , "absolute" , timeBound )
165
197
}
166
198
}
167
199
@@ -172,7 +204,7 @@ func (q *Querier) run(wg *sync.WaitGroup) {
172
204
}
173
205
}
174
206
175
- func (q * Querier ) query (expr string , timeMode string , keyConfig * KeyConfig ) {
207
+ func (q * Querier ) query (expr string , timeMode string , timeBound * configState ) {
176
208
queryCount .WithLabelValues (q .target , q .name , expr , q .qtype ).Inc ()
177
209
start := time .Now ()
178
210
@@ -187,17 +219,20 @@ func (q *Querier) query(expr string, timeMode string, keyConfig *KeyConfig) {
187
219
qParams .Set ("query" , expr )
188
220
if q .qtype == "range" {
189
221
if timeMode == "current" {
190
- fmt .Println ("range , current blocks" )
191
222
qParams .Set ("start" , fmt .Sprintf ("%d" , int64 (time .Now ().Add (- q .start ).Unix ())))
192
223
qParams .Set ("end" , fmt .Sprintf ("%d" , int64 (time .Now ().Add (- q .end ).Unix ())))
193
224
qParams .Set ("step" , q .step )
194
225
} else {
195
- fmt .Println ("range , absolute blocks" )
196
- endTime := time .Unix (0 , keyConfig .MaxTime * int64 (time .Millisecond ))
226
+ endTime := time .Unix (0 , timeBound .keyConfig .MaxTime * int64 (time .Millisecond ))
197
227
qParams .Set ("start" , fmt .Sprintf ("%d" , int64 (endTime .Add (- q .start ).Unix ())))
198
228
qParams .Set ("end" , fmt .Sprintf ("%d" , int64 (endTime .Add (- q .end ).Unix ())))
199
229
qParams .Set ("step" , q .step )
200
230
}
231
+ } else if timeMode == "absolute" {
232
+ x := timeBound .StaticValues
233
+ randomIndex := rand .Intn (len (x )) // calculating random index between StaticValues slice.
234
+ instantTime := time .Unix (0 , x [randomIndex ]* int64 (time .Millisecond ))
235
+ qParams .Set ("time" , fmt .Sprintf ("%d" , int64 (instantTime .Unix ())))
201
236
}
202
237
req .URL .RawQuery = qParams .Encode ()
203
238
@@ -263,11 +298,17 @@ func main() {
263
298
264
299
var wg sync.WaitGroup
265
300
301
+ keyConfig , err := loadKeyConfig ()
302
+ if err != nil {
303
+ fmt .Printf ("key.yml file is not present: %v\n " , err )
304
+ }
305
+ timeBound := configstate (keyConfig , err )
306
+
266
307
for i , group := range config .Querier .Groups {
267
308
wg .Add (1 )
268
- go NewQuerier (i , "pr" , prNumber , group ).run (& wg )
309
+ go NewQuerier (i , "pr" , prNumber , group ).run (& wg , timeBound )
269
310
wg .Add (1 )
270
- go NewQuerier (i , "release" , prNumber , group ).run (& wg )
311
+ go NewQuerier (i , "release" , prNumber , group ).run (& wg , timeBound )
271
312
}
272
313
273
314
prometheus .MustRegister (queryDuration , queryCount , queryFailCount )
0 commit comments