Skip to content

Commit

Permalink
feat: counter moved to anomalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Dec 27, 2024
1 parent 49c7b70 commit fe2e4a3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
21 changes: 21 additions & 0 deletions pkg/anomalizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pkg

type Anomalizer struct {
counter map[string]int
limitCounterKeys int
}

func NewAnomalizer() *Anomalizer {
return &Anomalizer{
counter: make(map[string]int),
limitCounterKeys: 100,
}
}

func (a *Anomalizer) MemSafeCount(key string) {
tk := Truncate(key, 100)
a.counter[tk]++
if a.counter[tk] > a.limitCounterKeys {
delete(a.counter, tk)
}
}
1 change: 0 additions & 1 deletion pkg/anomaly.go

This file was deleted.

8 changes: 4 additions & 4 deletions pkg/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ var (

func InitDB(dbName string) (*sql.DB, error) {
if db != nil {
if err := db.Ping(); err == nil {
err := db.Ping()
if err == nil {
slog.Info("Reusing database connection", "dbName", dbName)
return db, nil
} else {
slog.Warn("Closing database connection", "error", err.Error())
db.Close()
}
slog.Warn("Closing database connection", "error", err.Error())
db.Close()
}

slog.Info("Initializing database", "dbName", dbName)
Expand Down
25 changes: 21 additions & 4 deletions pkg/files.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkg

import (
"math"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -69,8 +70,24 @@ func GetHomedir() string {
}

func IsRecentlyModified(fileInfo os.FileInfo, within uint64) bool {
modTime := fileInfo.ModTime().Unix()
currentTime := time.Now().Unix()
bufferMins := int64(within) * 60
return currentTime-modTime < bufferMins
// Get the current time
now := time.Now()

// Get the file's modification time
modTime := fileInfo.ModTime()

// Add a 1-hour buffer (3600 seconds) to the "within" duration
adjustedWithin := within + 3600

// Ensure that adjustedWithin is within the bounds of int64
if adjustedWithin > math.MaxInt64 {
// If the value exceeds the max int64 value, return false as it would cause overflow
return false
}

// Calculate the time difference
diff := now.Sub(modTime)

// Check if the difference is within the adjusted duration
return diff <= time.Duration(adjustedWithin)*time.Second
}
8 changes: 3 additions & 5 deletions pkg/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Watcher struct {
ignorePattern string
lastLineNum int
lastFileSize int64
anomalizer *Anomalizer
anomaly bool
}

Expand All @@ -42,6 +43,7 @@ func NewWatcher(
dbName: dbName,
filePath: filePath,
anomaly: anomaly,
anomalizer: NewAnomalizer(),
matchPattern: matchPattern,
ignorePattern: ignorePattern,
anomalyKey: "anm-" + filePath,
Expand Down Expand Up @@ -112,8 +114,6 @@ func (w *Watcher) Scan() (*ScanResult, error) {
linesRead := 0
bytesRead := w.lastFileSize

counter := make(map[string]int)

for scanner.Scan() {
lines = append(lines, scanner.Text())
line := scanner.Bytes()
Expand All @@ -138,10 +138,8 @@ func (w *Watcher) Scan() (*ScanResult, error) {
}
if exactMatch != "" {
slog.Info("Match found", "line", string(line), "match", exactMatch)
counter[exactMatch]++
w.anomalizer.MemSafeCount(exactMatch)
}
// pp.Println(counter)

}

if regMatch.Match(line) {
Expand Down

0 comments on commit fe2e4a3

Please sign in to comment.