Skip to content

Commit

Permalink
(feat) preview lines
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Aug 8, 2024
1 parent 983439d commit 87f800a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
8 changes: 3 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions pkg/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions pkg/slices.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
package pkg

import (
"github.com/gravwell/gravwell/v3/timegrinder"
)

func Capped[T any](cap int, slice []T) []T {
capped := cap
if capped > len(slice) {
capped = len(slice)
}
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()
}
27 changes: 17 additions & 10 deletions pkg/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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++
}
}
Expand All @@ -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
}

Expand Down

0 comments on commit 87f800a

Please sign in to comment.