/
cost_all_period.go
80 lines (69 loc) · 1.7 KB
/
cost_all_period.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package teastats
import (
"github.com/TeaWeb/code/tealogs/accesslogs"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"strings"
)
// 耗时统计
type CostAllPeriodFilter struct {
CounterFilter
}
func (this *CostAllPeriodFilter) Name() string {
return "耗时统计"
}
func (this *CostAllPeriodFilter) Codes() []string {
return []string{
"cost.all.second",
"cost.all.minute",
"cost.all.hour",
"cost.all.day",
"cost.all.week",
"cost.all.month",
"cost.all.year",
}
}
func (this *CostAllPeriodFilter) Indexes() []string {
return []string{}
}
func (this *CostAllPeriodFilter) Start(queue *Queue, code string) {
if queue == nil {
logs.Println("stat queue should be specified for '" + code + "'")
return
}
this.queue = queue
this.queue.Index(this.Indexes())
this.IncreaseFunc = func(value maps.Map, inc maps.Map) maps.Map {
if inc == nil {
return value
}
if value == nil {
countReq := inc.GetInt64("countReq")
cost := inc.GetFloat64("cost")
value = maps.Map{
"countReq": countReq,
"cost": cost / float64(countReq),
}
} else {
totalReq := value.GetInt64("countReq")
totalCost := value.GetFloat64("cost") * float64(totalReq)
countReq := inc.GetInt64("countReq")
cost := inc.GetFloat64("cost")
value = maps.Map{
"countReq": totalReq + countReq,
"cost": (totalCost + cost) / float64(totalReq+countReq),
}
}
return value
}
this.StartFilter(code, code[strings.LastIndex(code, ".")+1:])
}
func (this *CostAllPeriodFilter) Filter(accessLog *accesslogs.AccessLog) {
this.ApplyFilter(accessLog, nil, maps.Map{
"countReq": 1,
"cost": accessLog.RequestTime,
})
}
func (this *CostAllPeriodFilter) Stop() {
this.StopFilter()
}