-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
199 lines (167 loc) · 3.13 KB
/
utils.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package gomtr
import (
"fmt"
"github.com/gogather/com"
"math"
"sort"
"strconv"
"time"
)
func fmtNumber(n float64) string {
return fmt.Sprintf("%.01f", n/1000)
}
func sortHasReply(array []*TTLData) bool {
for i := len(array) - 1; i >= 0; i-- {
item := array[i]
if item.status == "reply" {
return true
}
}
return false
}
func clearSummary(summary map[int]map[string]string) {
var ttlKeys []int
for k := range summary {
ttlKeys = append(ttlKeys, k)
}
sort.Ints(ttlKeys)
for i := len(ttlKeys) - 1; i >= 0; i-- {
k := ttlKeys[i]
if summary[k]["IP"] == "???" && summary[k-1]["IP"] == "???" && i > 0 {
delete(summary, k)
} else {
return
}
}
}
func sortLastTTLData(array []*TTLData) *TTLData {
l := len(array)
for i := l - 1; i >= 0; i-- {
item := array[i]
if item.err == nil {
return item
}
}
return array[l-1]
}
func sortIP(array []*TTLData) string {
last := sortLastTTLData(array)
if last.err != nil {
return "???"
} else {
return last.ip
}
}
func sortLast(array []*TTLData) float64 {
for i := len(array) - 1; i >= 0; i-- {
item := array[i]
if item.err == nil {
return float64(item.time)
}
}
return 0
}
func sortSntReality(array []*TTLData) int {
if len(array) <= 0 || array == nil {
return 0
}
c := 0
for i := 0; i < len(array); i++ {
item := array[i]
if item.err == nil {
c++
}
}
return c
}
func sortAvg(array []*TTLData) float64 {
if len(array) <= 0 || array == nil {
return 0
}
var result int64 = 0
c := 0
for i := 0; i < len(array); i++ {
item := array[i]
if item.err == nil {
result = result + item.time
c++
}
}
if c <= 0 {
c = 1
}
return float64(result) / float64(c)
}
func sortBest(array []*TTLData) float64 {
var best int64 = -1
for i := 0; i < len(array); i++ {
item := array[i]
if item.err == nil {
if item.time > best {
best = item.time
}
}
}
if best < 0 {
best = 0
}
return float64(best)
}
func sortWorst(array []*TTLData) float64 {
var worst int64 = array[0].time
for i := 0; i < len(array); i++ {
item := array[i]
if item.err == nil {
if item.time < worst {
worst = item.time
}
}
}
return float64(worst)
}
// 标准偏差统计
func sortSTDev(array []*TTLData) float64 {
if len(array) <= 0 || array == nil {
return 0
}
avg := sortAvg(array)
var s float64 = 0
var c int = 0
for i := 0; i < len(array); i++ {
item := array[i]
if item.err == nil {
c++
itemTime := float64(item.time)
d := itemTime - avg
delta := d * d
s = s + delta
}
}
if c <= 0 {
c = 1
}
stdev := math.Sqrt(s / float64(c-1))
if math.IsNaN(stdev) {
stdev = float64(0)
}
return stdev
}
func getTTLID(fullID int64) int {
idStr := fmt.Sprintf("%d", fullID)
length := len(idStr)
ttlStr := com.SubString(idStr, length-4, 4)
ttl, e := strconv.Atoi(ttlStr)
if e != nil {
ttl = 0
}
return ttl
}
func getRealID(fullID int64) int64 {
return fullID / 10000
}
func getMtrStartTime() string {
now := time.Now()
week := com.SubString(now.Weekday().String(), 0, 3)
month := com.SubString(now.Month().String(), 0, 3)
return fmt.Sprintf("%s %s %d %s", week, month, now.Day(), now.Format("15:04:05 2006"))
}