forked from megaease/easeprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
170 lines (152 loc) · 4.91 KB
/
filter.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
/*
* 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:https://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 report
import (
"fmt"
"strings"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/probe"
log "github.com/sirupsen/logrus"
)
// SLAFilter filter the probers
type SLAFilter struct {
Name string
Kind string
Endpoint string
Status *probe.Status
SLAGreater float64
SLALess float64
Message string
PageNum int
PageSize int
total int // the total number of probers
cnt int // the number of probers that match the filter
}
// NewEmptyFilter create a new SLAFilter
func NewEmptyFilter() *SLAFilter {
return &SLAFilter{
Name: "",
Kind: "",
Endpoint: "",
Status: nil,
SLAGreater: 0,
SLALess: 100,
Message: "",
PageNum: 1,
PageSize: global.DefaultPageSize,
total: 0,
cnt: 0,
}
}
// Check check the filter is valid or not
func (f *SLAFilter) Check() error {
log.Debugf("[Web] Check filter: %+v", f)
if f.SLAGreater > f.SLALess {
return fmt.Errorf("Error: Invalid SLA filter: gte(%0.2f) > (%0.2f)", f.SLAGreater, f.SLALess)
}
if f.SLAGreater > 100 || f.SLAGreater < 0 {
return fmt.Errorf("Error: Invalid SLA filter: gte(%0.2f), it must be between 0 - 100", f.SLAGreater)
}
if f.SLALess > 100 || f.SLALess < 0 {
return fmt.Errorf("Error: Invalid SLA filter: lte(%0.2f), it must be between 0 - 100", f.SLALess)
}
if f.PageNum < 1 {
return fmt.Errorf("Error: Invalid page number: %d, it must be greater than 0", f.PageNum)
}
if f.PageSize < 1 {
return fmt.Errorf("Error: Invalid page size: %d, it must be greater than 0", f.PageSize)
}
return nil
}
// HTML return the HTML format string
func (f *SLAFilter) HTML() string {
span := "<span style=\"font-size:9pt; background-color:#666; color:white; padding:0 5px;border-radius: 3px;\">"
_span := "</span> "
result := ""
if strings.TrimSpace(f.Name) != "" {
result += fmt.Sprintf(span+"<b>Name</b>: %s"+_span, f.Name)
}
if strings.TrimSpace(f.Kind) != "" {
result += fmt.Sprintf(span+"<b>Kind</b>: %s"+_span, f.Kind)
}
if strings.TrimSpace(f.Endpoint) != "" {
result += fmt.Sprintf(span+"<b>Endpoint</b>: %s"+_span, f.Endpoint)
}
if f.Status != nil {
result += fmt.Sprintf(span+"<b>Status</b>: %s"+_span, f.Status.String())
}
if strings.TrimSpace(f.Message) != "" {
result += fmt.Sprintf(span+"<b>Message</b>: %s"+_span, f.Message)
}
if f.SLAGreater > 0 || f.SLALess < 100 {
result += fmt.Sprintf(span+"<b>SLA</b>: %.2f%% - %.2f%% "+_span, f.SLAGreater, f.SLALess)
}
color := "#c00"
if f.PageNum <= f.cnt/f.PageSize+1 {
color = "#4E944F"
}
span = `<span style="font-size:9pt; background-color:` + color + `; color:white; padding:0 5px; margin-left:10px;border-radius: 3px;">`
result += fmt.Sprintf(span+"<b>Page %d / %d</b>"+_span, f.PageNum, f.cnt/f.PageSize+1)
span = `<span style="font-size:9pt; background-color:#4E944F; color:white; padding:0 5px; margin-left:10px;border-radius: 3px;">`
result += fmt.Sprintf(span+"<b>%d / %d Probers found!</b>"+_span, f.cnt, f.total)
result += "<br><br>"
return result
}
func (f *SLAFilter) getIndics() (start int, end int) {
start = (f.PageNum - 1) * f.PageSize
end = f.PageNum*f.PageSize - 1
return start, end
}
// Filter filter the probers
func (f *SLAFilter) Filter(probers []probe.Prober) []probe.Prober {
start, end := f.getIndics()
f.cnt = 0
result := make([]probe.Prober, 0)
for _, p := range probers {
// if the name is not empty then filter by name
if f.Name != "" && !strings.Contains(p.Name(), f.Name) {
continue
}
// if the kind is not empty then filter by kind
if f.Kind != "" && p.Kind() != f.Kind {
continue
}
// if the endpoint is not empty then filter by endpoint
if f.Endpoint != "" && !strings.Contains(p.Result().Endpoint, f.Endpoint) {
continue
}
// if the status is not right then ignore it
if f.Status != nil && p.Result().Status != *f.Status {
continue
}
// if the message is not empty then filter by message
if f.Message != "" && !strings.Contains(p.Result().Message, f.Message) {
continue
}
//if the SLA is not right then ignore it
percent := p.Result().SLAPercent()
if percent < f.SLAGreater || percent > f.SLALess {
continue
}
if f.cnt >= start && f.cnt <= end { // if the prober is in the page
result = append(result, p)
}
f.cnt++ // how many probers are filtered
}
f.total = len(probers)
return result
}