优化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 {
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 {
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 {

View File

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