-
Notifications
You must be signed in to change notification settings - Fork 10
/
storage.go
817 lines (701 loc) · 24.7 KB
/
storage.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/*
Copyright © 2021, 2022 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
// This source file contains an implementation of interface between Go code and
// (almost any) SQL database like PostgreSQL, SQLite, or MariaDB.
// Generated documentation is available at:
// https://pkg.go.dev/github.com/RedHatInsights/ccx-notification-writer/
//
// Documentation in literate-programming-style is available at:
// https://redhatinsights.github.io/ccx-notification-writer/packages/storage.html
// It is possible to configure connection to selected database by using
// StorageConfiguration structure. Currently that structure contains two
// configurable parameter:
//
// Driver - a SQL driver, like "sqlite3", "pq" etc.
// DataSource - specification of data source. The content of this parameter depends on the database used.
import (
"errors"
"fmt"
"math"
"time"
"database/sql"
types "github.com/RedHatInsights/insights-results-types"
_ "github.com/lib/pq" // PostgreSQL database driver
_ "github.com/mattn/go-sqlite3" // SQLite database driver
"github.com/rs/zerolog/log"
)
// Table creation-related scripts, queries, index creation etc.
const (
// this statement drops the migration_info table
dropTableMigrationInfo = `
DROP TABLE IF EXISTS migration_info
`
// This table contains information about DB schema version and about
// migration status.
createTableMigrationInfo = `
CREATE TABLE IF NOT EXISTS migration_info (
version integer not null
);
`
// This table contains list of all notification types used by
// Notification service. Frequency can be specified as in `crontab` -
// https://crontab.guru/
createTableNotificationTypes = `
CREATE TABLE IF NOT EXISTS notification_types (
id integer not null,
value varchar not null,
frequency varchar not null,
comment varchar,
PRIMARY KEY (id)
);
`
// This table contains states for each row stored in `reported` table.
// User can be notified about the report, report can be skipped if the
// same as previous, skipped because of lower pripority, or can be in
// error state.
createTableStates = `
CREATE TABLE IF NOT EXISTS states (
id integer not null,
value varchar not null,
comment varchar,
PRIMARY KEY (id)
);
`
// Information of notifications reported to user or skipped due to some
// conditions.
createTableReported = `
CREATE TABLE IF NOT EXISTS reported (
org_id integer not null,
account_number integer not null,
cluster character(36) not null,
notification_type integer not null,
state integer not null,
report varchar not null,
updated_at timestamp not null,
notified_at timestamp not null,
error_log varchar,
PRIMARY KEY (org_id, cluster, notified_at),
CONSTRAINT fk_notification_type
foreign key(notification_type)
references notification_types(id),
CONSTRAINT fk_state
foreign key (state)
references states(id)
);
`
// This table contains new reports consumed from Kafka topic and stored
// to database in shrunk format (some attributes are removed).
createTableNewReports = `
CREATE TABLE IF NOT EXISTS new_reports (
org_id integer not null,
account_number integer not null,
cluster character(36) not null,
report varchar not null,
updated_at timestamp not null,
kafka_offset bigint not null default 0,
PRIMARY KEY (org_id, cluster, updated_at)
);
`
// Index for the new_reports table
createIndexKafkaOffset = `
CREATE INDEX IF NOT EXISTS report_kafka_offset_btree_idx
ON new_reports (kafka_offset)
`
// Index for the new_reports table
createIndexNewReportsOrgID = `
CREATE INDEX IF NOT EXISTS new_reports_org_id_idx
ON new_reports
USING btree (org_id);
`
// Index for the new_reports table
createIndexNewReportsCluster = `
CREATE INDEX IF NOT EXISTS new_reports_cluster_idx
ON new_reports
USING btree (cluster);
`
// Index for the new_reports table
createIndexNewReportsUpdatedAtAsc = `
CREATE INDEX IF NOT EXISTS new_reports_updated_at_asc_idx
ON new_reports USING btree (updated_at ASC);
`
// Index for the new_reports table
createIndexNewReportsUpdatedAtDesc = `
CREATE INDEX IF NOT EXISTS new_reports_updated_at_desc_idx
ON new_reports USING btree (updated_at DESC);
`
// Index for the reported table
createIndexReportedNotifiedAtDesc = `
CREATE INDEX IF NOT EXISTS notified_at_desc_idx
ON reported
USING btree (notified_at DESC);
`
// Index for the reported table
createIndexReportedUpdatedAtAsc = `
CREATE INDEX IF NOT EXISTS updated_at_desc_idx
ON reported
USING btree (updated_at ASC);
`
// Index for the notification_types table
createIndexNotificationTypesID = `
CREATE INDEX IF NOT EXISTS notification_types_id_idx
ON notification_types USING btree (id ASC);
`
// Get 0 if DB version is not inserted, 1 instead
isDatabaseVersionExist = `SELECT count(*) FROM migration_info;`
// Get 0 or error if states table is not initialized
isStateTableExist = `SELECT count(*) FROM states;`
// Retrieve DB version
getDatabaseVersion = `SELECT version FROM migration_info LIMIT 1;`
// Display older records from new_reports table
displayOldRecordsFromNewReportsTable = `
SELECT org_id, account_number, cluster, updated_at, kafka_offset
FROM new_reports
WHERE updated_at < NOW() - $1::INTERVAL
ORDER BY updated_at
`
// Delete older records from new_reports table
deleteOldRecordsFromNewReportsTable = `
DELETE
FROM new_reports
WHERE updated_at < NOW() - $1::INTERVAL
`
// Display older records from reported table
displayOldRecordsFromReportedTable = `
SELECT org_id, account_number, cluster, updated_at, 0
FROM reported
WHERE updated_at < NOW() - $1::INTERVAL
ORDER BY updated_at
`
// Delete older records from reported table
deleteOldRecordsFromReportedTable = `
DELETE
FROM reported
WHERE updated_at < NOW() - $1::INTERVAL
`
// Display older records from reported table
displayOldRecordsFromReadErrorsTable = `
SELECT org_id, 0, cluster, updated_at, 0
FROM read_errors
WHERE updated_at < NOW() - $1::INTERVAL
ORDER BY updated_at
`
// Delete older records from new_reports table
deleteOldRecordsFromReadErrorsTable = `
DELETE
FROM read_errors
WHERE updated_at < NOW() - $1::INTERVAL
`
// Value to be stored in migration_info table
insertMigrationVersion = `
INSERT INTO migration_info (version)
VALUES (0);
`
// Value to be stored in notification_types table
insertInstantReport = `
INSERT INTO notification_types (id, value, frequency, comment)
VALUES (1, 'instant', '* * * * * *', 'instant notifications performed ASAP');
`
// Value to be stored in notification_types table
insertWeeklySummary = `
INSERT INTO notification_types (id, value, frequency, comment)
VALUES (2, 'weekly', '@weekly', 'weekly summary');
`
// Value to be stored in states table
insertSentState = `
INSERT INTO states (id, value, comment)
VALUES (1, 'sent', 'notification has been sent to user');
`
// Value to be stored in states table
insertSentSame = `
INSERT INTO states (id, value, comment)
VALUES (2, 'same', 'skipped, report is the same as previous one');
`
// Value to be stored in states table
insertSentLowPriority = `
INSERT INTO states (id, value, comment)
VALUES (3, 'lower', 'skipped, all issues has low priority');
`
// Value to be stored in states table
insertSentError = `
INSERT INTO states (id, value, comment)
VALUES (4, 'error', 'notification delivery error');
`
)
// SQL statements
const (
InsertNewReportStatement = `
INSERT INTO new_reports(org_id, account_number, cluster, report, updated_at, kafka_offset)
VALUES ($1, $2, $3, $4, $5, $6);
`
)
// Messages
const (
SQLStatementMessage = "SQL statement"
StatementMessage = "Statement"
OrgIDMessage = "Organization ID"
AccountNumberMessage = "Account number"
ClusterNameMessage = "Cluster name"
UpdatedAtMessage = "Updated at"
UnableToCloseDBRowsHandle = "Unable to close the DB rows handle"
AgeMessage = "Age"
MaxAgeAttribute = "max age"
)
// Storage represents an interface to almost any database or storage system
type Storage interface {
Close() error
WriteReportForCluster(
orgID types.OrgID,
accountNumber types.AccountNumber,
clusterName types.ClusterName,
report types.ClusterReport,
collectedAtTime time.Time,
kafkaOffset types.KafkaOffset,
) error
DatabaseInitialization() error
DatabaseCleanup() error
DatabaseDropTables() error
DatabaseDropIndexes() error
DatabaseInitMigration() error
GetLatestKafkaOffset() (types.KafkaOffset, error)
PrintNewReportsForCleanup(maxAge string) error
CleanupNewReports(maxAge string) (int, error)
PrintOldReportsForCleanup(maxAge string) error
CleanupOldReports(maxAge string) (int, error)
PrintReadErrorsForCleanup(maxAge string) error
CleanupReadErrors(maxAge string) (int, error)
}
// DBStorage is an implementation of Storage interface that use selected SQL like database
// like SQLite, PostgreSQL, MariaDB, RDS etc. That implementation is based on the standard
// sql package. It is possible to configure connection via Configuration structure.
// SQLQueriesLog is log for sql queries, default is nil which means nothing is logged
type DBStorage struct {
connection *sql.DB
dbDriverType DBDriver
}
// ErrOldReport is an error returned if a more recent already
// exists on the storage while attempting to write a report for a cluster.
var ErrOldReport = errors.New("more recent report already exists in storage")
// tableNames contains names of all tables in the database.
var tableNames []string
// initStatements contains all statements used to initialize database
var initStatements []string
// NewStorage function creates and initializes a new instance of Storage interface
func NewStorage(configuration *StorageConfiguration) (*DBStorage, error) {
log.Info().Msg("Initializing connection to storage")
driverType, driverName, dataSource, err := initAndGetDriver(configuration)
if err != nil {
log.Error().Err(err).Msg("Unsupported driver")
return nil, err
}
log.Info().
Str("driver", driverName).
Msg("Making connection to data storage")
// prepare connection
connection, err := sql.Open(driverName, dataSource)
if err != nil {
log.Error().Err(err).Msg("Can not connect to data storage")
return nil, err
}
// lazy initialization (TODO: use init function instead?)
tableNames = []string{
"migration_info",
"new_reports",
"reported",
"notification_types",
"states",
}
// lazy initialization (TODO: use init function instead?)
initStatements = []string{
// tables
createTableNotificationTypes,
createTableStates,
createTableReported,
createTableNewReports,
// indexes
createIndexKafkaOffset,
createIndexNewReportsOrgID,
createIndexNewReportsCluster,
createIndexNewReportsUpdatedAtAsc,
createIndexNewReportsUpdatedAtDesc,
createIndexReportedNotifiedAtDesc,
createIndexReportedUpdatedAtAsc,
createIndexNotificationTypesID,
// records
insertInstantReport,
insertWeeklySummary,
insertSentState,
insertSentSame,
insertSentLowPriority,
insertSentError,
}
log.Info().Msg("Connection to storage established")
return NewFromConnection(connection, driverType), nil
}
// NewFromConnection function creates and initializes a new instance of Storage interface from prepared connection
func NewFromConnection(connection *sql.DB, dbDriverType DBDriver) *DBStorage {
return &DBStorage{
connection: connection,
dbDriverType: dbDriverType,
}
}
// initAndGetDriver initializes driver(with logs if logSQLQueries is true),
// checks if it's supported and returns driver type, driver name, dataSource and error
func initAndGetDriver(configuration *StorageConfiguration) (driverType DBDriver, driverName, dataSource string, err error) {
driverName = configuration.Driver
switch driverName {
case "sqlite3":
driverType = DBDriverSQLite3
case "postgres":
driverType = DBDriverPostgres
dataSource = fmt.Sprintf(
"postgresql://%v:%v@%v:%v/%v?%v",
configuration.PGUsername,
configuration.PGPassword,
configuration.PGHost,
configuration.PGPort,
configuration.PGDBName,
configuration.PGParams,
)
default:
err = fmt.Errorf("driver %v is not supported", driverName)
return
}
return
}
// Close method closes the connection to database. Needs to be called at the end of application lifecycle.
func (storage DBStorage) Close() error {
log.Info().Msg("Closing connection to data storage")
if storage.connection != nil {
err := storage.connection.Close()
if err != nil {
log.Error().Err(err).Msg("Can not close connection to data storage")
return err
}
}
return nil
}
// WriteReportForCluster writes result (health status) for selected cluster for given organization
func (storage DBStorage) WriteReportForCluster(
orgID types.OrgID,
accountNumber types.AccountNumber,
clusterName types.ClusterName,
report types.ClusterReport,
lastCheckedTime time.Time,
kafkaOffset types.KafkaOffset,
) error {
if storage.dbDriverType != DBDriverSQLite3 && storage.dbDriverType != DBDriverPostgres {
return fmt.Errorf("writing report with DB %v is not supported", storage.dbDriverType)
}
// Begin a new transaction.
tx, err := storage.connection.Begin()
if err != nil {
return err
}
err = func(tx *sql.Tx) error {
err = storage.insertReport(tx, orgID, accountNumber, clusterName, report, lastCheckedTime, kafkaOffset)
if err != nil {
return err
}
return nil
}(tx)
finishTransaction(tx, err)
return err
}
func (storage DBStorage) insertReport(
tx *sql.Tx,
orgID types.OrgID,
accountNumber types.AccountNumber,
clusterName types.ClusterName,
report types.ClusterReport,
lastCheckedTime time.Time,
kafkaOffset types.KafkaOffset,
) error {
_, err := tx.Exec(InsertNewReportStatement, orgID, accountNumber, clusterName, report, lastCheckedTime, kafkaOffset)
if err != nil {
log.Err(err).
Int("org", int(orgID)).
Int("account", int(accountNumber)).
Int64("kafka offset", int64(kafkaOffset)).
Str("cluster", string(clusterName)).
Str("last checked", lastCheckedTime.String()).
Msg("Unable to insert the cluster report")
return err
}
return nil
}
// finishTransaction finishes the transaction depending on err. err == nil -> commit, err != nil -> rollback
func finishTransaction(tx *sql.Tx, err error) {
if err != nil {
rollbackError := tx.Rollback()
if rollbackError != nil {
log.Err(rollbackError).Msg("Error when trying to rollback a transaction")
}
} else {
commitError := tx.Commit()
if commitError != nil {
log.Err(commitError).Msg("Error when trying to commit a transaction")
}
}
}
func tablesRelatedOperation(storage DBStorage, cmd func(string) string) error {
// Begin a new transaction.
tx, err := storage.connection.Begin()
if err != nil {
return err
}
err = func(tx *sql.Tx) error {
// perform some operation to all tables
for _, tableName := range tableNames {
// it is not possible to use parameter for table name or a key
// disable "G202 (CWE-89): SQL string concatenation (Confidence: HIGH, Severity: MEDIUM)"
// #nosec G202
sqlStatement := cmd(tableName)
log.Info().Str(StatementMessage, sqlStatement).Msg(SQLStatementMessage)
// perform the SQL statement in transaction
_, err := tx.Exec(sqlStatement)
if err != nil {
return err
}
}
return nil
}(tx)
finishTransaction(tx, err)
return err
}
func deleteFromTableStatement(tableName string) string {
// it is not possible to use parameter for table name or a key
// disable "G202 (CWE-89): SQL string concatenation (Confidence: HIGH, Severity: MEDIUM)"
// #nosec G202
return "DELETE FROM " + tableName + ";"
}
// DatabaseCleanup method performs database cleanup - deletes content of all
// tables in database.
func (storage DBStorage) DatabaseCleanup() error {
err := tablesRelatedOperation(storage, deleteFromTableStatement)
return err
}
func dropTableStatement(tableName string) string {
return "DROP TABLE " + tableName + ";"
}
// DatabaseDropTables method performs database drop for all tables in database.
func (storage DBStorage) DatabaseDropTables() error {
err := tablesRelatedOperation(storage, dropTableStatement)
return err
}
func dropIndexStatement(indexName string) string {
return "DROP INDEX IF EXISTS " + indexName + ";"
}
// DatabaseDropIndexes method performs database drop for all tables in database.
func (storage DBStorage) DatabaseDropIndexes() error {
err := tablesRelatedOperation(storage, dropIndexStatement)
return err
}
// GetDatabaseVersionInfo method tries to retrieve database version from
// migration table.
func (storage DBStorage) GetDatabaseVersionInfo() (int, error) {
// check if version info is stored in the database
var count int
err := storage.connection.QueryRow(isDatabaseVersionExist).Scan(&count)
if err != nil {
return -1, err
}
// table exists, but does not containing DB version
if count == 0 {
return -1, nil
}
// process version info
var version int
err = storage.connection.QueryRow(getDatabaseVersion).Scan(&version)
if err != nil {
return -1, err
}
return version, nil
}
// DatabaseInitMigration method initializes migration_info table
func (storage DBStorage) DatabaseInitMigration() error {
// Begin a new transaction.
tx, err := storage.connection.Begin()
if err != nil {
return err
}
err = func(tx *sql.Tx) error {
// check if table already exists
version, err := storage.GetDatabaseVersionInfo()
if version >= 0 && err == nil {
// version_info table already created
log.Info().Msgf("database current version: %v", version)
return nil
}
// erase old migration table
log.Info().Str(StatementMessage, dropTableMigrationInfo).Msg(SQLStatementMessage)
_, err = tx.Exec(dropTableMigrationInfo)
if err != nil {
return err
}
// migration_info table creation
log.Info().Str(StatementMessage, createTableMigrationInfo).Msg(SQLStatementMessage)
_, err = tx.Exec(createTableMigrationInfo)
if err != nil {
return err
}
// set version to zero
log.Info().Str(StatementMessage, insertMigrationVersion).Msg(SQLStatementMessage)
_, err = tx.Exec(insertMigrationVersion)
if err != nil {
return err
}
return nil
}(tx)
finishTransaction(tx, err)
return err
}
// DatabaseInitialization method performs database initialization - creates all
// tables in database.
func (storage DBStorage) DatabaseInitialization() error {
// Begin a new transaction.
tx, err := storage.connection.Begin()
if err != nil {
return err
}
err = func(tx *sql.Tx) error {
// check if database is already initialized
var count int
err := storage.connection.QueryRow(isStateTableExist).Scan(&count)
if count > 0 && err == nil {
log.Info().Msg("Database is already initialized")
return nil
}
// databaze initialization
for _, sqlStatement := range initStatements {
log.Info().Str(StatementMessage, sqlStatement).Msg(SQLStatementMessage)
// perform the SQL statement in transaction
_, err := tx.Exec(sqlStatement)
if err != nil {
return err
}
}
return nil
}(tx)
finishTransaction(tx, err)
return err
}
// GetLatestKafkaOffset returns latest kafka offset from report table
func (storage DBStorage) GetLatestKafkaOffset() (types.KafkaOffset, error) {
var offset types.KafkaOffset
err := storage.connection.QueryRow("SELECT COALESCE(MAX(kafka_offset), 0) FROM new_reports;").Scan(&offset)
return offset, err
}
// PrintNewReports method prints all reports from selected table older than
// specified relative time
func (storage DBStorage) PrintNewReports(maxAge, query, tableName string) error {
log.Info().
Str(MaxAgeAttribute, maxAge).
Str("select statement", query).
Msg("PrintReportsForCleanup operation")
rows, err := storage.connection.Query(query, maxAge)
if err != nil {
return err
}
// used to compute a real record age
now := time.Now()
// iterate over all old records
for rows.Next() {
var (
orgID int
accountNumber int
clusterName string
updatedAt time.Time
kafkaOffset int64
)
// read one old record from the report table
if err := rows.Scan(&orgID, &accountNumber, &clusterName, &updatedAt, &kafkaOffset); err != nil {
// close the result set in case of any error
if closeErr := rows.Close(); closeErr != nil {
log.Error().Err(closeErr).Msg(UnableToCloseDBRowsHandle)
}
return err
}
// compute the real record age
age := int(math.Ceil(now.Sub(updatedAt).Hours() / 24)) // in days
// prepare for the report
updatedAtF := updatedAt.Format(time.RFC3339)
// just print the report
log.Info().
Int(OrgIDMessage, orgID).
Int(AccountNumberMessage, accountNumber).
Str(ClusterNameMessage, clusterName).
Str(UpdatedAtMessage, updatedAtF).
Int(AgeMessage, age).
Msg("Old report from `" + tableName + "` table")
}
return nil
}
// PrintNewReportsForCleanup method prints all reports from `new_reports` table
// older than specified relative time
func (storage DBStorage) PrintNewReportsForCleanup(maxAge string) error {
return storage.PrintNewReports(maxAge, displayOldRecordsFromNewReportsTable, "new_reports")
}
// PrintOldReportsForCleanup method prints all reports from `reported` table
// older than specified relative time
func (storage DBStorage) PrintOldReportsForCleanup(maxAge string) error {
return storage.PrintNewReports(maxAge, displayOldRecordsFromReportedTable, "reported")
}
// PrintReadErrorsForCleanup method prints all recorsd from `read_errors` table
// older than specified relative time
func (storage DBStorage) PrintReadErrorsForCleanup(maxAge string) error {
return storage.PrintNewReports(maxAge, displayOldRecordsFromReadErrorsTable, "read_errors")
}
// Cleanup method deletes all reports older than specified
// relative time
func (storage DBStorage) Cleanup(maxAge, statement string) (int, error) {
log.Info().
Str(MaxAgeAttribute, maxAge).
Str("delete statement", statement).
Msg("Cleanup operation")
// perform the SQL statement
result, err := storage.connection.Exec(statement, maxAge)
if err != nil {
return 0, err
}
// read number of affected (deleted) rows
affected, err := result.RowsAffected()
if err != nil {
return 0, err
}
return int(affected), nil
}
// CleanupNewReports method deletes all reports from `new_reports` table older
// than specified relative time
func (storage DBStorage) CleanupNewReports(maxAge string) (int, error) {
return storage.Cleanup(maxAge, deleteOldRecordsFromNewReportsTable)
}
// CleanupOldReports method deletes all reports from `reported` table older
// than specified relative time
func (storage DBStorage) CleanupOldReports(maxAge string) (int, error) {
return storage.Cleanup(maxAge, deleteOldRecordsFromReportedTable)
}
// CleanupReadErrors method deletes all reports from `read_errors` table older
// than specified relative time
func (storage DBStorage) CleanupReadErrors(maxAge string) (int, error) {
return storage.Cleanup(maxAge, deleteOldRecordsFromReadErrorsTable)
}
// Connection returns the storage connection
func (storage DBStorage) Connection() *sql.DB {
return storage.connection
}
// TruncateOldReports method truncates the reported table.
func (storage DBStorage) TruncateOldReports() error {
_, err := storage.connection.Exec("TRUNCATE TABLE reported;")
return err
}