forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
149 lines (118 loc) · 3.71 KB
/
service.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
package notebooks
import (
"context"
"database/sql"
"errors"
"time"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/snowflake"
"github.com/influxdata/influxdb/v2/sqlite"
)
var _ influxdb.NotebookService = (*Service)(nil)
type Service struct {
store *sqlite.SqlStore
idGenerator platform.IDGenerator
}
func NewService(store *sqlite.SqlStore) *Service {
return &Service{
store: store,
idGenerator: snowflake.NewIDGenerator(),
}
}
func (s *Service) GetNotebook(ctx context.Context, id platform.ID) (*influxdb.Notebook, error) {
var n influxdb.Notebook
query := `
SELECT id, org_id, name, spec, created_at, updated_at
FROM notebooks WHERE id = $1`
if err := s.store.DB.GetContext(ctx, &n, query, id); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, influxdb.ErrNotebookNotFound
}
return nil, err
}
return &n, nil
}
// CreateNotebook creates a notebook. Note that this and all "write" operations on the database need to use the Mutex lock,
// since sqlite can only handle 1 concurrent write operation at a time.
func (s *Service) CreateNotebook(ctx context.Context, create *influxdb.NotebookReqBody) (*influxdb.Notebook, error) {
s.store.Mu.Lock()
defer s.store.Mu.Unlock()
nowTime := time.Now().UTC()
n := influxdb.Notebook{
ID: s.idGenerator.ID(),
OrgID: create.OrgID,
Name: create.Name,
Spec: create.Spec,
CreatedAt: nowTime,
UpdatedAt: nowTime,
}
query := `
INSERT INTO notebooks (id, org_id, name, spec, created_at, updated_at)
VALUES (:id, :org_id, :name, :spec, :created_at, :updated_at)`
_, err := s.store.DB.NamedExecContext(ctx, query, &n)
if err != nil {
return nil, err
}
// Ideally, the create query would use "RETURNING" in order to avoid making a separate query.
// Unfortunately this breaks the scanning of values into the result struct, so we have to make a separate
// SELECT request to return the result from the database.
return s.GetNotebook(ctx, n.ID)
}
// UpdateNotebook updates a notebook.
func (s *Service) UpdateNotebook(ctx context.Context, id platform.ID, update *influxdb.NotebookReqBody) (*influxdb.Notebook, error) {
s.store.Mu.Lock()
defer s.store.Mu.Unlock()
nowTime := time.Now().UTC()
n := influxdb.Notebook{
ID: id,
OrgID: update.OrgID,
Name: update.Name,
Spec: update.Spec,
UpdatedAt: nowTime,
}
query := `
UPDATE notebooks SET org_id = :org_id, name = :name, spec = :spec, updated_at = :updated_at
WHERE id = :id`
_, err := s.store.DB.NamedExecContext(ctx, query, &n)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, influxdb.ErrNotebookNotFound
}
return nil, err
}
return s.GetNotebook(ctx, n.ID)
}
// DeleteNotebook deletes a notebook.
func (s *Service) DeleteNotebook(ctx context.Context, id platform.ID) error {
s.store.Mu.Lock()
defer s.store.Mu.Unlock()
query := `
DELETE FROM notebooks
WHERE id = $1`
res, err := s.store.DB.ExecContext(ctx, query, id.String())
if err != nil {
return err
}
r, err := res.RowsAffected()
if err != nil {
return err
}
if r == 0 {
return influxdb.ErrNotebookNotFound
}
return nil
}
// ListNotebooks lists notebooks matching the provided filter. Currently, only org_id is used in the filter.
// Future uses may support pagination via this filter as well.
func (s *Service) ListNotebooks(ctx context.Context, filter influxdb.NotebookListFilter) ([]*influxdb.Notebook, error) {
ns := []*influxdb.Notebook{}
query := `
SELECT id, org_id, name, spec, created_at, updated_at
FROM notebooks
WHERE org_id = $1`
if err := s.store.DB.SelectContext(ctx, &ns, query, filter.OrgID); err != nil {
return nil, err
}
return ns, nil
}