@@ -123,7 +123,7 @@ var checkPgQueriesCmd = &cobra.Command{
123
123
checkQueries (rules , & stats , dbPool , queries )
124
124
125
125
if checkPgQueriesConfig .writeRulesWithStat != "" {
126
- rules .UpdateFromStats (stats , checkPgQueriesConfig .sortRulesByCount )
126
+ rules .UpdateFromStats (& stats , checkPgQueriesConfig .sortRulesByCount )
127
127
if err := rules .WriteToFile (checkPgQueriesConfig .writeRulesWithStat ); err != nil {
128
128
log .Printf ("Failed to update rules stat: %v" , err )
129
129
}
@@ -384,17 +384,28 @@ func checkQueries(rules Rules, stats *QueryStats, dbPool *internal.YdbPool, quer
384
384
var itemsCounter atomic.Int64
385
385
writeStatEveryItems := int64 (checkPgQueriesConfig .writeStatEveryItems )
386
386
var wg sync.WaitGroup
387
+ var writeStatMutex sync.Mutex
387
388
for range checkPgQueriesConfig .checkersCount {
388
389
wg .Add (1 )
389
390
go func () {
390
391
defer wg .Done ()
391
392
for q := range queries {
392
393
checkQuery (stats , rules , dbPool , q )
393
394
counter := itemsCounter .Add (1 )
394
- if counter % writeStatEveryItems == 0 && checkPgQueriesConfig .writeStatPath != "" {
395
- if err := stats .SaveToFile (checkPgQueriesConfig .writeStatPath ); err != nil {
396
- log .Printf ("Stat file written failed %q: %v" , checkPgQueriesConfig .writeStatPath , err )
395
+ if writeStatEveryItems > 0 && counter % writeStatEveryItems == 0 {
396
+ writeStatMutex .Lock ()
397
+ if checkPgQueriesConfig .writeStatPath != "" {
398
+ if err := stats .SaveToFile (checkPgQueriesConfig .writeStatPath ); err != nil {
399
+ log .Printf ("Stat file written failed %q: %v" , checkPgQueriesConfig .writeStatPath , err )
400
+ }
401
+ }
402
+ if checkPgQueriesConfig .writeRulesWithStat != "" {
403
+ rules .UpdateFromStats (stats , checkPgQueriesConfig .sortRulesByCount )
404
+ if err := rules .WriteToFile (checkPgQueriesConfig .writeRulesWithStat ); err != nil {
405
+ log .Printf ("Failed to update rules stat: %v" , err )
406
+ }
397
407
}
408
+ writeStatMutex .Unlock ()
398
409
}
399
410
}
400
411
}()
@@ -495,13 +506,27 @@ type QueryStats struct {
495
506
m sync.RWMutex
496
507
writeStatMutex sync.Mutex
497
508
498
- OkCount int
499
- TotalCount int
509
+ okCount int
510
+ totalCount int
500
511
501
512
MatchToRules map [string ]* CounterWithExample [string ] // [rule name] query example
502
513
UnknownProblems map [string ]* CounterWithExample [string ]
503
514
}
504
515
516
+ func (s * QueryStats ) GetTotalCount () int {
517
+ s .m .Lock ()
518
+ defer s .m .Unlock ()
519
+
520
+ return s .totalCount
521
+ }
522
+
523
+ func (s * QueryStats ) GetOkCount () int {
524
+ s .m .Lock ()
525
+ defer s .m .Lock ()
526
+
527
+ return s .okCount
528
+ }
529
+
505
530
func (s * QueryStats ) GetOkPercent () float64 {
506
531
s .m .RLock ()
507
532
defer s .m .RUnlock ()
@@ -510,22 +535,22 @@ func (s *QueryStats) GetOkPercent() float64 {
510
535
}
511
536
512
537
func (s * QueryStats ) getOkPercentNeedLock () float64 {
513
- return float64 (s .OkCount ) / float64 (s .TotalCount ) * 100
538
+ return float64 (s .okCount ) / float64 (s .totalCount ) * 100
514
539
}
515
540
516
541
func (s * QueryStats ) CountASOK (query string ) {
517
542
s .m .Lock ()
518
543
defer s .m .Unlock ()
519
544
520
- s .TotalCount ++
521
- s .OkCount ++
545
+ s .totalCount ++
546
+ s .okCount ++
522
547
}
523
548
524
549
func (s * QueryStats ) CountAsKnown (ruleName string , query string ) {
525
550
s .m .Lock ()
526
551
defer s .m .Unlock ()
527
552
528
- s .TotalCount ++
553
+ s .totalCount ++
529
554
if s .MatchToRules == nil {
530
555
s .MatchToRules = make (map [string ]* CounterWithExample [string ])
531
556
}
@@ -550,7 +575,7 @@ func (s *QueryStats) CountAsUnknown(reason string, query string) {
550
575
s .m .Lock ()
551
576
defer s .m .Unlock ()
552
577
553
- s .TotalCount ++
578
+ s .totalCount ++
554
579
if s .UnknownProblems == nil {
555
580
s .UnknownProblems = make (map [string ]* CounterWithExample [string ])
556
581
}
@@ -597,7 +622,7 @@ func (s *QueryStats) PrintStats() {
597
622
defer s .m .Unlock ()
598
623
599
624
fmt .Println ("Queries stat." )
600
- fmt .Println ("Ok Count:" , s .OkCount )
625
+ fmt .Println ("Ok Count:" , s .okCount )
601
626
fmt .Println ()
602
627
fmt .Println ("Known issues" )
603
628
SessionStats_printExampleCounter (getTopCounter (s .MatchToRules , 10 ))
@@ -656,8 +681,8 @@ func (s *QueryStats) SaveToFile(path string) error {
656
681
KnownIssues []CounterWithExample [string ] `yaml:"known_issues"`
657
682
}
658
683
659
- statFile .TotalCount = s .TotalCount
660
- statFile .OkCount = s .OkCount
684
+ statFile .TotalCount = s .totalCount
685
+ statFile .OkCount = s .okCount
661
686
statFile .OkPercent = s .getOkPercentNeedLock ()
662
687
statFile .UnknownIssues = s .getTopUnknownNeedLock (math .MaxInt )
663
688
statFile .KnownIssues = s .getTopKnownNeedLock (math .MaxInt )
0 commit comments