From fe0a4ab0cb4cbb3993c027595db44e08c4d6ce35 Mon Sep 17 00:00:00 2001 From: Nityananda Gohain Date: Fri, 7 Oct 2022 20:06:01 +0530 Subject: [PATCH] Fix/delete old snapshot (#1621) * fix: remove old snapshots --- ee/query-service/usage/manager.go | 2 ++ ee/query-service/usage/repository/repository.go | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/ee/query-service/usage/manager.go b/ee/query-service/usage/manager.go index 59f16b1a3d..067b65e0b4 100644 --- a/ee/query-service/usage/manager.go +++ b/ee/query-service/usage/manager.go @@ -139,6 +139,8 @@ func (lm *Manager) UsageExporter(ctx context.Context) { lm.CollectAndStoreUsage(ctx) case <-uploadTicker.C: lm.UploadUsage(ctx) + // remove the old snapshots + lm.repository.DropOldSnapshots(ctx) } } } diff --git a/ee/query-service/usage/repository/repository.go b/ee/query-service/usage/repository/repository.go index 99bd4c5796..57bf5388b6 100644 --- a/ee/query-service/usage/repository/repository.go +++ b/ee/query-service/usage/repository/repository.go @@ -17,6 +17,7 @@ import ( const ( MaxFailedSyncCount = 9 // a snapshot will be ignored if the max failed count is greater than or equal to 9 + SnapShotLife = 3 * 24 * time.Hour ) // Repository is usage Repository which stores usage snapshot in a secured DB @@ -111,6 +112,18 @@ func (r *Repository) GetSnapshotsNotSynced(ctx context.Context) ([]*model.Usage, return snapshots, nil } +func (r *Repository) DropOldSnapshots(ctx context.Context) error { + query := `delete from usage where created_at <= $1` + + _, err := r.db.ExecContext(ctx, query, time.Now().Add(-(SnapShotLife))) + if err != nil { + zap.S().Errorf("failed to remove old snapshots from db: %v", zap.Error(err)) + return err + } + + return nil +} + // CheckSnapshotGtCreatedAt checks if there is any snapshot greater than the provided timestamp func (r *Repository) CheckSnapshotGtCreatedAt(ctx context.Context, ts time.Time) (bool, error) {