forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfake.go
101 lines (81 loc) · 2.31 KB
/
fake.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
package annotationstest
import (
"context"
"sync"
"github.com/grafana/grafana/pkg/services/annotations"
)
type fakeAnnotationsRepo struct {
mtx sync.Mutex
annotations map[int64]annotations.Item
}
func NewFakeAnnotationsRepo() *fakeAnnotationsRepo {
return &fakeAnnotationsRepo{
annotations: map[int64]annotations.Item{},
}
}
func (repo *fakeAnnotationsRepo) Delete(_ context.Context, params *annotations.DeleteParams) error {
repo.mtx.Lock()
defer repo.mtx.Unlock()
if params.ID != 0 {
delete(repo.annotations, params.ID)
} else {
for _, v := range repo.annotations {
if params.DashboardID == v.DashboardID && params.PanelID == v.PanelID {
delete(repo.annotations, v.ID)
}
}
}
return nil
}
func (repo *fakeAnnotationsRepo) Save(ctx context.Context, item *annotations.Item) error {
repo.mtx.Lock()
defer repo.mtx.Unlock()
if item.ID == 0 {
item.ID = int64(len(repo.annotations) + 1)
}
repo.annotations[item.ID] = *item
return nil
}
func (repo *fakeAnnotationsRepo) SaveMany(ctx context.Context, items []annotations.Item) error {
repo.mtx.Lock()
defer repo.mtx.Unlock()
for _, i := range items {
if i.ID == 0 {
i.ID = int64(len(repo.annotations) + 1)
}
repo.annotations[i.ID] = i
}
return nil
}
func (repo *fakeAnnotationsRepo) Update(_ context.Context, item *annotations.Item) error {
return nil
}
func (repo *fakeAnnotationsRepo) Find(_ context.Context, query *annotations.ItemQuery) ([]*annotations.ItemDTO, error) {
repo.mtx.Lock()
defer repo.mtx.Unlock()
if annotation, has := repo.annotations[query.AnnotationID]; has {
return []*annotations.ItemDTO{{ID: annotation.ID, DashboardID: annotation.DashboardID}}, nil
}
annotations := []*annotations.ItemDTO{{ID: 1, DashboardID: 0}}
return annotations, nil
}
func (repo *fakeAnnotationsRepo) FindTags(_ context.Context, query *annotations.TagsQuery) (annotations.FindTagsResult, error) {
result := annotations.FindTagsResult{
Tags: []*annotations.TagsDTO{},
}
return result, nil
}
func (repo *fakeAnnotationsRepo) Len() int {
repo.mtx.Lock()
defer repo.mtx.Unlock()
return len(repo.annotations)
}
func (repo *fakeAnnotationsRepo) Items() map[int64]annotations.Item {
repo.mtx.Lock()
defer repo.mtx.Unlock()
ret := make(map[int64]annotations.Item)
for k, v := range repo.annotations {
ret[k] = v
}
return ret
}