Skip to content

Commit

Permalink
analyze: Use unique.Handle for UA
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Jan 27, 2025
1 parent bba3e1e commit 8721739
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pkg/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"sync"
"time"
"unique"

"github.com/cakturk/go-netstat/netstat"
"github.com/dustin/go-humanize"
Expand All @@ -28,6 +29,8 @@ var (
tableColorBold = tablewriter.Colors{tablewriter.Bold}
)

type UAKeyType = unique.Handle[string]

type IPStats struct {
Size uint64
Requests uint64
Expand All @@ -44,7 +47,7 @@ type IPStats struct {
LastURLAccess time.Time

// User-agent
UAStore map[string]struct{}
UAStore map[UAKeyType]struct{}
}

func (i IPStats) UpdateWith(item parser.LogItem) IPStats {
Expand All @@ -62,13 +65,9 @@ func (i IPStats) UpdateWith(item parser.LogItem) IPStats {
}
}
if i.UAStore == nil {
i.UAStore = make(map[string]struct{})
}
if len(item.Useragent) <= 50 {
i.UAStore[item.Useragent] = struct{}{}
} else {
i.UAStore[item.Useragent[:50]] = struct{}{}
i.UAStore = make(map[UAKeyType]struct{})
}
i.UAStore[unique.Make(item.Useragent)] = struct{}{}
return i
}

Expand Down

0 comments on commit 8721739

Please sign in to comment.