-
Notifications
You must be signed in to change notification settings - Fork 7
/
mega_http_dispatcher.go
54 lines (43 loc) · 1.47 KB
/
mega_http_dispatcher.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
package main
import "fmt"
func Process(server Server, servIndex int) {
logcurrent := RequestLog{}
ShouldDeleteLog(server.ID)
LoadLog(server.ID, &logcurrent)
for _, endpointCheck := range server.Endpoints {
reqframe := Req(server, endpointCheck)
apiid := fmt.Sprintf(urlformat, endpointCheck.Method, endpointCheck.Path)
logcurrent.Requests = append(logcurrent.Requests, Request{Code: reqframe, Owner: apiid})
}
// Lock log file
SaveLog(server.ID, &logcurrent)
for endIndex, endpointCheck := range server.Endpoints {
apiid := fmt.Sprintf(urlformat, endpointCheck.Method, endpointCheck.Path)
success, failed := CountAndReturn(logcurrent.Requests, apiid)
ShouldLock()
Config.Servers[servIndex].Endpoints[endIndex].Uptime = float64(success) / float64(success+failed)
ShouldUnlock()
}
success, failed := CountAndReturn(logcurrent.Requests, EmptyString)
ShouldLock()
Config.Servers[servIndex].Uptime = float64(success) / float64(success+failed)
latestServerInformation := Config.Servers[servIndex]
ShouldUnlock()
if Config.AlertsHistory == nil {
ClearHistory()
}
go Notify(latestServerInformation, Config.Contacts, Config.Mail, Config.SMS)
go SaveConfig(&Config)
}
func CountAndReturn(requests []Request, owner string) (success int, failed int) {
for _, reqcap := range requests {
if reqcap.Owner == owner || owner == EmptyString {
if reqcap.Code < MaxPossibleHTTPSuccessCode {
success++
} else {
failed++
}
}
}
return success, failed
}