-
Notifications
You must be signed in to change notification settings - Fork 3
/
persistence.go
352 lines (318 loc) · 10.3 KB
/
persistence.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
/*
Copyright 2021 Adevinta
*/
package persistence
import (
"encoding/json"
"errors"
"strings"
"time"
"github.com/adevinta/vulcan-scan-engine/pkg/api"
"github.com/adevinta/vulcan-scan-engine/pkg/api/persistence/db"
uuid "github.com/satori/go.uuid"
)
const (
errNoInitChecksFinishedStr = `pq: null value in column "data" of relation "scans" violates not-null constraint`
)
var (
// ErrChecksFinishedNotInitialized is returned by the AddCheckAsFinished when trying to add
// a check finished to its scan.
ErrChecksFinishedNotInitialized = errors.New("field checks_finished is not initialized")
)
type ScansStore interface {
CreateScan(id uuid.UUID, scan api.Scan) (int64, error)
UpsertCheck(scanID, id uuid.UUID, check api.Check, updateStates []string) (int64, error)
GetScans(offset, limit uint32) ([]api.Scan, error)
GetScanChecks(scanID uuid.UUID) ([]api.Check, error)
GetScanChecksByStatus(scanID uuid.UUID, status string) ([]api.Check, error)
GetScanByID(id uuid.UUID) (api.Scan, error)
GetScanStats(scanID uuid.UUID) (map[string]int, error)
UpdateScan(id uuid.UUID, scan api.Scan, updateStates []string) (int64, error)
GetScansByExternalID(ID string, offset, limit uint32) ([]api.Scan, error)
GetCheckByID(id uuid.UUID) (api.Check, error)
DeleteScanChecks(scanID uuid.UUID) error
GetScanIDForCheck(ID uuid.UUID) (uuid.UUID, error)
AddCheckAsFinished(checkID uuid.UUID) (int64, error)
GetScanStatus(ID uuid.UUID) (api.Scan, error)
}
// Persistence implements ScansStore interface
// by using the underlying document store.
type Persistence struct {
store db.DB
}
// NewPersistence creates and initializes a store.
func NewPersistence(s db.DB) Persistence {
return Persistence{store: s}
}
// Close closes the underlaying connection to the store.
func (db Persistence) Close() error {
return db.store.Close()
}
// CreateScan creates a new scan in the database.
func (db Persistence) CreateScan(id uuid.UUID, scan api.Scan) (int64, error) {
return db.store.UpsertDocument(id, scan, "")
}
// UpdateScan updates a scan in the database. The data of the current scan in
// the db it is not replaced, but merged using json merge as is defined in the
// concatenate operation here:
// https://www.postgresql.org/docs/9.5/functions-json.html
func (db Persistence) UpdateScan(id uuid.UUID, scan api.Scan, updateStates []string) (int64, error) {
condition := ""
if len(updateStates) > 0 {
condition = `scans.data->>'status' IN (`
marks := []string{}
for i := 0; i < len(updateStates); i++ {
marks = append(marks, "?")
}
cond := strings.Join(marks, ",")
condition = condition + cond + ")"
}
if scan.Progress != nil {
pcondition := `( ? >= COALESCE(scans.data->>'progress', '0.0')::float )`
if condition != "" {
condition = pcondition + " AND (" + condition + ")"
} else {
condition = pcondition
}
}
args := []interface{}{}
if scan.Progress != nil {
args = append(args, scan.Progress)
}
for _, v := range updateStates {
args = append(args, interface{}(v))
}
count, err := db.store.UpsertDocument(id, scan, condition, args...)
return count, err
}
// AddCheckAsFinished mark the given check as finished in the given scan by
// increasing the the number the field ChecksFinished in the giver scan. The
// operation is idempotent That is if for a given scanID and CheckID the
// operation will only increase the field ChecksFinished of the scan one time,
// no matters the number of times it is called.
func (db Persistence) AddCheckAsFinished(checkID uuid.UUID) (int64, error) {
query := `
WITH if_check_was_not_added AS(
SELECT parent_id as scan_id FROM checks WHERE checks.id=? AND
data->'check_added' is NULL
),
add_check AS (
UPDATE scans SET data = data || ('{"checks_finished": ' || ((data->>'checks_finished')::int + 1) || '}')::jsonb
FROM if_check_was_not_added as scan_to_add
WHERE scans.id = scan_to_add.scan_id
RETURNING scans.id
)
UPDATE checks SET data = data || '{"check_added":true}', updated_at=?
FROM add_check as scan
WHERE checks.id=?
`
now := time.Now()
n, err := db.store.ExecRaw(query, checkID, now, checkID)
if err != nil && err.Error() == errNoInitChecksFinishedStr {
err = ErrChecksFinishedNotInitialized
}
return n, err
}
// UpsertCheck adds or updates a check of a given scan.
func (db Persistence) UpsertCheck(scanID, id uuid.UUID, check api.Check, updateStates []string) (int64, error) {
now := time.Now()
condition := ""
if len(updateStates) > 0 {
condition = `checks.data->>'status' IN (`
marks := []string{}
for i := 0; i < len(updateStates); i++ {
marks = append(marks, "?")
}
cond := strings.Join(marks, ",")
condition = condition + cond + ")"
}
// TODO: It would be better to not reference the table name 'checks' in the condition.
if check.Progress != nil {
pcondition := ` ( ? >= COALESCE(checks.data->>'progress', '0.0')::float )`
if condition != "" {
condition = pcondition + " AND (" + condition + ")"
} else {
condition = pcondition
}
}
args := []interface{}{}
if check.Progress != nil {
args = append(args, check.Progress)
}
for _, v := range updateStates {
args = append(args, interface{}(v))
}
check.UpdatedAt = &now
data, err := json.Marshal(check)
if err != nil {
return 0, err
}
check.Data = data
return db.store.UpsertChildDocumentWithData(scanID, id, check, check.Data, condition, args...)
}
// GetScans returns the list of scans.
func (db Persistence) GetScans(offset, limit uint32) ([]api.Scan, error) {
datas, err := db.store.GetAllDocsFromDocTypeWithLimit(api.Scan{}, offset, limit)
if err != nil {
return []api.Scan{}, err
}
res := []api.Scan{}
for _, v := range datas {
c := api.Scan{}
err := json.Unmarshal(v, &c)
if err != nil {
return []api.Scan{}, err
}
res = append(res, c)
}
return res, nil
}
// GetScanByID returns a scan given its ID.
func (db Persistence) GetScanByID(id uuid.UUID) (api.Scan, error) {
s := api.Scan{}
err := db.store.GetDocByIDFromDocType(&s, id)
return s, err
}
// GetScanChecks returns all checks of a scan.
func (db Persistence) GetScanChecks(scanID uuid.UUID) ([]api.Check, error) {
datas, err := db.store.GetChildDocsFromDocType(api.Check{}, scanID)
if err != nil {
return []api.Check{}, err
}
res := []api.Check{}
for _, v := range datas {
c := api.Check{}
err := json.Unmarshal(v, &c)
if err != nil {
return []api.Check{}, err
}
c.Data = v
res = append(res, c)
}
return res, nil
}
// GetScanChecksByStatus returns all checks of a scan that have the given status.
func (db Persistence) GetScanChecksByStatus(scanID uuid.UUID, status string) ([]api.Check, error) {
cond := "checks.data->>'status' = ?"
datas, err := db.store.GetChildDocsFromDocTypeWithCondition(api.Check{}, scanID, cond, status)
if err != nil {
return []api.Check{}, err
}
res := []api.Check{}
for _, v := range datas {
c := api.Check{}
err := json.Unmarshal(v, &c)
if err != nil {
return []api.Check{}, err
}
c.Data = v
res = append(res, c)
}
return res, nil
}
// DeleteScanChecks deletes all the checks of a given scan.
func (db Persistence) DeleteScanChecks(scanID uuid.UUID) error {
return db.store.DeleteChildDocuments(scanID, api.Check{})
}
// GetCheckByID returns the check for the given ID.
func (db Persistence) GetCheckByID(id uuid.UUID) (api.Check, error) {
c := api.Check{}
err := db.store.GetDocByIDFromDocType(&c, id)
return c, err
}
// GetScanStatus returns a scan struct with only the fields: Status, CheckCount, ChecksFinished
// filled.
func (db Persistence) GetScanStatus(ID uuid.UUID) (api.Scan, error) {
query := `SELECT data->>'status' as status,
data->'checks_finished' as checks_finished,
data->'check_count', data->>'tag', data->>'external_id' as check_count FROM scans WHERE id=?`
s := new(api.Scan)
var (
count, finished int
status string
tag string
externalID string
)
rest := []interface{}{&status, &finished, &count, &tag, &externalID}
err := db.store.QueryRaw(query, rest, ID)
if err != nil {
return api.Scan{}, err
}
s.ID = ID
s.Status = &status
s.CheckCount = &count
s.ChecksFinished = &finished
s.Tag = &tag
s.ExternalID = &externalID
return *s, nil
}
// GetScanStats returns the number of checks by status for the given scan.
func (db Persistence) GetScanStats(scanID uuid.UUID) (map[string]int, error) {
return db.store.GetChildDocsStatsFromDocType(api.Check{}, "status", scanID)
}
// GetScansByExternalID returns scans with a given ExternalID applying the given offset and limit.
func (db Persistence) GetScansByExternalID(ID string, offset, limit uint32) ([]api.Scan, error) {
var (
err error
datas [][]byte
scan api.Scan
)
if offset == 0 && limit == 0 {
datas, err = db.store.GetDocsByFieldFromDocType(&scan, `"`+ID+`"`, `external_id`)
} else {
datas, err = db.store.GetDocsByFieldLimitFromDocType(&scan, `"`+ID+`"`, offset, limit, `external_id`)
}
if err != nil {
return []api.Scan{}, err
}
res := []api.Scan{}
for _, v := range datas {
c := api.Scan{}
err := json.Unmarshal(v, &c)
if err != nil {
return []api.Scan{}, err
}
res = append(res, c)
}
return res, nil
}
// InsertCheckIfNotExists looks if a check exists in the database with the same
// ScanID and ScanIndex than the passed check. If it exists, the function
// returns the id of the check in the database. If it does not exist the
// function inserts the passed check and returns the id of the inserted check
// id.
func (db Persistence) InsertCheckIfNotExists(c api.Check) (string, error) {
if c.ScanIndex == nil {
return "", errors.New("ScanIndex can not be nil")
}
index := *c.ScanIndex
id, err := uuid.FromString(c.ID)
if err != nil {
return "", err
}
sID, err := uuid.FromString(c.ScanID)
if err != nil {
return "", err
}
data, err := json.Marshal(c)
if err != nil {
return "", err
}
c.Data = data
return db.store.InsertChildDocIfNotExistsFromDocType(c, sID, id, index, c.Data)
}
func (db Persistence) GetScanIDForCheck(ID uuid.UUID) (uuid.UUID, error) {
c := api.Check{}
return db.store.GetParentID(c, ID)
}
func (db Persistence) GetCreatingScans() ([]string, error) {
s := api.Scan{}
condition := `(data->'check_count' <> data->'checks_created' AND data->>'status' = 'RUNNING')`
return db.store.GetDocIDsWithCondFromDocType(s, condition)
}
func (db Persistence) TryLockScan(id string) (*db.Lock, error) {
return db.store.TryGetLock(id)
}
func (db Persistence) ReleaseScanLock(l *db.Lock) error {
return db.store.ReleaseLock(l)
}