mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
feat: add log coupling for hostsensorutils
Signed-off-by: Alessio Greggi <ale_grey_91@hotmail.it>
This commit is contained in:
51
core/pkg/hostsensorutils/log_coupling.go
Normal file
51
core/pkg/hostsensorutils/log_coupling.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package hostsensorutils
|
||||
|
||||
import "sync"
|
||||
|
||||
type LogsMap struct {
|
||||
// use sync.Mutex to avoid read/write
|
||||
// access issues in multi-thread environments.
|
||||
sync.Mutex
|
||||
usedLogs map[string]int
|
||||
}
|
||||
|
||||
// NewLogCoupling return an empty LogsMap struct ready to be used.
|
||||
func NewLogCoupling() *LogsMap {
|
||||
return &LogsMap{
|
||||
usedLogs: make(map[string]int),
|
||||
}
|
||||
}
|
||||
|
||||
// update add the logContent to the internal map
|
||||
// and set the occurrencty to 1 (if it has never been used before),
|
||||
// increment its values otherwise.
|
||||
func (lm *LogsMap) update(logContent string) {
|
||||
lm.Lock()
|
||||
_, ok := lm.usedLogs[logContent]
|
||||
if !ok {
|
||||
lm.usedLogs[logContent] = 1
|
||||
} else {
|
||||
lm.usedLogs[logContent]++
|
||||
}
|
||||
lm.Unlock()
|
||||
}
|
||||
|
||||
// isDuplicated check if logContent is already present inside the internal map.
|
||||
// Return true in case logContent already exists, false otherwise.
|
||||
func (lm *LogsMap) isDuplicated(logContent string) bool {
|
||||
lm.Lock()
|
||||
_, ok := lm.usedLogs[logContent]
|
||||
lm.Unlock()
|
||||
return ok
|
||||
}
|
||||
|
||||
// GgtOccurrence retrieve the number of occurrences logContent has been used.
|
||||
func (lm *LogsMap) getOccurrence(logContent string) int {
|
||||
lm.Lock()
|
||||
occurrence, ok := lm.usedLogs[logContent]
|
||||
lm.Unlock()
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return occurrence
|
||||
}
|
||||
Reference in New Issue
Block a user