Skip to content

Commit

Permalink
Feature/fixes (#28)
Browse files Browse the repository at this point in the history
* fix: possible inf loop

* feat: messaging and fmts

* feat: messaging and fmts
  • Loading branch information
kevincobain2000 authored Jan 20, 2025
1 parent 371f03d commit 16a783e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 28 deletions.
58 changes: 31 additions & 27 deletions pkg/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func NotifyOwnError(e error, r slog.Record, msTeamsHook, proxy string) {
}
err := gmt.Send(hostname, details, msTeamsHook, proxy)
if err != nil {
slog.Error("Error sending to Teams", "error", err.Error())
// keep it warn to prevent infinite loop from the global handler of slog
slog.Warn("Error sending to Teams", "error", err.Error())
} else {
slog.Info("Successfully sent own error to MS Teams")
}
Expand All @@ -49,67 +50,69 @@ func Notify(result *ScanResult, f Flags, version string) {
Message: version,
},
{
Label: "File Path",
Label: "File",
Message: result.FilePath,
},
{
Label: "Running Every",
Message: fmt.Sprintf("%d secs", f.Every),
},
{
Label: "Match Pattern",
Label: "Match",
Message: f.Match,
},
{
Label: "Ignore Pattern",
Label: "Ignore",
Message: f.Ignore,
},
{
Label: "First Line",
Message: Truncate(result.FirstLine, TruncateMax),
},
{
Label: "Mid Lines",
Message: result.PreviewLine,
Label: "Lines",
Message: fmt.Sprintf(
"%s\n\r%s\n\r%s",
Truncate(result.FirstLine, TruncateMax),
ReduceToNLines(result.PreviewLine, 3),
Truncate(result.LastLine, TruncateMax),
),
},
{
Label: "Last Line",
Message: Truncate(result.LastLine, TruncateMax),
Label: "Settings",
Message: fmt.Sprintf(
"min (%d), every (%d secs), max streak (%d)",
f.Min,
f.Every,
f.Streak,
),
},
{
Label: "Details",
Label: "Scan Details",
Message: fmt.Sprintf(
"Min Threshold: %d, Lines Read: %d\n\rMatches Found: %d, Ratio %.2f%%",
f.Min,
result.LinesRead,
result.ErrorCount,
"lines read (%s), %.2f%% errors (%s), scans til date (%s)",
NumberToK(result.LinesRead),
result.ErrorPercent,
NumberToK(result.ErrorCount),
NumberToK(result.ScanCount),
),
},
{
Label: "Streaks",
Message: StreakSymbols(result.Streak, f.Streak, f.Min) + "\n\r" + fmt.Sprintf("Last %d failed. Scan counter: %d", f.Streak, result.ScanCount),
Message: StreakSymbols(result.Streak, f.Streak, f.Min),
},
}
if result.FirstDate != "" || result.LastDate != "" {
var duration string
if result.FirstDate != "" && result.LastDate != "" {
firstDate, err := time.Parse("2006-01-02 15:04:05", result.FirstDate)
if err != nil {
duration = "X"
duration = ""
} else {
lastDate, err := time.Parse("2006-01-02 15:04:05", result.LastDate)
if err == nil {
duration = lastDate.Sub(firstDate).String()
duration = fmt.Sprintf("(%s)", lastDate.Sub(firstDate).String())
} else {
duration = "X"
duration = ""
}
}
}

details = append(details, gmt.Details{
Label: "Range",
Message: fmt.Sprintf("%s to %s (Duration: %s)", result.FirstDate, result.LastDate, duration),
Message: fmt.Sprintf("%s to %s %s", result.FirstDate, result.LastDate, duration),
})
}

Expand All @@ -129,7 +132,8 @@ func Notify(result *ScanResult, f Flags, version string) {

err := gmt.Send(hostname, details, f.MSTeamsHook, f.Proxy)
if err != nil {
slog.Error("Error sending to Teams", "error", err.Error())
// keep it warn to prevent infinite loop from the global handler of slog
slog.Warn("Error sending to Teams", "error", err.Error())
} else {
slog.Info("Successfully sent to MS Teams")
}
Expand Down
23 changes: 22 additions & 1 deletion pkg/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/hex"
"log/slog"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -87,6 +88,26 @@ func StreakSymbols(arr []int, length int, minimum int) string {
for i := len(symbols); i < DisplayableStreakNumber(length); i++ {
symbols = append([]string{"□"}, symbols...)
}
// if last is ✕ then replace with ✖(bold)
if symbols[len(symbols)-1] == "✕" {
symbols[len(symbols)-1] = "✖"
}

return strings.Join(symbols, "")
}

func NumberToK(num int) string {
if num >= 1000 {
return strconv.FormatFloat(float64(num)/1000, 'f', 1, 64) + "K"
}
return strconv.Itoa(num)
}

return strings.Join(symbols, " ")
// reduce to n lines
func ReduceToNLines(s string, n int) string {
lines := strings.Split(s, "\n")
if len(lines) <= n {
return s
}
return strings.Join(lines[:n], "\n")
}
34 changes: 34 additions & 0 deletions pkg/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pkg

import (
"strconv"
"testing"
)

func TestNumberToK(t *testing.T) {
tests := []struct {
input int
expected string
}{
{999, "999"}, // Less than 1000
{1000, "1.0K"}, // Exactly 1000
{1500, "1.5K"}, // Simple thousands
{25000, "25.0K"}, // Larger thousands
{1000000, "1000.0K"}, // Very large numbers
{0, "0"}, // Edge case: zero
{-500, "-500"}, // Negative numbers (no conversion)
{-1500, "-1500"}, // Negative thousand (not supported)
}

for _, test := range tests {
t.Run(
"Input: "+strconv.Itoa(test.input),
func(t *testing.T) {
result := NumberToK(test.input)
if result != test.expected {
t.Errorf("ConvertToK(%d) = %s; expected %s", test.input, result, test.expected)
}
},
)
}
}
5 changes: 5 additions & 0 deletions pkg/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (w *Watcher) Scan() (*ScanResult, error) {
currentLineNum := 1
linesRead := 0
bytesRead := w.lastFileSize
isFirstScan := w.getScanCount() == 0

for scanner.Scan() {
line := scanner.Bytes()
Expand All @@ -128,6 +129,10 @@ func (w *Watcher) Scan() (*ScanResult, error) {
linesRead = -linesRead
}

if isFirstScan {
continue
}

if w.ignorePattern != "" && regIgnore.Match(line) {
continue
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestNewWatcher(t *testing.T) {
caches := make(map[string]*cache.Cache)
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
watcher, err := NewWatcher(filePath, f, caches[filePath])
watcher.incrementScanCount()
assert.NoError(t, err)
assert.NotNil(t, watcher)

Expand Down Expand Up @@ -62,6 +63,7 @@ error:1`
caches := make(map[string]*cache.Cache)
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
watcher, err := NewWatcher(filePath, f, caches[filePath])
watcher.incrementScanCount()
assert.NoError(t, err)
defer watcher.Close()

Expand Down Expand Up @@ -91,6 +93,7 @@ line2`
caches := make(map[string]*cache.Cache)
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
watcher, err := NewWatcher(filePath, f, caches[filePath])
watcher.incrementScanCount()
assert.NoError(t, err)
defer watcher.Close()

Expand Down Expand Up @@ -134,6 +137,7 @@ error:1`
caches := make(map[string]*cache.Cache)
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
watcher, err := NewWatcher(filePath, f, caches[filePath])
watcher.incrementScanCount()
if err != nil {
b.Fatal(err)
}
Expand All @@ -160,6 +164,7 @@ func BenchmarkLoadAndSaveState(b *testing.B) {
caches := make(map[string]*cache.Cache)
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
watcher, err := NewWatcher(filePath, f, caches[filePath])
watcher.incrementScanCount()
if err != nil {
b.Fatal(err)
}
Expand All @@ -171,6 +176,7 @@ func BenchmarkLoadAndSaveState(b *testing.B) {
caches := make(map[string]*cache.Cache)
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
_, err := NewWatcher(filePath, f, caches[filePath])
watcher.incrementScanCount()
if err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -198,6 +204,7 @@ line2`
caches := make(map[string]*cache.Cache)
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
watcher, err := NewWatcher(filePath, f, caches[filePath])
watcher.incrementScanCount()
if err != nil {
b.Fatal(err)
}
Expand Down

0 comments on commit 16a783e

Please sign in to comment.