forked from megaease/easeprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
177 lines (163 loc) · 4.65 KB
/
result.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (c) 2022, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package probe
import (
"encoding/json"
"fmt"
"time"
"github.com/megaease/easeprobe/global"
log "github.com/sirupsen/logrus"
)
// Stat is the statistics of probe result
type Stat struct {
Since time.Time `json:"since" yaml:"since"`
Total int64 `json:"total" yaml:"total"`
Status map[Status]int64 `json:"status" yaml:"status"`
UpTime time.Duration `json:"uptime" yaml:"uptime"`
DownTime time.Duration `json:"downtime" yaml:"downtime"`
StatusCounter
}
// Result is the status of health check
type Result struct {
Name string `json:"name" yaml:"name"`
Endpoint string `json:"endpoint" yaml:"endpoint"`
StartTime time.Time `json:"time" yaml:"time"`
StartTimestamp int64 `json:"timestamp" yaml:"timestamp"`
RoundTripTime time.Duration `json:"rtt" yaml:"rtt"`
Status Status `json:"status" yaml:"status"`
PreStatus Status `json:"prestatus" yaml:"prestatus"`
Message string `json:"message" yaml:"message"`
LatestDownTime time.Time `json:"latestdowntime" yaml:"latestdowntime"`
RecoveryDuration time.Duration `json:"recoverytime" yaml:"recoverytime"`
Stat Stat `json:"stat" yaml:"stat"`
}
// NewResult return a Result object
func NewResult() *Result {
return &Result{
Name: "",
Endpoint: "",
StartTime: time.Now().UTC(),
StartTimestamp: 0,
RoundTripTime: 0,
Status: StatusInit,
PreStatus: StatusInit,
Message: "",
LatestDownTime: time.Time{},
RecoveryDuration: 0,
Stat: Stat{
Since: time.Now().UTC(),
Total: 0,
Status: map[Status]int64{},
UpTime: 0,
DownTime: 0,
StatusCounter: *NewStatusCounter(global.DefaultStatusChangeThresholdSetting),
},
}
}
// NewResultWithName return a Result object with name
func NewResultWithName(name string) *Result {
r := GetResultData(name)
if r != nil {
return r
}
r = NewResult()
r.Name = name
SetResultData(name, r)
return r
}
// Clone return a clone of the Result
func (r *Result) Clone() Result {
dst := Result{}
dst.Name = r.Name
dst.Endpoint = r.Endpoint
dst.StartTime = r.StartTime
dst.StartTimestamp = r.StartTimestamp
dst.RoundTripTime = r.RoundTripTime
dst.Status = r.Status
dst.PreStatus = r.PreStatus
dst.Message = r.Message
dst.LatestDownTime = r.LatestDownTime
dst.RecoveryDuration = r.RecoveryDuration
dst.Stat = r.Stat.Clone()
return dst
}
// Clone return a clone of the Stat
func (s *Stat) Clone() Stat {
dst := Stat{}
dst.Since = s.Since
dst.Total = s.Total
dst.Status = make(map[Status]int64)
for k, v := range s.Status {
dst.Status[k] = v
}
dst.UpTime = s.UpTime
dst.DownTime = s.DownTime
dst.StatusCounter = s.StatusCounter.Clone()
return dst
}
// DoStat is the function do the statistics
func (r *Result) DoStat(d time.Duration) {
r.Stat.Total++
r.Stat.Status[r.Status]++
if r.Status == StatusUp {
r.Stat.UpTime += d
} else {
r.Stat.DownTime += d
}
}
// Title return the title for notification
func (r *Result) Title() string {
t := ""
if r.PreStatus == StatusInit && r.Status == StatusUp {
t = "Monitoring %s"
} else if r.Status != StatusUp {
t = "%s Failure"
} else {
t = "%s Recovery - ( " + r.RecoveryDuration.Round(time.Second).String() + " Downtime )"
}
return fmt.Sprintf(t, r.Name)
}
// DebugJSON convert the object to DebugJSON
func (r *Result) DebugJSON() string {
j, err := json.Marshal(&r)
if err != nil {
log.Errorf("error: %v", err)
return ""
}
return string(j)
}
// DebugJSONIndent convert the object to indent JSON
func (r *Result) DebugJSONIndent() string {
j, err := json.MarshalIndent(&r, "", " ")
if err != nil {
log.Errorf("error: %v", err)
return ""
}
return string(j)
}
// SLAPercent calculate the SLAPercent
func (r *Result) SLAPercent() float64 {
uptime := r.Stat.UpTime.Seconds()
downtime := r.Stat.DownTime.Seconds()
if uptime+downtime <= 0 {
if r.Status == StatusUp {
return 100
}
return 0
}
return uptime / (uptime + downtime) * 100
}