This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.go
149 lines (138 loc) · 3.19 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
package gotel
import (
"errors"
"fmt"
"math"
"net"
"sort"
"strconv"
"strings"
"time"
)
const (
// Minute is the nubmer of seconds in a minute
Minute = 60
// Hour is the nubmer of seconds in an hour
Hour = 60 * Minute
// Day is the nubmer of seconds in a day
Day = 24 * Hour
// Week is the nubmer of seconds in a week
Week = 7 * Day
// Month is the nubmer of seconds in a 30 day month
Month = 30 * Day
// Year is the nubmer of seconds in almost a year
Year = 12 * Month
// LongTime is the nubmer of seconds in a while
LongTime = 37 * Year
)
var magnitudes = []struct {
d int64
format string
divby int64
}{
{1, "now", 1},
{2, "1 second %s", 1},
{Minute, "%d seconds %s", 1},
{2 * Minute, "1 minute %s", 1},
{Hour, "%d minutes %s", Minute},
{2 * Hour, "1 hour %s", 1},
{Day, "%d hours %s", Hour},
{2 * Day, "1 day %s", 1},
{Week, "%d days %s", Day},
{2 * Week, "1 week %s", 1},
{Month, "%d weeks %s", Week},
{2 * Month, "1 month %s", 1},
{Year, "%d months %s", Month},
{18 * Month, "1 year %s", 1},
{2 * Year, "2 years %s", 1},
{LongTime, "%d years %s", Year},
{math.MaxInt64, "a long while %s", 1},
}
// returns the external IP of the machine you're on
func externalIP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network?")
}
// RelTime returns the duration between to times, formatted as a string
func RelTime(a, b time.Time, albl, blbl string) string {
lbl := albl
diff := b.Unix() - a.Unix()
after := a.After(b)
if after {
lbl = blbl
diff = a.Unix() - b.Unix()
}
n := sort.Search(len(magnitudes), func(i int) bool {
return magnitudes[i].d > diff
})
mag := magnitudes[n]
args := []interface{}{}
escaped := false
for _, ch := range mag.format {
if escaped {
switch ch {
case '%':
case 's':
args = append(args, lbl)
case 'd':
args = append(args, diff/mag.divby)
}
escaped = false
} else {
escaped = ch == '%'
}
}
return fmt.Sprintf(mag.format, args...)
}
func (res reservation) formatAlert(format string) string {
lastCheckin := time.Unix(res.LastCheckin, 0)
ip, err := externalIP()
if err != nil {
ip = "N/A"
}
replacer := strings.NewReplacer(
"{jobid}", strconv.Itoa(res.JobID),
"{app}", res.App,
"{component}", res.Component,
"{owner}", res.Owner,
"{notify}", res.Notify,
"{frequency}", strconv.Itoa(res.Frequency) + res.TimeUnits,
"{last}", time.Unix(res.LastCheckin, 0).Format(time.RFC1123),
"{since}", RelTime(lastCheckin, time.Now(), "ago", ""),
"{checkins}", strconv.Itoa(res.NumCheckins),
"{srv}", ip,
)
return replacer.Replace(format)
}