Skip to content

Commit 4598080

Browse files
feat: anomaly key
1 parent 7ec54b4 commit 4598080

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ tmp/
2525
go-watch-logs
2626
*.sqlite
2727
*.sqlite3
28-
logs/
28+
logs/
29+
testdata/

pkg/database.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ var (
1515

1616
func InitDB(dbName string) (*sql.DB, error) {
1717
if db != nil {
18-
// do a db ping to check if the connection is still alive
1918
if err := db.Ping(); err == nil {
2019
slog.Info("Reusing database connection", "dbName", dbName)
2120
return db, nil
@@ -33,38 +32,49 @@ func InitDB(dbName string) (*sql.DB, error) {
3332
return nil, err
3433
}
3534

36-
_, err = db.Exec(`
35+
db.SetMaxOpenConns(5)
36+
db.SetMaxIdleConns(5)
37+
db.SetConnMaxLifetime(time.Hour)
38+
39+
if err := createTables(db); err != nil {
40+
return nil, err
41+
}
42+
43+
return db, nil
44+
}
45+
func Vacuum(dbName string) error {
46+
if _, err := os.Stat(dbName); err == nil {
47+
if err := os.Remove(dbName); err != nil {
48+
return err
49+
}
50+
}
51+
return nil
52+
}
53+
54+
func createTables(db *sql.DB) error {
55+
slog.Info("Creating tables if not exist")
56+
_, err := db.Exec(`
3757
CREATE TABLE IF NOT EXISTS state (
3858
key TEXT PRIMARY KEY,
39-
value INTEGER
59+
value INTEGER,
60+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
4061
)
4162
`)
4263
if err != nil {
4364
slog.Error("Error creating state table", "error", err.Error())
44-
return nil, err
65+
return err
4566
}
4667
_, err = db.Exec(`
47-
CREATE TABLE IF NOT EXISTS plot (
68+
CREATE TABLE IF NOT EXISTS anomaly (
4869
key TEXT PRIMARY KEY,
49-
value INTEGER
70+
value INTEGER,
5071
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
5172
)
5273
`)
5374
if err != nil {
5475
slog.Error("Error creating plot table", "error", err.Error())
55-
return nil, err
76+
return err
5677
}
57-
db.SetMaxOpenConns(5)
58-
db.SetMaxIdleConns(5)
59-
db.SetConnMaxLifetime(time.Hour)
6078

61-
return db, nil
62-
}
63-
func Vacuum(dbName string) error {
64-
if _, err := os.Stat(dbName); err == nil {
65-
if err := os.Remove(dbName); err != nil {
66-
return err
67-
}
68-
}
6979
return nil
7080
}

pkg/watcher.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Watcher struct {
1414
db *sql.DB
1515
dbName string // full path
1616
filePath string
17+
anomalyKey string
1718
lastLineKey string
1819
lastFileSizeKey string
1920
matchPattern string
@@ -43,6 +44,7 @@ func NewWatcher(
4344
anomaly: anomaly,
4445
matchPattern: matchPattern,
4546
ignorePattern: ignorePattern,
47+
anomalyKey: "anm-" + filePath,
4648
lastLineKey: "llk-" + filePath,
4749
lastFileSizeKey: "llks-" + filePath,
4850
}
@@ -65,6 +67,8 @@ type ScanResult struct {
6567
LastDate string
6668
}
6769

70+
var lines = []string{}
71+
6872
func (w *Watcher) Scan() (*ScanResult, error) {
6973
errorCounts := 0
7074
firstLine := ""
@@ -108,6 +112,7 @@ func (w *Watcher) Scan() (*ScanResult, error) {
108112
bytesRead := w.lastFileSize
109113

110114
for scanner.Scan() {
115+
lines = append(lines, scanner.Text())
111116
line := scanner.Bytes()
112117
bytesRead += int64(len(line)) + 1 // Adding 1 for the newline character
113118
currentLineNum++
@@ -116,10 +121,24 @@ func (w *Watcher) Scan() (*ScanResult, error) {
116121
if linesRead < 0 {
117122
linesRead = -linesRead
118123
}
119-
slog.Debug("Scanning line", "line", string(line), "lineNum", currentLineNum, "linesRead", linesRead)
124+
// slog.Debug("Scanning line", "line", string(line), "lineNum", currentLineNum, "linesRead", linesRead)
120125
if w.ignorePattern != "" && ri.Match(line) {
121126
continue
122127
}
128+
129+
// anomaly insertion
130+
if w.anomaly {
131+
match := re.FindAllString(string(line), -1)
132+
var exactMatch string
133+
if len(match) >= 1 {
134+
exactMatch = match[0]
135+
}
136+
if exactMatch != "" {
137+
slog.Info("Match found", "line", string(line), "match", exactMatch)
138+
}
139+
140+
}
141+
123142
if re.Match(line) {
124143
lineStr := string(line)
125144
if firstLine == "" {

0 commit comments

Comments
 (0)