@@ -17,6 +17,7 @@ import (
1717 "fmt"
1818 "io"
1919 "log"
20+ "math/rand"
2021 "net/http"
2122 "os"
2223 "sync"
@@ -98,6 +99,12 @@ type KeyConfig struct {
9899 MaxTime int64 `yaml:"maxTime"`
99100}
100101
102+ type configState struct {
103+ keyConfig * KeyConfig
104+ Err error
105+ StaticValues []int64 // For storing the static absolute time of the downloaded blocks.
106+ }
107+
101108func NewQuerier (groupID int , target , prNumber string , qg QueryGroup ) * Querier {
102109 qtype := qg .Type
103110 if qtype == "" {
@@ -126,6 +133,18 @@ func NewQuerier(groupID int, target, prNumber string, qg QueryGroup) *Querier {
126133 }
127134}
128135
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+
129148// Function to load `minTime` and `maxTime` from key.yml
130149func loadKeyConfig () (* KeyConfig , error ) {
131150 filePath := "/config/key.yml"
@@ -148,20 +167,33 @@ func loadKeyConfig() (*KeyConfig, error) {
148167 return & keyConfig , nil
149168}
150169
151- func (q * Querier ) run (wg * sync.WaitGroup ) {
170+ func configstate (v * KeyConfig , err error ) (* configState , error ) {
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 generateStateValues 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+ }, nil
182+ }
183+
184+ func (q * Querier ) run (wg * sync.WaitGroup , timeBound * configState ) {
152185 defer wg .Done ()
153186 fmt .Printf ("Running querier %s %s for %s\n " , q .target , q .name , q .url )
154187 time .Sleep (20 * time .Second )
155188
156- keyConfig , err := loadKeyConfig ()
157-
158189 for {
159190 start := time .Now ()
160191
161192 for _ , query := range q .queries {
162193 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 )
165197 }
166198 }
167199
@@ -172,7 +204,7 @@ func (q *Querier) run(wg *sync.WaitGroup) {
172204 }
173205}
174206
175- func (q * Querier ) query (expr string , timeMode string , keyConfig * KeyConfig ) {
207+ func (q * Querier ) query (expr string , timeMode string , timeBound * configState ) {
176208 queryCount .WithLabelValues (q .target , q .name , expr , q .qtype ).Inc ()
177209 start := time .Now ()
178210
@@ -187,17 +219,20 @@ func (q *Querier) query(expr string, timeMode string, keyConfig *KeyConfig) {
187219 qParams .Set ("query" , expr )
188220 if q .qtype == "range" {
189221 if timeMode == "current" {
190- fmt .Println ("range , current blocks" )
191222 qParams .Set ("start" , fmt .Sprintf ("%d" , int64 (time .Now ().Add (- q .start ).Unix ())))
192223 qParams .Set ("end" , fmt .Sprintf ("%d" , int64 (time .Now ().Add (- q .end ).Unix ())))
193224 qParams .Set ("step" , q .step )
194225 } 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 ))
197227 qParams .Set ("start" , fmt .Sprintf ("%d" , int64 (endTime .Add (- q .start ).Unix ())))
198228 qParams .Set ("end" , fmt .Sprintf ("%d" , int64 (endTime .Add (- q .end ).Unix ())))
199229 qParams .Set ("step" , q .step )
200230 }
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 ())))
201236 }
202237 req .URL .RawQuery = qParams .Encode ()
203238
@@ -263,11 +298,16 @@ func main() {
263298
264299 var wg sync.WaitGroup
265300
301+ keyConfig , err := loadKeyConfig ()
302+ timeBound , err := configstate (keyConfig , err )
303+ if err != nil {
304+ log .Fatalf ("Error creating app state: %v" , err )
305+ }
266306 for i , group := range config .Querier .Groups {
267307 wg .Add (1 )
268- go NewQuerier (i , "pr" , prNumber , group ).run (& wg )
308+ go NewQuerier (i , "pr" , prNumber , group ).run (& wg , timeBound )
269309 wg .Add (1 )
270- go NewQuerier (i , "release" , prNumber , group ).run (& wg )
310+ go NewQuerier (i , "release" , prNumber , group ).run (& wg , timeBound )
271311 }
272312
273313 prometheus .MustRegister (queryDuration , queryCount , queryFailCount )
0 commit comments