forked from ant0ine/go-json-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
86 lines (73 loc) · 2.06 KB
/
status.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
package rest
import (
"fmt"
"net/http"
"os"
"sync"
"time"
)
type statusService struct {
lock sync.Mutex
start time.Time
pid int
responseCounts map[string]int
totalResponseTime time.Time
}
func newStatusService() *statusService {
return &statusService{
start: time.Now(),
pid: os.Getpid(),
responseCounts: map[string]int{},
totalResponseTime: time.Time{},
}
}
func (self *statusService) update(statusCode int, responseTime *time.Duration) {
self.lock.Lock()
self.responseCounts[fmt.Sprintf("%d", statusCode)]++
self.totalResponseTime = self.totalResponseTime.Add(*responseTime)
self.lock.Unlock()
}
type status struct {
Pid int
UpTime string
UpTimeSec float64
Time string
TimeUnix int64
StatusCodeCount map[string]int
TotalCount int
TotalResponseTime string
TotalResponseTimeSec float64
AverageResponseTime string
AverageResponseTimeSec float64
}
func (self *statusService) getStatus(w *Response, r *Request) {
now := time.Now()
uptime := now.Sub(self.start)
totalCount := 0
for _, count := range self.responseCounts {
totalCount += count
}
totalResponseTime := self.totalResponseTime.Sub(time.Time{})
averageResponseTime := time.Duration(0)
if totalCount > 0 {
avgNs := int64(totalResponseTime) / int64(totalCount)
averageResponseTime = time.Duration(avgNs)
}
st := &status{
Pid: self.pid,
UpTime: uptime.String(),
UpTimeSec: uptime.Seconds(),
Time: now.String(),
TimeUnix: now.Unix(),
StatusCodeCount: self.responseCounts,
TotalCount: totalCount,
TotalResponseTime: totalResponseTime.String(),
TotalResponseTimeSec: totalResponseTime.Seconds(),
AverageResponseTime: averageResponseTime.String(),
AverageResponseTimeSec: averageResponseTime.Seconds(),
}
err := w.WriteJson(st)
if err != nil {
http.Error(w, err.Error(), 500)
}
}