/
schedule.go
191 lines (167 loc) · 4.29 KB
/
schedule.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
package gdq
import (
"fmt"
"strings"
"sync"
"time"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/secure/precis"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
// Schedule represents the runs occurring at a GDQ event
type Schedule struct {
Runs []*Run
byRunner map[string][]*Run
byHost map[string][]*Run
l sync.RWMutex
}
// NewSchedule returns an empty Schedule
func NewSchedule() *Schedule {
return &Schedule{
Runs: []*Run{},
byRunner: map[string][]*Run{},
byHost: map[string][]*Run{},
}
}
// NewScheduleFrom returns a scheduled filled with the runs
func NewScheduleFrom(runs []*Run) *Schedule {
if len(runs) == 0 {
return NewSchedule()
}
s := &Schedule{
Runs: make([]*Run, 0, len(runs)),
byRunner: map[string][]*Run{},
byHost: map[string][]*Run{},
}
s.load(runs)
return s
}
// load a series of runs in the Schedule
//
// Call this method when wanting to add runs to a schedule to ensure that
// the byRunner and byHost maps get updated. This permits the filter functions
// like ForHost and ForRunner to work
func (s *Schedule) load(runs []*Run) {
s.l.Lock()
defer s.l.Unlock()
for _, run := range runs {
s.Runs = append(s.Runs, run)
for _, runner := range run.Runners {
rev, ok := s.byRunner[runner.Handle]
if ok {
s.byRunner[runner.Handle] = append(rev, run)
} else {
s.byRunner[runner.Handle] = []*Run{run}
}
}
for _, host := range run.Hosts {
hev, ok := s.byHost[host]
if ok {
s.byHost[host] = append(hev, run)
} else {
s.byHost[host] = []*Run{run}
}
}
}
}
// ForRunner returns a new schedule with runs only matching this runner
//
// The runner's name is matched using a string submatch. This means that if you
// call somtething like schedule.ForRunner("b") you can get a schedule with runs
// for multiple runners.
//
// The match is case insensitive.
func (s *Schedule) ForRunner(name string) *Schedule {
return s.forEntity("runner", name)
}
// ForHost returns a new schedule with runs only matching this host
//
// The host's name is matched using a string submatch. This means that if you
// call somtething like schedule.ForHust("b") you can get a schedule with runs
// for multiple hosts.
//
// The match is case insensitive.
func (s *Schedule) ForHost(name string) *Schedule {
return s.forEntity("host", name)
}
// ForTitle returns a new schedule with runs only matching this runs title
//
// The title is matched using a string submatch. This means that if you call
// somtething like schedule.ForTitle("b") you can get a schedule with multiple
// runs.
//
// The match is case insensitive.
func (s *Schedule) ForTitle(title string) *Schedule {
if strings.TrimSpace(title) == "" {
return NewSchedule()
}
s.l.RLock()
defer s.l.RUnlock()
runs := []*Run{}
for _, run := range s.Runs {
if strings.Contains(normalised(run.Title), normalised(title)) {
runs = append(runs, run)
}
}
ns := NewScheduleFrom(runs)
return ns
}
func (s *Schedule) forEntity(kind string, match string) *Schedule {
ns := NewSchedule()
if strings.TrimSpace(match) == "" {
return ns
}
var runs map[string][]*Run
switch kind {
case "host":
runs = s.byHost
case "runner":
runs = s.byRunner
default:
panic(fmt.Sprintf("unsupported kind: %s in forEntity call", kind))
}
s.l.RLock()
defer s.l.RUnlock()
for h := range runs {
if strings.Contains(normalised(h), normalised(match)) {
ns.load(runs[h])
}
}
return ns
}
// NextRun returns the next/upcoming run in the schedule
func (s *Schedule) NextRun() *Run {
now := time.Now().UTC()
var runs *Run
s.l.RLock()
defer s.l.RUnlock()
for _, run := range s.Runs {
if run.Start.After(now) {
runs = run
break
}
}
return runs
}
// normalised transforms a string to a variant that has punctuation and
// diacritics removed, and is mapped to lower case
func normalised(s string) string {
s = runes.Remove(runes.In(unicode.Punct)).String(s)
filter := precis.NewIdentifier(
precis.LowerCase(),
precis.AdditionalMapping(func() transform.Transformer {
return transform.Chain(
norm.NFD,
runes.Remove(runes.In(unicode.Mn)))
}),
precis.Norm(norm.NFC),
)
normalised := []string{}
for _, p := range strings.Fields(s) {
res, _ := filter.String(p)
normalised = append(normalised, res)
}
return strings.Join(normalised, " ")
}