Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/utils/stats/stats.go
2070 views
1
package stats
2
3
import (
4
"fmt"
5
"sync/atomic"
6
7
"github.com/logrusorgru/aurora"
8
"github.com/projectdiscovery/gologger"
9
mapsutil "github.com/projectdiscovery/utils/maps"
10
)
11
12
// Storage is a storage for storing statistics information
13
// about the nuclei engine displaying it at user-defined intervals.
14
type Storage struct {
15
data *mapsutil.SyncLockMap[string, *storageDataItem]
16
}
17
18
type storageDataItem struct {
19
description string
20
value atomic.Int64
21
}
22
23
var Default *Storage
24
25
func init() {
26
Default = New()
27
}
28
29
// NewEntry creates a new entry in the storage object
30
func NewEntry(name, description string) {
31
Default.NewEntry(name, description)
32
}
33
34
// Increment increments the value for a name string
35
func Increment(name string) {
36
Default.Increment(name)
37
}
38
39
// Display displays the stats for a name
40
func Display(name string) {
41
Default.Display(name)
42
}
43
44
func DisplayAsWarning(name string) {
45
Default.DisplayAsWarning(name)
46
}
47
48
// ForceDisplayWarning forces the display of a warning
49
// regardless of current verbosity level
50
func ForceDisplayWarning(name string) {
51
Default.ForceDisplayWarning(name)
52
}
53
54
// GetValue returns the value for a set variable
55
func GetValue(name string) int64 {
56
return Default.GetValue(name)
57
}
58
59
// New creates a new storage object
60
func New() *Storage {
61
data := mapsutil.NewSyncLockMap[string, *storageDataItem]()
62
return &Storage{data: data}
63
}
64
65
// NewEntry creates a new entry in the storage object
66
func (s *Storage) NewEntry(name, description string) {
67
_ = s.data.Set(name, &storageDataItem{description: description, value: atomic.Int64{}})
68
}
69
70
// Increment increments the value for a name string
71
func (s *Storage) Increment(name string) {
72
data, ok := s.data.Get(name)
73
if !ok {
74
return
75
}
76
data.value.Add(1)
77
}
78
79
// Display displays the stats for a name
80
func (s *Storage) Display(name string) {
81
data, ok := s.data.Get(name)
82
if !ok {
83
return
84
}
85
86
dataValue := data.value.Load()
87
if dataValue == 0 {
88
return // don't show for nil stats
89
}
90
gologger.Error().Label("WRN").Msgf(data.description, dataValue)
91
}
92
93
func (s *Storage) DisplayAsWarning(name string) {
94
data, ok := s.data.Get(name)
95
if !ok {
96
return
97
}
98
99
dataValue := data.value.Load()
100
if dataValue == 0 {
101
return // don't show for nil stats
102
}
103
gologger.Warning().Label("WRN").Msgf(data.description, dataValue)
104
}
105
106
// ForceDisplayWarning forces the display of a warning
107
// regardless of current verbosity level
108
func (s *Storage) ForceDisplayWarning(name string) {
109
data, ok := s.data.Get(name)
110
if !ok {
111
return
112
}
113
114
dataValue := data.value.Load()
115
if dataValue == 0 {
116
return // don't show for nil stats
117
}
118
gologger.Print().Msgf("[%v] %v", aurora.BrightYellow("WRN"), fmt.Sprintf(data.description, dataValue))
119
}
120
121
// GetValue returns the value for a set variable
122
func (s *Storage) GetValue(name string) int64 {
123
data, ok := s.data.Get(name)
124
if !ok {
125
return 0
126
}
127
128
return data.value.Load()
129
}
130
131