From 87f800a811e097ea3fe9e08fab819b2af81771a4 Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Thu, 8 Aug 2024 20:09:26 +0900 Subject: [PATCH] (feat) preview lines --- main.go | 8 +++----- pkg/card.go | 4 ++++ pkg/slices.go | 20 ++++++++++++++++++++ pkg/watcher.go | 27 +++++++++++++++++---------- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 457f0db..db99efc 100644 --- a/main.go +++ b/main.go @@ -160,11 +160,9 @@ func watch(filePath string) { } slog.Info("Error count", "count", result.ErrorCount) - // first line - slog.Info("1st line", "line", pkg.Truncate(result.FirstLine, 50)) - - // last line - slog.Info("Last line", "line", pkg.Truncate(result.LastLine, 50)) + slog.Info("1st line", "line", pkg.Truncate(result.FirstLine, 150)) + slog.Info("Preview line", "line", pkg.Truncate(result.PreviewLine, 150)) + slog.Info("Last line", "line", pkg.Truncate(result.LastLine, 150)) slog.Info("Scanning complete", "filePath", result.FilePath) diff --git a/pkg/card.go b/pkg/card.go index c46fd83..5ba1d16 100644 --- a/pkg/card.go +++ b/pkg/card.go @@ -73,6 +73,10 @@ func GetAlertDetails(f *Flags, result *ScanResult) []gmt.Details { Label: "First Line", Message: result.FirstLine, }, + { + Label: "Mid Lines", + Message: result.PreviewLine, + }, { Label: "Last Line", Message: result.LastLine, diff --git a/pkg/slices.go b/pkg/slices.go index 87ef114..2ae206a 100644 --- a/pkg/slices.go +++ b/pkg/slices.go @@ -1,5 +1,9 @@ package pkg +import ( + "github.com/gravwell/gravwell/v3/timegrinder" +) + func Capped[T any](cap int, slice []T) []T { capped := cap if capped > len(slice) { @@ -7,3 +11,19 @@ func Capped[T any](cap int, slice []T) []T { } return slice[:capped] } + +func searchDate(input string) string { + cfg := timegrinder.Config{} + tg, err := timegrinder.NewTimeGrinder(cfg) + if err != nil { + return "" + } + ts, ok, err := tg.Extract([]byte(input)) + if err != nil { + return "" + } + if !ok { + return "" + } + return ts.String() +} diff --git a/pkg/watcher.go b/pkg/watcher.go index ff24897..9b19e58 100644 --- a/pkg/watcher.go +++ b/pkg/watcher.go @@ -50,16 +50,18 @@ func NewWatcher( } type ScanResult struct { - FilePath string - ErrorCount int - FirstLine string - LastLine string + FilePath string + ErrorCount int + FirstLine string + PreviewLine string + LastLine string } func (w *Watcher) Scan() (*ScanResult, error) { errorCounts := 0 firstLine := "" lastLine := "" + previewLine := "" fileInfo, err := os.Stat(w.filePath) if err != nil { @@ -104,10 +106,14 @@ func (w *Watcher) Scan() (*ScanResult, error) { continue } if re.Match(line) { + lineStr := string(line) if firstLine == "" { - firstLine = string(line) + firstLine = lineStr } - lastLine = string(line) + if len(previewLine) < 1000 && firstLine != lineStr { + previewLine += lineStr + } + lastLine = lineStr errorCounts++ } } @@ -127,10 +133,11 @@ func (w *Watcher) Scan() (*ScanResult, error) { return nil, err } return &ScanResult{ - ErrorCount: errorCounts, - FirstLine: firstLine, - LastLine: lastLine, - FilePath: w.filePath, + ErrorCount: errorCounts, + FirstLine: firstLine, + PreviewLine: previewLine, + LastLine: lastLine, + FilePath: w.filePath, }, nil }