Skip to content

Commit

Permalink
Add --sort-by flag for analyze (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug authored Aug 30, 2024
1 parent e727119 commit 296de83
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func printTopValuesRoutine(a *analyze.Analyzer) {
displayRecord := make(map[netip.Prefix]time.Time)
ticker := time.NewTicker(time.Duration(a.Config.RefreshSec) * time.Second)
for range ticker.C {
a.PrintTopValues(displayRecord)
a.PrintTopValues(displayRecord, "size")
fmt.Println()
}
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func runWithConfig(cmd *cobra.Command, args []string, config analyze.AnalyzerCon
analyzer.RunLoop(iterator)

if config.Analyze {
analyzer.PrintTopValues(nil)
analyzer.PrintTopValues(nil, config.SortBy)
}
return nil
}
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"github.com/taoky/ayano/cmd"
)
import "github.com/taoky/ayano/cmd"

func main() {
cmd.RootCmd().Execute()
Expand Down
12 changes: 8 additions & 4 deletions pkg/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type AnalyzerConfig struct {
Parser string
RefreshSec int
Server string
SortBy string
Threshold SizeFlag
TopN int
Whole bool
Expand All @@ -71,6 +72,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.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 @@ -83,6 +85,7 @@ func DefaultConfig() AnalyzerConfig {
return AnalyzerConfig{
Parser: "nginx-json",
RefreshSec: 5,
SortBy: "size",
Threshold: SizeFlag(10e6),
TopN: 10,
}
Expand Down Expand Up @@ -185,7 +188,7 @@ func (a *Analyzer) handleLine(line []byte) error {
a.ipInfo[clientPrefix] = ipStats
return nil
}
func (a *Analyzer) PrintTopValues(displayRecord map[netip.Prefix]time.Time) {
func (a *Analyzer) PrintTopValues(displayRecord map[netip.Prefix]time.Time, sortBy string) {
activeConn := make(map[netip.Prefix]int)
if !a.Config.NoNetstat {
// Get active connections
Expand Down Expand Up @@ -229,9 +232,10 @@ func (a *Analyzer) PrintTopValues(displayRecord map[netip.Prefix]time.Time) {
for k := range a.ipInfo {
keys = append(keys, k)
}
slices.SortFunc(keys, func(l, r netip.Prefix) int {
return int(a.ipInfo[r].Size - a.ipInfo[l].Size)
})
sortFunc := GetSortFunc(sortBy, a.ipInfo)
if sortFunc != nil {
slices.SortFunc(keys, sortFunc)
}

// print top N
top := a.Config.TopN
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)
a.PrintTopValues(nil, "size")
}

func BenchmarkAnalyzeLoopNgxJSON(b *testing.B) {
Expand Down
42 changes: 42 additions & 0 deletions pkg/analyze/sortfunc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package analyze

import (
"net/netip"
"slices"
)

type SortFunc func(l, r netip.Prefix) int

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

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

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

func ListSortFuncs() []string {
ret := make([]string, 0, len(sortFuncs))
for key := range sortFuncs {
ret = append(ret, key)
}
slices.Sort(ret)
return ret
}

0 comments on commit 296de83

Please sign in to comment.