-
Notifications
You must be signed in to change notification settings - Fork 7
/
store.go
460 lines (415 loc) · 11.7 KB
/
store.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package webui
import (
"errors"
"fmt"
"path/filepath"
"strings"
"sync"
"time"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/analysis/analyzer/keyword"
"github.com/blevesearch/bleve/search"
"github.com/blevesearch/bleve/search/query"
"github.com/sirupsen/logrus"
)
const (
// eventsIndexMappingVersion is the version of the events index mapping
// if you change something in the mapping, increment this version
// this is used to ensure the index will be recreated if we change the mapping
eventsIndexMappingVersion = 1
)
type Store struct {
config StoreConfig
gcStopChan chan struct{}
events bleve.Index
jobs bleve.Index
mergeStatus []MergePool
mergeStatusMutex sync.RWMutex
mergeHistory []MergeRecord
mergeHistoryMutex sync.RWMutex
}
type StoreConfig struct {
DataPath string
MaxEvents int
EventsMaxAge time.Duration
}
func NewStore(cfg StoreConfig, logger *logrus.Logger) (*Store, error) {
var (
store = new(Store)
err error
)
keywordFieldMapping := bleve.NewTextFieldMapping()
keywordFieldMapping.Analyzer = keyword.Name
jobsMapping := bleve.NewIndexMapping()
jobsMapping.DefaultAnalyzer = keyword.Name
jobsMapping.DefaultMapping.AddFieldMappingsAt("Start", bleve.NewDateTimeFieldMapping())
jobsMapping.DefaultMapping.AddFieldMappingsAt("End", bleve.NewDateTimeFieldMapping())
eventsMapping := bleve.NewIndexMapping()
eventsMapping.DefaultAnalyzer = keyword.Name
eventsMapping.DefaultMapping.AddFieldMappingsAt("Time", bleve.NewDateTimeFieldMapping())
store.jobs, err = bleve.NewMemOnly(jobsMapping)
if err != nil {
return nil, fmt.Errorf("failed to created a Bleve in-memory Index: %w", err)
}
if cfg.DataPath == "" {
store.events, err = bleve.NewMemOnly(eventsMapping)
if err != nil {
return nil, fmt.Errorf("failed to created a Bleve in-memory Index: %w", err)
}
} else {
eventsDataPath := filepath.Join(cfg.DataPath, fmt.Sprintf("events-v%d", eventsIndexMappingVersion))
store.events, err = bleve.Open(eventsDataPath)
if errors.Is(err, bleve.ErrorIndexPathDoesNotExist) {
store.events, err = bleve.New(eventsDataPath, eventsMapping)
} else if err != nil {
if logger != nil {
logger.WithError(err).WithField("index-path", eventsDataPath).Warning("failed to open existing Bleve index - a new (empty) index will be created...")
}
store.events, err = bleve.New(eventsDataPath, eventsMapping)
}
if err != nil {
return nil, fmt.Errorf("failed to created a Bleve Index at %s: %w", eventsDataPath, err)
}
}
store.config = cfg
store.gcStopChan = make(chan struct{})
ticker := time.NewTicker(1 * time.Minute)
go func() {
for {
select {
case <-ticker.C:
store.CollectGarbage()
case <-store.gcStopChan:
logger.Info("Store GarbageCollector exiting...")
return
}
}
}()
return store, nil
}
func (s *Store) Close() error {
close(s.gcStopChan)
return s.events.Close()
}
func (s *Store) SetMergeStatus(pools []MergePool) {
s.mergeStatusMutex.Lock()
defer s.mergeStatusMutex.Unlock()
s.mergeStatus = make([]MergePool, len(pools))
copy(s.mergeStatus, pools)
}
func (s *Store) QueryMergeStatus(q MergeStatusQuery) []MergePool {
s.mergeStatusMutex.RLock()
defer s.mergeStatusMutex.RUnlock()
var pools []MergePool
for _, pool := range s.mergeStatus {
if q.Owner != "" && q.Owner != pool.Owner {
continue
}
if q.Repository != "" && q.Repository != pool.Repository {
continue
}
if q.Branch != "" && q.Branch != pool.Branch {
continue
}
pools = append(pools, pool)
}
return pools
}
func (s *Store) SetMergeHistory(records []MergeRecord) {
s.mergeHistoryMutex.Lock()
defer s.mergeHistoryMutex.Unlock()
s.mergeHistory = make([]MergeRecord, len(records))
copy(s.mergeHistory, records)
}
func (s *Store) QueryMergeHistory(q MergeHistoryQuery) []MergeRecord {
s.mergeHistoryMutex.RLock()
defer s.mergeHistoryMutex.RUnlock()
var records []MergeRecord
for _, record := range s.mergeHistory {
if q.Owner != "" && q.Owner != record.Owner {
continue
}
if q.Repository != "" && q.Repository != record.Repository {
continue
}
if q.Branch != "" && q.Branch != record.Branch {
continue
}
records = append(records, record)
}
return records
}
func (s *Store) AddJob(j Job) error {
return s.jobs.Index(j.Name, j)
}
func (s *Store) DeleteJob(name string) error {
return s.jobs.Delete(name)
}
func (s *Store) AddEvent(e Event) error {
return s.events.Index(e.GUID, e)
}
func (s *Store) QueryJobs(q JobsQuery) (*Jobs, error) {
request := bleve.NewSearchRequest(q.ToBleveQuery())
request.SortBy([]string{"-Start"})
request.Size = 10000
request.Fields = []string{"*"}
request.AddFacet("State", bleve.NewFacetRequest("State", 4))
request.AddFacet("Repository", bleve.NewFacetRequest("Repository", 3))
request.AddFacet("Type", bleve.NewFacetRequest("Type", 3))
request.AddFacet("Author", bleve.NewFacetRequest("Author", 3))
result, err := s.jobs.Search(request)
if err != nil {
return nil, fmt.Errorf("failed to search for %v: %w", q, err)
}
jobs := bleveResultToJobs(result)
return &jobs, nil
}
func (s *Store) QueryEvents(q EventsQuery) (*Events, error) {
request := bleve.NewSearchRequest(q.ToBleveQuery())
request.SortBy([]string{"-Time"})
request.Size = 10000
request.Fields = []string{"*"}
request.AddFacet("Kind", bleve.NewFacetRequest("Kind", 4))
request.AddFacet("Repository", bleve.NewFacetRequest("Repository", 3))
request.AddFacet("Sender", bleve.NewFacetRequest("Sender", 3))
result, err := s.events.Search(request)
if err != nil {
return nil, fmt.Errorf("failed to search for %v: %w", q, err)
}
events := bleveResultToEvents(result)
return &events, nil
}
func (s *Store) CollectGarbage() error {
var deleteMatchingEvents = func(req *bleve.SearchRequest) error {
result, err := s.events.Search(req)
if err != nil {
return err
}
for _, doc := range result.Hits {
if err = s.events.Delete(doc.ID); err != nil {
return err
}
}
return nil
}
if s.config.MaxEvents > 0 {
request := bleve.NewSearchRequest(bleve.NewMatchAllQuery())
request.SortBy([]string{"-Time"})
request.Size = 1000
request.From = s.config.MaxEvents
if err := deleteMatchingEvents(request); err != nil {
return err
}
}
if s.config.EventsMaxAge > 0 {
request := bleve.NewSearchRequest(bleve.NewDateRangeQuery(time.Time{}, time.Now().Add(-s.config.EventsMaxAge)))
request.Size = 1000
if err := deleteMatchingEvents(request); err != nil {
return err
}
}
return nil
}
type JobsQuery struct {
EventGUID string
Owner string
Repository string
Branch string
Query string
}
func (q JobsQuery) ToBleveQuery() query.Query {
var queryString strings.Builder
if len(q.Query) > 0 {
queryString.WriteString("+")
queryString.WriteString(q.Query)
}
if len(q.EventGUID) > 0 {
if queryString.Len() > 0 {
queryString.WriteString(" ")
}
queryString.WriteString("+EventGUID:")
queryString.WriteString(q.EventGUID)
}
if len(q.Owner) > 0 {
if queryString.Len() > 0 {
queryString.WriteString(" ")
}
queryString.WriteString("+Owner:")
queryString.WriteString(q.Owner)
}
if len(q.Repository) > 0 {
if queryString.Len() > 0 {
queryString.WriteString(" ")
}
queryString.WriteString("+Repository:")
queryString.WriteString(q.Repository)
}
if len(q.Branch) > 0 {
if queryString.Len() > 0 {
queryString.WriteString(" ")
}
queryString.WriteString("+Branch:")
queryString.WriteString(q.Branch)
}
if queryString.Len() == 0 {
return bleve.NewMatchAllQuery()
}
return bleve.NewQueryStringQuery(queryString.String())
}
func bleveResultToJobs(result *bleve.SearchResult) Jobs {
var jobs Jobs
for _, doc := range result.Hits {
job := bleveDocToJob(doc)
jobs.Jobs = append(jobs.Jobs, job)
}
for _, facet := range result.Facets {
counts := map[string]int{}
for _, term := range facet.Terms {
counts[term.Term] = term.Count
}
for _, numericRange := range facet.NumericRanges {
counts[numericRange.Name] = numericRange.Count
}
counts["Other"] = facet.Other
switch facet.Field {
case "State":
jobs.Counts.States = counts
case "Repository":
jobs.Counts.Repositories = counts
case "Type":
jobs.Counts.Types = counts
case "Author":
jobs.Counts.Authors = counts
}
}
return jobs
}
func bleveDocToJob(doc *search.DocumentMatch) Job {
var (
startDate, endDate time.Time
)
if start, ok := doc.Fields["Start"].(string); ok {
startDate, _ = time.Parse(time.RFC3339, start)
}
if end, ok := doc.Fields["End"].(string); ok {
endDate, _ = time.Parse(time.RFC3339, end)
}
return Job{
Name: doc.Fields["Name"].(string),
Type: doc.Fields["Type"].(string),
EventGUID: doc.Fields["EventGUID"].(string),
Owner: doc.Fields["Owner"].(string),
Repository: doc.Fields["Repository"].(string),
Branch: doc.Fields["Branch"].(string),
Build: doc.Fields["Build"].(string),
Context: doc.Fields["Context"].(string),
Author: doc.Fields["Author"].(string),
State: doc.Fields["State"].(string),
Description: doc.Fields["Description"].(string),
ReportURL: doc.Fields["ReportURL"].(string),
TraceID: doc.Fields["TraceID"].(string),
Start: startDate,
End: endDate,
Duration: time.Duration(doc.Fields["Duration"].(float64)),
}
}
type EventsQuery struct {
GUID string
Owner string
Repository string
Branch string
Query string
}
func (q EventsQuery) ToBleveQuery() query.Query {
var queryString strings.Builder
if len(q.Query) > 0 {
queryString.WriteString("+")
queryString.WriteString(q.Query)
}
if len(q.GUID) > 0 {
if queryString.Len() > 0 {
queryString.WriteString(" ")
}
queryString.WriteString("+GUID:")
queryString.WriteString(q.GUID)
}
if len(q.Owner) > 0 {
if queryString.Len() > 0 {
queryString.WriteString(" ")
}
queryString.WriteString("+Owner:")
queryString.WriteString(q.Owner)
}
if len(q.Repository) > 0 {
if queryString.Len() > 0 {
queryString.WriteString(" ")
}
queryString.WriteString("+Repository:")
queryString.WriteString(q.Repository)
}
if len(q.Branch) > 0 {
if queryString.Len() > 0 {
queryString.WriteString(" ")
}
queryString.WriteString("+Branch:")
queryString.WriteString(q.Branch)
}
if queryString.Len() == 0 {
return bleve.NewMatchAllQuery()
}
return bleve.NewQueryStringQuery(queryString.String())
}
func bleveResultToEvents(result *bleve.SearchResult) Events {
var events Events
for _, doc := range result.Hits {
event := bleveDocToEvent(doc)
events.Events = append(events.Events, event)
}
for _, facet := range result.Facets {
counts := map[string]int{}
for _, term := range facet.Terms {
counts[term.Term] = term.Count
}
for _, numericRange := range facet.NumericRanges {
counts[numericRange.Name] = numericRange.Count
}
counts["Other"] = facet.Other
switch facet.Field {
case "Kind":
events.Counts.Kinds = counts
case "Repository":
events.Counts.Repositories = counts
case "Sender":
events.Counts.Senders = counts
}
}
return events
}
func bleveDocToEvent(doc *search.DocumentMatch) Event {
var eventTime time.Time
if evTime, ok := doc.Fields["Time"].(string); ok {
eventTime, _ = time.Parse(time.RFC3339, evTime)
}
return Event{
GUID: doc.Fields["GUID"].(string),
Owner: doc.Fields["Owner"].(string),
Repository: doc.Fields["Repository"].(string),
Branch: doc.Fields["Branch"].(string),
Kind: doc.Fields["Kind"].(string),
Action: doc.Fields["Action"].(string),
Details: doc.Fields["Details"].(string),
URL: doc.Fields["URL"].(string),
Sender: doc.Fields["Sender"].(string),
Time: eventTime,
}
}
type MergeStatusQuery struct {
Owner string
Repository string
Branch string
}
type MergeHistoryQuery struct {
Owner string
Repository string
Branch string
}