Skip to content

Commit

Permalink
Add --truncate for long URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Sep 5, 2024
1 parent 4af7e64 commit 6a3096b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type AnalyzerConfig struct {
SortBy SortByFlag
Threshold SizeFlag
TopN int
Truncate bool
Whole bool

Analyze bool
Expand All @@ -99,6 +100,7 @@ func (c *AnalyzerConfig) InstallFlags(flags *pflag.FlagSet) {
flags.VarP(&c.SortBy, "sort-by", "S", "Sort result by (size|requests)")
flags.VarP(&c.Threshold, "threshold", "t", "Threshold size for request (only requests at least this large will be counted)")
flags.IntVarP(&c.TopN, "top", "n", c.TopN, "Number of top items to show")
flags.BoolVar(&c.Truncate, "truncate", c.Truncate, "Truncate long URLs from output")
flags.BoolVarP(&c.Whole, "whole", "w", c.Whole, "Analyze whole log file and then tail it")
}
func (c *AnalyzerConfig) UseLock() bool {
Expand Down Expand Up @@ -306,6 +308,9 @@ func (a *Analyzer) PrintTopValues(displayRecord map[netip.Prefix]time.Time, sort
total := ipStats.Size
reqTotal := ipStats.Requests
last := ipStats.LastURL
if a.Config.Truncate {
last = TruncateURLPath(last)
}

var lastUpdateTime string
var lastAccessTime string
Expand Down
11 changes: 11 additions & 0 deletions pkg/analyze/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package analyze

import (
"errors"
"fmt"
"net/netip"
"strconv"
"strings"

"github.com/dustin/go-humanize"
)
Expand Down Expand Up @@ -70,3 +72,12 @@ func (a *Analyzer) IPPrefix(ip netip.Addr) netip.Prefix {
}
return clientPrefix.Masked()
}

func TruncateURLPath(input string) string {
count := strings.Count(input, "/")
if count <= 2 {
return input
}
parts := strings.Split(input, "/")
return fmt.Sprintf("/%s/.../%s", parts[1], parts[count])
}

0 comments on commit 6a3096b

Please sign in to comment.