forked from influxdata/chronograf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sources.go
96 lines (86 loc) · 2.42 KB
/
sources.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
package multistore
import (
"context"
"github.com/hws522/chronograf"
)
// Ensure SourcesStore implements chronograf.SourcesStore.
var _ chronograf.SourcesStore = &SourcesStore{}
// SourcesStore delegates to the SourcesStores that compose it
type SourcesStore struct {
Stores []chronograf.SourcesStore
}
// All concatenates the Sources of all contained Stores
func (multi *SourcesStore) All(ctx context.Context) ([]chronograf.Source, error) {
all := []chronograf.Source{}
sourceSet := map[int]struct{}{}
ok := false
var err error
for _, store := range multi.Stores {
var sources []chronograf.Source
sources, err = store.All(ctx)
if err != nil {
// If this Store is unable to return an array of sources, skip to the
// next Store.
continue
}
ok = true // We've received a response from at least one Store
for _, s := range sources {
// Enforce that the source has a unique ID
// If the source has been seen before, don't override what we already have
if _, okay := sourceSet[s.ID]; !okay { // We have a new Source!
sourceSet[s.ID] = struct{}{} // We just care that the ID is unique
all = append(all, s)
}
}
}
if !ok {
return nil, err
}
return all, nil
}
// Add the src to the first Store to respond successfully
func (multi *SourcesStore) Add(ctx context.Context, src chronograf.Source) (chronograf.Source, error) {
var err error
for _, store := range multi.Stores {
var s chronograf.Source
s, err = store.Add(ctx, src)
if err == nil {
return s, nil
}
}
return chronograf.Source{}, nil
}
// Delete delegates to all stores, returns success if one Store is successful
func (multi *SourcesStore) Delete(ctx context.Context, src chronograf.Source) error {
var err error
for _, store := range multi.Stores {
err = store.Delete(ctx, src)
if err == nil {
return nil
}
}
return err
}
// Get finds the Source by id among all contained Stores
func (multi *SourcesStore) Get(ctx context.Context, id int) (chronograf.Source, error) {
var err error
for _, store := range multi.Stores {
var s chronograf.Source
s, err = store.Get(ctx, id)
if err == nil {
return s, nil
}
}
return chronograf.Source{}, err
}
// Update the first store to return a successful response
func (multi *SourcesStore) Update(ctx context.Context, src chronograf.Source) error {
var err error
for _, store := range multi.Stores {
err = store.Update(ctx, src)
if err == nil {
return nil
}
}
return err
}