Skip to content

Commit

Permalink
feat: anomaly key
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Dec 25, 2024
1 parent 7ec54b4 commit 4598080
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ tmp/
go-watch-logs
*.sqlite
*.sqlite3
logs/
logs/
testdata/
46 changes: 28 additions & 18 deletions pkg/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var (

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

_, err = db.Exec(`
db.SetMaxOpenConns(5)
db.SetMaxIdleConns(5)
db.SetConnMaxLifetime(time.Hour)

if err := createTables(db); err != nil {
return nil, err
}

return db, nil
}
func Vacuum(dbName string) error {
if _, err := os.Stat(dbName); err == nil {
if err := os.Remove(dbName); err != nil {
return err
}
}
return nil
}

func createTables(db *sql.DB) error {
slog.Info("Creating tables if not exist")
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS state (
key TEXT PRIMARY KEY,
value INTEGER
value INTEGER,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`)
if err != nil {
slog.Error("Error creating state table", "error", err.Error())
return nil, err
return err
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS plot (
CREATE TABLE IF NOT EXISTS anomaly (
key TEXT PRIMARY KEY,
value INTEGER
value INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`)
if err != nil {
slog.Error("Error creating plot table", "error", err.Error())
return nil, err
return err
}
db.SetMaxOpenConns(5)
db.SetMaxIdleConns(5)
db.SetConnMaxLifetime(time.Hour)

return db, nil
}
func Vacuum(dbName string) error {
if _, err := os.Stat(dbName); err == nil {
if err := os.Remove(dbName); err != nil {
return err
}
}
return nil
}
21 changes: 20 additions & 1 deletion pkg/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Watcher struct {
db *sql.DB
dbName string // full path
filePath string
anomalyKey string
lastLineKey string
lastFileSizeKey string
matchPattern string
Expand Down Expand Up @@ -43,6 +44,7 @@ func NewWatcher(
anomaly: anomaly,
matchPattern: matchPattern,
ignorePattern: ignorePattern,
anomalyKey: "anm-" + filePath,
lastLineKey: "llk-" + filePath,
lastFileSizeKey: "llks-" + filePath,
}
Expand All @@ -65,6 +67,8 @@ type ScanResult struct {
LastDate string
}

var lines = []string{}

func (w *Watcher) Scan() (*ScanResult, error) {
errorCounts := 0
firstLine := ""
Expand Down Expand Up @@ -108,6 +112,7 @@ func (w *Watcher) Scan() (*ScanResult, error) {
bytesRead := w.lastFileSize

for scanner.Scan() {
lines = append(lines, scanner.Text())
line := scanner.Bytes()
bytesRead += int64(len(line)) + 1 // Adding 1 for the newline character
currentLineNum++
Expand All @@ -116,10 +121,24 @@ func (w *Watcher) Scan() (*ScanResult, error) {
if linesRead < 0 {
linesRead = -linesRead
}
slog.Debug("Scanning line", "line", string(line), "lineNum", currentLineNum, "linesRead", linesRead)
// slog.Debug("Scanning line", "line", string(line), "lineNum", currentLineNum, "linesRead", linesRead)
if w.ignorePattern != "" && ri.Match(line) {
continue
}

// anomaly insertion
if w.anomaly {
match := re.FindAllString(string(line), -1)
var exactMatch string
if len(match) >= 1 {
exactMatch = match[0]
}
if exactMatch != "" {
slog.Info("Match found", "line", string(line), "match", exactMatch)
}

}

if re.Match(line) {
lineStr := string(line)
if firstLine == "" {
Expand Down

0 comments on commit 4598080

Please sign in to comment.