-
Notifications
You must be signed in to change notification settings - Fork 69
/
archives.go
390 lines (347 loc) · 9.46 KB
/
archives.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package db
import (
"fmt"
"strings"
"time"
"github.com/pborman/uuid"
)
type Archive struct {
UUID uuid.UUID `json:"uuid"`
StoreKey string `json:"key"`
TakenAt int64 `json:"taken_at"`
ExpiresAt int64 `json:"expires_at"`
Notes string `json:"notes"`
Status string `json:"status"`
PurgeReason string `json:"purge_reason"`
TargetUUID uuid.UUID `json:"target_uuid"`
TargetName string `json:"target_name"`
TargetPlugin string `json:"target_plugin"`
TargetEndpoint string `json:"target_endpoint"`
StoreUUID uuid.UUID `json:"store_uuid"`
StoreName string `json:"store_name"`
StorePlugin string `json:"store_plugin"`
StoreEndpoint string `json:"store_endpoint"`
Job string `json:"job"`
EncryptionType string `json:"encryption_type"`
TenantUUID uuid.UUID `json:"tenant_uuid"`
Size int64 `json:"size"`
}
type ArchiveFilter struct {
ForTarget string
ForStore string
Before *time.Time
After *time.Time
ExpiresBefore *time.Time
ExpiresAfter *time.Time
WithStatus []string
WithOutStatus []string
ForTenant string
Limit int
}
func (f *ArchiveFilter) Query() (string, []interface{}) {
wheres := []string{"a.uuid = a.uuid"}
var args []interface{}
if f.ForTarget != "" {
wheres = append(wheres, "target_uuid = ?")
args = append(args, f.ForTarget)
}
if f.ForStore != "" {
wheres = append(wheres, "store_uuid = ?")
args = append(args, f.ForStore)
}
if f.Before != nil {
wheres = append(wheres, "taken_at <= ?")
args = append(args, f.Before.Unix())
}
if f.After != nil {
wheres = append(wheres, "taken_at >= ?")
args = append(args, f.After.Unix())
}
if len(f.WithStatus) > 0 {
var params []string
for _, e := range f.WithStatus {
params = append(params, "?")
args = append(args, e)
}
wheres = append(wheres, fmt.Sprintf("status IN (%s)", strings.Join(params, ", ")))
}
if len(f.WithOutStatus) > 0 {
var params []string
for _, e := range f.WithOutStatus {
params = append(params, "?")
args = append(args, e)
}
wheres = append(wheres, fmt.Sprintf("status NOT IN (%s)", strings.Join(params, ", ")))
}
if f.ExpiresBefore != nil {
wheres = append(wheres, "expires_at < ?")
args = append(args, f.ExpiresBefore.Unix())
}
if f.ForTenant != "" {
wheres = append(wheres, "a.tenant_uuid = ?")
args = append(args, f.ForTenant)
}
limit := ""
if f.Limit > 0 {
limit = " LIMIT ?"
args = append(args, f.Limit)
}
return `
SELECT a.uuid, a.store_key,
a.taken_at, a.expires_at, a.notes,
t.uuid, t.name, t.plugin, t.endpoint,
s.uuid, s.name, s.plugin, s.endpoint,
a.status, a.purge_reason, a.job, a.encryption_type,
a.tenant_uuid, a.size
FROM archives a
INNER JOIN targets t ON t.uuid = a.target_uuid
INNER JOIN stores s ON s.uuid = a.store_uuid
WHERE ` + strings.Join(wheres, " AND ") + `
ORDER BY a.taken_at DESC, a.uuid ASC
` + limit, args
}
func (db *DB) CountArchives(filter *ArchiveFilter) (int, error) {
if filter == nil {
filter = &ArchiveFilter{}
}
query, args := filter.Query()
r, err := db.Query(query, args...)
if err != nil {
return 0, err
}
defer r.Close()
i := 0
for r.Next() {
i++
}
return i, nil
}
func (db *DB) GetAllArchives(filter *ArchiveFilter) ([]*Archive, error) {
if filter == nil {
filter = &ArchiveFilter{}
}
l := []*Archive{}
query, args := filter.Query()
r, err := db.Query(query, args...)
if err != nil {
return l, err
}
defer r.Close()
for r.Next() {
ann := &Archive{}
var takenAt, expiresAt, size *int64
var targetName, storeName *string
var this, target, store, tenant NullUUID
if err = r.Scan(
&this, &ann.StoreKey, &takenAt, &expiresAt, &ann.Notes,
&target, &targetName, &ann.TargetPlugin, &ann.TargetEndpoint,
&store, &storeName, &ann.StorePlugin, &ann.StoreEndpoint,
&ann.Status, &ann.PurgeReason, &ann.Job, &ann.EncryptionType,
&tenant, &size); err != nil {
return l, err
}
ann.UUID = this.UUID
ann.TargetUUID = target.UUID
ann.StoreUUID = store.UUID
ann.TenantUUID = tenant.UUID
if takenAt != nil {
ann.TakenAt = *takenAt
}
if expiresAt != nil {
ann.ExpiresAt = *expiresAt
}
if targetName != nil {
ann.TargetName = *targetName
}
if storeName != nil {
ann.StoreName = *storeName
}
if size != nil {
ann.Size = *size
}
l = append(l, ann)
}
return l, nil
}
func (db *DB) GetArchive(id uuid.UUID) (*Archive, error) {
r, err := db.Query(`
SELECT a.uuid, a.store_key,
a.taken_at, a.expires_at, a.notes,
t.uuid, t.name, t.plugin, t.endpoint,
s.uuid, s.name, s.plugin, s.endpoint, a.status,
a.purge_reason, a.job, a.encryption_type,
a.tenant_uuid, a.size
FROM archives a
INNER JOIN targets t ON t.uuid = a.target_uuid
INNER JOIN stores s ON s.uuid = a.store_uuid
WHERE a.uuid = ?`, id.String())
if err != nil {
return nil, err
}
defer r.Close()
if !r.Next() {
return nil, nil
}
ann := &Archive{}
var takenAt, expiresAt, size *int64
var targetName, storeName *string
var this, target, store, tenant NullUUID
if err = r.Scan(
&this, &ann.StoreKey, &takenAt, &expiresAt, &ann.Notes,
&target, &targetName, &ann.TargetPlugin, &ann.TargetEndpoint,
&store, &storeName, &ann.StorePlugin, &ann.StoreEndpoint,
&ann.Status, &ann.PurgeReason, &ann.Job, &ann.EncryptionType,
&tenant, &size); err != nil {
return nil, err
}
ann.UUID = this.UUID
ann.TargetUUID = target.UUID
ann.StoreUUID = store.UUID
ann.TenantUUID = tenant.UUID
if takenAt != nil {
ann.TakenAt = *takenAt
}
if expiresAt != nil {
ann.ExpiresAt = *expiresAt
}
if targetName != nil {
ann.TargetName = *targetName
}
if storeName != nil {
ann.StoreName = *storeName
}
if size != nil {
ann.Size = *size
}
return ann, nil
}
func (db *DB) UpdateArchive(update *Archive) error {
return db.Exec(
`UPDATE archives SET notes = ? WHERE uuid = ?`,
update.Notes, update.UUID.String(),
)
}
func (db *DB) AnnotateTargetArchive(target uuid.UUID, id string, notes string) error {
return db.Exec(
`UPDATE archives SET notes = ? WHERE uuid = ? AND target_uuid = ?`,
notes, id, target.String(),
)
}
func (db *DB) GetArchivesNeedingPurge() ([]*Archive, error) {
filter := &ArchiveFilter{
WithOutStatus: []string{"purged", "valid"},
}
return db.GetAllArchives(filter)
}
func (db *DB) GetExpiredArchives() ([]*Archive, error) {
now := time.Now()
filter := &ArchiveFilter{
ExpiresBefore: &now,
WithStatus: []string{"valid"},
}
return db.GetAllArchives(filter)
}
func (db *DB) InvalidateArchive(id uuid.UUID) error {
return db.Exec(`UPDATE archives SET status = 'invalid' WHERE uuid = ?`, id.String())
}
func (db *DB) PurgeArchive(id uuid.UUID) error {
a, err := db.GetArchive(id)
if err != nil {
return err
}
if a.Status == "valid" {
return fmt.Errorf("Invalid attempt to purge a 'valid' archive detected")
}
err = db.Exec(`UPDATE archives SET purge_reason = status WHERE uuid = ?`, id.String())
if err != nil {
return err
}
return db.Exec(`UPDATE archives SET status = 'purged' WHERE uuid = ?`, id.String())
}
func (db *DB) ExpireArchive(id uuid.UUID) error {
return db.Exec(`UPDATE archives SET status = 'expired' WHERE uuid = ?`, id.String())
}
func (db *DB) DeleteArchive(id uuid.UUID) (bool, error) {
return true, db.Exec(
`DELETE FROM archives WHERE uuid = ?`,
id.String(),
)
}
func (db *DB) ArchiveStorageFootprint(filter *ArchiveFilter) (int64, error) {
var i int64
if filter == nil {
filter = &ArchiveFilter{}
}
wheres := []string{"a.uuid = a.uuid"}
var args []interface{}
if filter.ForTarget != "" {
wheres = append(wheres, "target_uuid = ?")
args = append(args, filter.ForTarget)
}
if filter.ForStore != "" {
wheres = append(wheres, "store_uuid = ?")
args = append(args, filter.ForStore)
}
if filter.Before != nil {
wheres = append(wheres, "taken_at <= ?")
args = append(args, filter.Before.Unix())
}
if filter.After != nil {
wheres = append(wheres, "taken_at >= ?")
args = append(args, filter.After.Unix())
}
if len(filter.WithStatus) > 0 {
var params []string
for _, e := range filter.WithStatus {
params = append(params, "?")
args = append(args, e)
}
wheres = append(wheres, fmt.Sprintf("status IN (%s)", strings.Join(params, ", ")))
}
if len(filter.WithOutStatus) > 0 {
var params []string
for _, e := range filter.WithOutStatus {
params = append(params, "?")
args = append(args, e)
}
wheres = append(wheres, fmt.Sprintf("status NOT IN (%s)", strings.Join(params, ", ")))
}
if filter.ExpiresBefore != nil {
wheres = append(wheres, "expires_at <= ?")
args = append(args, filter.ExpiresBefore.Unix())
}
if filter.ExpiresAfter != nil {
wheres = append(wheres, "expires_at >= ?")
args = append(args, filter.ExpiresAfter.Unix())
}
if filter.ForTenant != "" {
wheres = append(wheres, "a.tenant_uuid = ?")
args = append(args, filter.ForTenant)
}
limit := ""
if filter.Limit > 0 {
limit = " LIMIT ?"
args = append(args, filter.Limit)
}
r, err := db.Query(`
SELECT SUM(a.size)
FROM archives a
INNER JOIN targets t ON t.uuid = a.target_uuid
INNER JOIN stores s ON s.uuid = a.store_uuid
WHERE `+strings.Join(wheres, " AND ")+limit, args...)
if err != nil {
return i, err
}
defer r.Close()
var p *int64
for r.Next() {
if err = r.Scan(&p); err != nil {
return i, err
}
if p != nil {
i = *p
}
return i, nil
}
return i, fmt.Errorf("no results from SUM(size) query...")
}