Skip to content

Commit

Permalink
Allow changing sort by runtime with 'S' key
Browse files Browse the repository at this point in the history
  • Loading branch information
taoky committed Sep 2, 2024
1 parent 38dc213 commit e7f88c9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
8 changes: 4 additions & 4 deletions pkg/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type AnalyzerConfig struct {
Parser string
RefreshSec int
Server string
SortBy string
SortBy SortByFlag
Threshold SizeFlag
TopN int
Whole bool
Expand All @@ -78,7 +78,7 @@ func (c *AnalyzerConfig) InstallFlags(flags *pflag.FlagSet) {
flags.StringVarP(&c.Parser, "parser", "p", c.Parser, "Log parser (nginx-combined|nginx-json|caddy-json|goaccess)")
flags.IntVarP(&c.RefreshSec, "refresh", "r", c.RefreshSec, "Refresh interval in seconds")
flags.StringVarP(&c.Server, "server", "s", c.Server, "Server IP to filter (nginx-json only)")
flags.StringVarP(&c.SortBy, "sort-by", "S", c.Server, "Sort result by (size|requests)")
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.BoolVarP(&c.Whole, "whole", "w", c.Whole, "Analyze whole log file and then tail it")
Expand All @@ -91,7 +91,7 @@ func DefaultConfig() AnalyzerConfig {
return AnalyzerConfig{
Parser: "nginx-json",
RefreshSec: 5,
SortBy: "size",
SortBy: SortBySize,
Threshold: SizeFlag(10e6),
TopN: 10,
}
Expand Down Expand Up @@ -195,7 +195,7 @@ func (a *Analyzer) handleLine(line []byte) error {
return nil
}

func (a *Analyzer) PrintTopValues(displayRecord map[netip.Prefix]time.Time, sortBy string, serverFilter string) {
func (a *Analyzer) PrintTopValues(displayRecord map[netip.Prefix]time.Time, sortBy SortByFlag, serverFilter string) {
activeConn := make(map[netip.Prefix]int)
if !a.Config.NoNetstat {
// Get active connections
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyze/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func benchmarkAnalyzeLoop(b *testing.B, parserStr string) {
}

a.RunLoop(t)
a.PrintTopValues(nil, "size", "")
a.PrintTopValues(nil, SortBySize, "")
}

func BenchmarkAnalyzeLoopNgxJSON(b *testing.B) {
Expand Down
16 changes: 6 additions & 10 deletions pkg/analyze/sortfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,29 @@ import (

type SortFunc func(l, r StatKey) int

var sortFuncs = map[string]func(a map[StatKey]IPStats) SortFunc{
"size": func(i map[StatKey]IPStats) SortFunc {
var sortFuncs = map[SortByFlag]func(a map[StatKey]IPStats) SortFunc{
SortBySize: func(i map[StatKey]IPStats) SortFunc {
return func(l, r StatKey) int {
return int(i[r].Size - i[l].Size)
}
},
"requests": func(i map[StatKey]IPStats) SortFunc {
SortByRequests: func(i map[StatKey]IPStats) SortFunc {
return func(l, r StatKey) int {
return int(i[r].Requests - i[l].Requests)
}
},
}

func init() {
sortFuncs["reqs"] = sortFuncs["requests"]
}

func GetSortFunc(name string, i map[StatKey]IPStats) SortFunc {
func GetSortFunc(name SortByFlag, i map[StatKey]IPStats) SortFunc {
fn, ok := sortFuncs[name]
if !ok {
return nil
}
return fn(i)
}

func ListSortFuncs() []string {
ret := make([]string, 0, len(sortFuncs))
func ListSortFuncs() []SortByFlag {
ret := make([]SortByFlag, 0, len(sortFuncs))
for key := range sortFuncs {
ret = append(ret, key)
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/analyze/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package analyze

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

Expand Down Expand Up @@ -33,6 +34,33 @@ func (s SizeFlag) Type() string {
return "size"
}

type SortByFlag string

const (
SortBySize SortByFlag = "size"
SortByRequests SortByFlag = "requests"
)

func (s SortByFlag) String() string {
return string(s)
}

func (s *SortByFlag) Set(value string) error {
switch value {
case "size":
*s = SortBySize
case "requests", "reqs":
*s = SortByRequests
default:
return errors.New(`must be one of "size" or "requests"`)
}
return nil
}

func (s SortByFlag) Type() string {
return "size|requests"
}

func IPPrefix(ip netip.Addr) netip.Prefix {
var clientPrefix netip.Prefix
if ip.Is4() {
Expand Down
21 changes: 16 additions & 5 deletions pkg/tui/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const (

const helpMsg = `Available shortcuts:
t/T: print total size aggregated by server
s/S: get user input for server filtering
s: set server filtering
S: change sort by
?: help`

type Tui struct {
Expand All @@ -27,6 +28,7 @@ type Tui struct {
displayRecord map[netip.Prefix]time.Time
serverFilter string
mode ShowMode
sortBy analyze.SortByFlag
ticker *time.Ticker
refreshChan chan struct{}
inputChan chan byte
Expand All @@ -38,6 +40,7 @@ func New(analyzer *analyze.Analyzer) *Tui {
analyzer: analyzer,
displayRecord: make(map[netip.Prefix]time.Time),
mode: TopValues,
sortBy: analyzer.Config.SortBy,
refreshChan: make(chan struct{}),
inputChan: make(chan byte),
}
Expand All @@ -54,7 +57,7 @@ func (t *Tui) Run() {
t.handleInput(k)
case <-t.refreshChan:
if t.mode == TopValues {
a.PrintTopValues(t.displayRecord, "size", t.serverFilter)
a.PrintTopValues(t.displayRecord, t.sortBy, t.serverFilter)
} else {
a.PrintTotal()
}
Expand All @@ -65,8 +68,16 @@ func (t *Tui) Run() {

func (t *Tui) handleInput(key byte) {
switch key {
case 'S', 's':
t.handleS()
case 's':
t.handles()
case 'S':
if t.sortBy == analyze.SortBySize {
t.sortBy = analyze.SortByRequests
fmt.Println("Switched to sort by requests")
} else {
t.sortBy = analyze.SortBySize
fmt.Println("Switched to sort by size")
}
case 'T', 't':
if t.mode == TopValues {
t.mode = Total
Expand All @@ -82,7 +93,7 @@ func (t *Tui) handleInput(key byte) {
go t.waitForOneByte()
}

func (t *Tui) handleS() {
func (t *Tui) handles() {
t.noPrint.Store(true)
defer t.noPrint.Store(false)

Expand Down

0 comments on commit e7f88c9

Please sign in to comment.