@@ -19,6 +19,7 @@ import (
1919 "github.com/schollz/progressbar/v3"
2020 "github.com/spf13/pflag"
2121 "github.com/taoky/ayano/pkg/fileiter"
22+ "github.com/taoky/ayano/pkg/grep"
2223 "github.com/taoky/ayano/pkg/parser"
2324 "github.com/taoky/ayano/pkg/util"
2425)
@@ -153,16 +154,15 @@ type AnalyzerConfig struct {
153154 Parser string
154155 PrefixV4 int
155156 PrefixV6 int
156- PrintDelta SizeFlag
157+ PrintDelta util. SizeFlag
157158 RefreshSec int
158159 RepeatWarn time.Duration
159- Server string
160160 SortBy SortByFlag
161- Threshold SizeFlag
162161 TopN int
163162 Truncate bool
164163 Truncate2 int
165164 Whole bool
165+ Filter grep.Filter
166166
167167 Analyze bool
168168 Daemon bool
@@ -181,13 +181,13 @@ func (c *AnalyzerConfig) InstallFlags(flags *pflag.FlagSet, cmdname string) {
181181 flags .IntVar (& c .PrefixV6 , "prefixv6" , c .PrefixV6 , "Group IPv6 by prefix" )
182182 flags .DurationVar (& c .RepeatWarn , "repeat-warn" , c .RepeatWarn , "Highlight repeated URL visits longer than duration" )
183183 flags .IntVarP (& c .RefreshSec , "refresh" , "r" , c .RefreshSec , "Refresh interval in seconds" )
184- flags .StringVarP (& c .Server , "server" , "s" , c .Server , "Server IP to filter (nginx-json only)" )
185184 flags .VarP (& c .SortBy , "sort-by" , "S" , "Sort result by (size|requests)" )
186- flags .VarP (& c .Threshold , "threshold" , "t" , "Threshold size for request (only requests at least this large will be counted)" )
187185 flags .IntVarP (& c .TopN , "top" , "n" , c .TopN , "Number of top items to show" )
188186 flags .BoolVar (& c .Truncate , "truncate" , c .Truncate , "Truncate long URLs from output" )
189187 flags .IntVar (& c .Truncate2 , "truncate-to" , c .Truncate2 , "Truncate URLs to given length, overrides --truncate" )
190188
189+ c .Filter .InstallFlags (flags )
190+
191191 flags .StringVar (& c .CpuProfile , "cpuprof" , c .CpuProfile , "Write CPU profiling information" )
192192 flags .StringVar (& c .MemProfile , "memprof" , c .MemProfile , "Write memory profiling information" )
193193
@@ -209,14 +209,16 @@ func (c *AnalyzerConfig) UseLock() bool {
209209}
210210
211211func DefaultConfig () AnalyzerConfig {
212+ filter := grep.Filter {}
213+ filter .Threshold = util .SizeFlag (10e6 )
212214 return AnalyzerConfig {
213215 Parser : "nginx-json" ,
214216 PrefixV4 : 24 ,
215217 PrefixV6 : 48 ,
216- PrintDelta : SizeFlag (1e9 ),
218+ PrintDelta : util . SizeFlag (1e9 ),
217219 RefreshSec : 5 ,
218220 SortBy : SortBySize ,
219- Threshold : SizeFlag ( 10e6 ) ,
221+ Filter : filter ,
220222 TopN : 10 ,
221223 }
222224}
@@ -357,14 +359,8 @@ func (a *Analyzer) handleLogItem(logItem parser.LogItem) error {
357359 return nil
358360 }
359361
360- // Filter by server
361- if a .Config .Server != "" && logItem .Server != a .Config .Server {
362- return nil
363- }
364-
365- // Filter by sent size
366- size := logItem .Size
367- if size < uint64 (a .Config .Threshold ) {
362+ // Filter
363+ if err := a .Config .Filter .Match (logItem ); err != nil {
368364 return nil
369365 }
370366
@@ -385,7 +381,7 @@ func (a *Analyzer) handleLogItem(logItem parser.LogItem) error {
385381
386382 if a .Config .Analyze || a .Config .Daemon {
387383 // Avoid using double memory when not in interactive mode
388- updateStats (StatKey {a .Config .Server , clientPrefix })
384+ updateStats (StatKey {a .Config .Filter . Server , clientPrefix })
389385 } else {
390386 updateStats (StatKey {logItem .Server , clientPrefix })
391387
@@ -396,7 +392,7 @@ func (a *Analyzer) handleLogItem(logItem parser.LogItem) error {
396392 }
397393
398394 if a .Config .Daemon {
399- ipStats := a .stats [StatKey {a .Config .Server , clientPrefix }]
395+ ipStats := a .stats [StatKey {a .Config .Filter . Server , clientPrefix }]
400396 delta := ipStats .Size - ipStats .LastSize
401397 if ipStats .LastSize == 0 {
402398 ipStats .FirstSeen = logItem .Time
@@ -410,8 +406,8 @@ func (a *Analyzer) handleLogItem(logItem parser.LogItem) error {
410406 logItem .URL )
411407 }
412408 ipStats .LastSize += printTimes * uint64 (a .Config .PrintDelta )
413- // Just update [StatKey{a.Config.Server, clientPrefix}] here, as the config would not be updated runtime now
414- a .stats [StatKey {a .Config .Server , clientPrefix }] = ipStats
409+ // Just update [StatKey{a.Config.Filter. Server, clientPrefix}] here, as the config would not be updated runtime now
410+ a .stats [StatKey {a .Config .Filter . Server , clientPrefix }] = ipStats
415411 }
416412
417413 if a .Config .DirAnalyze {
0 commit comments