-
Notifications
You must be signed in to change notification settings - Fork 6
/
ping.go
54 lines (50 loc) · 1.45 KB
/
ping.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 stats
import (
"github.com/NodeFactoryIo/vedran/internal/models"
"github.com/NodeFactoryIo/vedran/internal/repositories"
"math"
"time"
)
func CalculateTotalPingsForNode(
repos repositories.Repos,
nodeId string,
intervalStart time.Time,
intervalEnd time.Time,
) (float64, error) {
downtimesInInterval, err := repos.DowntimeRepo.FindDowntimesInsideInterval(nodeId, intervalStart, intervalEnd)
if err != nil {
if err.Error() == "not found" {
downtimesInInterval = []models.Downtime{}
} else {
return 0, err
}
}
totalTime := intervalEnd.Sub(intervalStart)
leftTime := totalTime
for _, downtime := range downtimesInInterval {
var downtimeLength time.Duration
// case 1: entire downtime inside interval
if downtime.Start.After(intervalStart) && downtime.End.Before(intervalEnd) {
downtimeLength = downtime.End.Sub(downtime.Start)
}
// case 2: downtime started before interval
if downtime.Start.Before(intervalStart) {
downtimeLength = downtime.End.Sub(intervalStart)
}
leftTime -= downtimeLength
}
// case 3: downtime still active
_, duration, err := repos.PingRepo.CalculateDowntime(nodeId, intervalEnd)
if err != nil {
return 0, err
}
if duration.Seconds() > leftTime.Seconds() {
// if node was down for entire observed interval
return 0, nil
}
if math.Abs(duration.Seconds()) > PingIntervalInSeconds {
leftTime -= duration
}
totalPings := leftTime.Seconds() / PingIntervalInSeconds
return totalPings, nil
}