From 95467e823e183253f22bd6236d05a5036e0f7f13 Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Fri, 13 Dec 2024 16:19:06 +0900 Subject: [PATCH] feat: anomaly detection --- main.go | 3 ++- pkg/flags.go | 1 + pkg/watcher.go | 1 + pkg/watcher_test.go | 14 +++++++------- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 8c9434e..90cc05d 100644 --- a/main.go +++ b/main.go @@ -157,7 +157,7 @@ func validate() { } func watch(filePath string) { - watcher, err := pkg.NewWatcher(f.DBPath, filePath, f.Match, f.Ignore) + watcher, err := pkg.NewWatcher(f.DBPath, filePath, f.Match, f.Ignore, f.Anomaly) if err != nil { slog.Error("Error creating watcher", "error", err.Error(), "filePath", filePath) return @@ -233,6 +233,7 @@ func flags() { flag.IntVar(&f.MemLimit, "mem-limit", 100, "memory limit in MB (0 to disable)") flag.IntVar(&f.FilePathsCap, "file-paths-cap", 100, "max number of file paths to watch") flag.IntVar(&f.Min, "min", 1, "on minimum num of matches, it should notify") + flag.BoolVar(&f.Anomaly, "anomaly", false, "") flag.BoolVar(&f.Version, "version", false, "") flag.BoolVar(&f.Test, "test", false, `Quickly test paths or regex # will test if the input matches the regex diff --git a/pkg/flags.go b/pkg/flags.go index 04a9202..59d0b8f 100644 --- a/pkg/flags.go +++ b/pkg/flags.go @@ -16,6 +16,7 @@ type Flags struct { LogLevel int MemLimit int MSTeamsHook string + Anomaly bool Test bool Version bool } diff --git a/pkg/watcher.go b/pkg/watcher.go index 92871fe..adb26ee 100644 --- a/pkg/watcher.go +++ b/pkg/watcher.go @@ -27,6 +27,7 @@ func NewWatcher( filePath string, matchPattern string, ignorePattern string, + anomaly bool, ) (*Watcher, error) { dbName += ".sqlite" db, err := InitDB(dbName) diff --git a/pkg/watcher_test.go b/pkg/watcher_test.go index 1a9fc92..ea8cc0a 100644 --- a/pkg/watcher_test.go +++ b/pkg/watcher_test.go @@ -31,7 +31,7 @@ func TestNewWatcher(t *testing.T) { matchPattern := "error:1" // nolint: goconst ignorePattern := "ignore" // nolint: goconst - watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern) + watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern, false) assert.NoError(t, err) assert.NotNil(t, watcher) @@ -52,7 +52,7 @@ error:1` matchPattern := `error:1` // nolint: goconst ignorePattern := `ignore` // nolint: goconst - watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern) + watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern, false) assert.NoError(t, err) defer watcher.Close() @@ -75,7 +75,7 @@ line2` matchPattern := `error:1` // nolint: goconst ignorePattern := `ignore` // nolint: goconst - watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern) + watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern, false) assert.NoError(t, err) defer watcher.Close() @@ -112,7 +112,7 @@ error:1` matchPattern := `error:1` ignorePattern := `ignore` - watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern) + watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern, false) if err != nil { b.Fatal(err) } @@ -132,7 +132,7 @@ func BenchmarkLoadAndSaveState(b *testing.B) { matchPattern := "error:1" ignorePattern := "ignore" - watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern) + watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern, false) if err != nil { b.Fatal(err) } @@ -141,7 +141,7 @@ func BenchmarkLoadAndSaveState(b *testing.B) { watcher.lastLineNum = 10 for i := 0; i < b.N; i++ { - _, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern) + _, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern, false) if err != nil { b.Fatal(err) } @@ -162,7 +162,7 @@ line2` matchPattern := `error:1` ignorePattern := `ignore` - watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern) + watcher, err := NewWatcher(dbName, filePath, matchPattern, ignorePattern, false) if err != nil { b.Fatal(err) }