forked from influxdata/chronograf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protoboards.go
56 lines (51 loc) · 1.44 KB
/
protoboards.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
package multistore
import (
"context"
"github.com/hws522/chronograf"
)
// Protoboards is a ProtoboardsStore that contains multiple ProtoboardsStores
// The All method will return the set of all Protoboards.
// Each method will be tried against the Stores slice serially.
type Protoboards struct {
Stores []chronograf.ProtoboardsStore
}
// All returns the set of all protoboards
func (s *Protoboards) All(ctx context.Context) ([]chronograf.Protoboard, error) {
all := []chronograf.Protoboard{}
protoboardSet := map[string]chronograf.Protoboard{}
ok := false
var err error
for _, store := range s.Stores {
var protoboards []chronograf.Protoboard
protoboards, err = store.All(ctx)
if err != nil {
// Try to load as many protoboards as possible
continue
}
ok = true
for _, l := range protoboards {
// Enforce that the protoboard has a unique ID
// If the protoboard has been seen before then skip
if _, okay := protoboardSet[l.ID]; !okay {
protoboardSet[l.ID] = l
all = append(all, l)
}
}
}
if !ok {
return nil, err
}
return all, nil
}
// Get retrieves protoboard if `ID` exists. Searches through each store sequentially until success.
func (s *Protoboards) Get(ctx context.Context, ID string) (chronograf.Protoboard, error) {
var err error
for _, store := range s.Stores {
var l chronograf.Protoboard
l, err = store.Get(ctx, ID)
if err == nil {
return l, nil
}
}
return chronograf.Protoboard{}, err
}