优化stat, 添加了source的相关统计

This commit is contained in:
M09Ic 2023-01-10 23:54:46 +08:00
parent b120d703b8
commit 880b11e860
2 changed files with 40 additions and 15 deletions

View File

@ -223,10 +223,12 @@ func (r *Runner) Prepare(ctx context.Context) error {
} }
if r.Color { if r.Color {
logs.Log.Important(pool.Statistor.ColorString()) logs.Log.Important(pool.Statistor.ColorString())
logs.Log.Important(pool.Statistor.ColorDetail()) logs.Log.Important(pool.Statistor.ColorCountString())
logs.Log.Important(pool.Statistor.ColorSourceString())
} else { } else {
logs.Log.Important(pool.Statistor.String()) logs.Log.Important(pool.Statistor.String())
logs.Log.Important(pool.Statistor.Detail()) logs.Log.Important(pool.Statistor.CountString())
logs.Log.Important(pool.Statistor.SourceString())
} }
if r.StatFile != nil { if r.StatFile != nil {

View File

@ -17,6 +17,7 @@ func NewStatistor(url string) *Statistor {
stat := DefaultStatistor stat := DefaultStatistor
stat.StartTime = time.Now().Unix() stat.StartTime = time.Now().Unix()
stat.Counts = make(map[int]int) stat.Counts = make(map[int]int)
stat.Sources = make(map[int]int)
stat.BaseUrl = url stat.BaseUrl = url
return &stat return &stat
} }
@ -30,6 +31,7 @@ func NewStatistorFromStat(origin *Statistor) *Statistor {
RuleFiles: origin.RuleFiles, RuleFiles: origin.RuleFiles,
RuleFilter: origin.RuleFilter, RuleFilter: origin.RuleFilter,
Counts: make(map[int]int), Counts: make(map[int]int),
Sources: map[int]int{},
StartTime: time.Now().Unix(), StartTime: time.Now().Unix(),
} }
} }
@ -37,6 +39,7 @@ func NewStatistorFromStat(origin *Statistor) *Statistor {
type Statistor struct { type Statistor struct {
BaseUrl string `json:"url"` BaseUrl string `json:"url"`
Counts map[int]int `json:"counts"` Counts map[int]int `json:"counts"`
Sources map[int]int `json:"sources"`
FailedNumber int32 `json:"failed"` FailedNumber int32 `json:"failed"`
ReqTotal int32 `json:"req_total"` ReqTotal int32 `json:"req_total"`
CheckNumber int `json:"check"` CheckNumber int `json:"check"`
@ -44,7 +47,6 @@ type Statistor struct {
FilteredNumber int `json:"filtered"` FilteredNumber int `json:"filtered"`
FuzzyNumber int `json:"fuzzy"` FuzzyNumber int `json:"fuzzy"`
WafedNumber int `json:"wafed"` WafedNumber int `json:"wafed"`
End int `json:"end"` End int `json:"end"`
Offset int `json:"offset"` Offset int `json:"offset"`
Total int `json:"total"` Total int `json:"total"`
@ -88,7 +90,7 @@ func (stat *Statistor) String() string {
return s.String() return s.String()
} }
func (stat *Statistor) Detail() string { func (stat *Statistor) CountString() string {
var s strings.Builder var s strings.Builder
s.WriteString("[stat] ") s.WriteString("[stat] ")
s.WriteString(stat.BaseUrl) s.WriteString(stat.BaseUrl)
@ -101,7 +103,17 @@ func (stat *Statistor) Detail() string {
return s.String() return s.String()
} }
func (stat *Statistor) ColorDetail() string { func (stat *Statistor) SourceString() string {
var s strings.Builder
s.WriteString("[stat] ")
s.WriteString(stat.BaseUrl)
for k, v := range stat.Sources {
s.WriteString(fmt.Sprintf(" %s: %d,", GetSourceName(k), v))
}
return s.String()
}
func (stat *Statistor) ColorCountString() string {
var s strings.Builder var s strings.Builder
s.WriteString("[stat] ") s.WriteString("[stat] ")
s.WriteString(stat.BaseUrl) s.WriteString(stat.BaseUrl)
@ -114,6 +126,16 @@ func (stat *Statistor) ColorDetail() string {
return s.String() return s.String()
} }
func (stat *Statistor) ColorSourceString() string {
var s strings.Builder
s.WriteString("[stat] ")
s.WriteString(stat.BaseUrl)
for k, v := range stat.Sources {
s.WriteString(fmt.Sprintf(" %s: %s,", logs.YellowBold(GetSourceName(k)), logs.YellowBold(strconv.Itoa(v))))
}
return s.String()
}
func (stat *Statistor) Json() string { func (stat *Statistor) Json() string {
content, err := json.Marshal(stat) content, err := json.Marshal(stat)
if err != nil { if err != nil {
@ -136,6 +158,7 @@ func ReadStatistors(filename string) (Statistors, error) {
} }
stats = append(stats, &stat) stats = append(stats, &stat)
} }
return stats, nil return stats, nil
} }