Skip to content

Commit

Permalink
feat: anomaly detection
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Dec 13, 2024
1 parent c4c54eb commit 95467e8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Flags struct {
LogLevel int
MemLimit int
MSTeamsHook string
Anomaly bool
Test bool
Version bool
}
1 change: 1 addition & 0 deletions pkg/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func NewWatcher(
filePath string,
matchPattern string,
ignorePattern string,
anomaly bool,
) (*Watcher, error) {
dbName += ".sqlite"
db, err := InitDB(dbName)
Expand Down
14 changes: 7 additions & 7 deletions pkg/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 95467e8

Please sign in to comment.