-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdstatus.go
160 lines (145 loc) · 3.57 KB
/
dstatus.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package router
import (
"bytes"
"html/template"
"log"
"math"
"net/http"
"strings"
"sync"
"time"
)
type AdditionalPrintF func() string
type Dstatus struct {
tmpl *template.Template
data DstatusData
actions DstatusActions
Additional AdditionalPrintF
IsDetailed bool
}
type DstatusData struct {
Name string
Port string
Country string
CommitId string
CommitName string
StartTime time.Time
}
func (dstatus *Dstatus) init() {
dstatus.tmpl = template.New("dstatus")
if dstatus.data.StartTime.IsZero() {
dstatus.data.StartTime = time.Now()
}
var err error
//dstatus.tmpl, err = template.ParseFiles(path.Dir(filename) + "/template/dstatus.html")
dstatus.tmpl, err = template.ParseFiles("./template/dstatus.html")
if err != nil {
log.Fatal(err)
}
dstatus.actions.m = make(DstatusActionMap)
}
func (dstatus *Dstatus) Show(writer http.ResponseWriter, request *http.Request, uriParts []string) {
//"↳"
var tpl bytes.Buffer
additionalData := ""
if dstatus.Additional != nil {
additionalData = dstatus.Additional()
}
dstatus.actions.RLock()
defer dstatus.actions.RUnlock()
data := struct {
Name string
Ip string
Port string
Country string
Uri string
StartTime time.Time
Duration time.Duration
CommitId string
CommitName string
Map DstatusActionMap
AdditionalData template.HTML
}{
Ip: strings.Split(request.RemoteAddr, ":")[0],
Uri: request.URL.String(),
Name: dstatus.data.Name,
Port: dstatus.data.Port,
StartTime: dstatus.data.StartTime,
Duration: time.Since(dstatus.data.StartTime),
CommitId: dstatus.data.CommitId,
CommitName: dstatus.data.CommitName,
Map: dstatus.actions.m,
Country: "Russia",
AdditionalData: template.HTML(additionalData),
}
if err := dstatus.tmpl.Execute(&tpl, data); err != nil {
log.Fatal(err)
}
writer.Write([]byte(tpl.String()))
}
func (dstatus *Dstatus) HandleWrapper(f HandlerFunc) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, uriParts []string) {
start := time.Now()
f(w, r, uriParts)
for idx := range uriParts {
action := strings.Join(uriParts[0:idx], "↳")
if len(action) > 0 {
dstatus.actions.update(action, time.Since(start).Seconds())
}
if !dstatus.IsDetailed && idx > 0 {
break
}
}
}
}
func NewDstatus(data DstatusData) *Dstatus {
dstatus := Dstatus{data: data}
dstatus.init()
return &dstatus
}
type DstatusActionData struct {
Count uint64
PerSec float64
MinExecTime float64
MaxExecTime float64
ExecTime float64
AvgExecTime float64
LongRequests uint64
Start time.Time
Last time.Time
}
type DstatusActionMap map[string]*DstatusActionData
type DstatusActions struct {
sync.RWMutex
m DstatusActionMap
}
func (actions *DstatusActions) update(action string, d float64) {
actions.Lock()
defer actions.Unlock()
var longRequests uint64
if d > 1 {
longRequests = 1
}
now := time.Now()
if val, ok := actions.m[action]; ok {
val.Count += 1
val.MinExecTime = math.Min(val.MinExecTime, d)
val.MaxExecTime = math.Max(val.MaxExecTime, d)
val.ExecTime += d
val.LongRequests += longRequests
val.AvgExecTime = val.ExecTime / float64(val.Count)
val.PerSec = float64(val.Count) / (time.Since(val.Start).Seconds())
val.Last = now
} else {
actions.m[action] = &DstatusActionData{
Count: 1,
MinExecTime: d,
MaxExecTime: d,
ExecTime: d,
LongRequests: longRequests,
AvgExecTime: d,
Start: now,
Last: now,
}
}
}