From 5f673a19df14d834a76ba6f382cc29e0e7e270e0 Mon Sep 17 00:00:00 2001 From: Tim van Osch Date: Thu, 23 Jan 2025 11:48:41 +0100 Subject: [PATCH] feat(tracing): must specify days as flag in cleanup cmd --- services/tracing/cmd/cleanDatabase.go | 3 ++- services/tracing/cmd/root.go | 7 +++++++ services/tracing/tracing/tracing.go | 8 ++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/services/tracing/cmd/cleanDatabase.go b/services/tracing/cmd/cleanDatabase.go index 65cad79..8e1d442 100644 --- a/services/tracing/cmd/cleanDatabase.go +++ b/services/tracing/cmd/cleanDatabase.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "time" "github.com/urfave/cli/v2" "sensorbucket.nl/sensorbucket/services/tracing/tracing" @@ -14,7 +15,7 @@ func cmdCleanDatabase(cmd *cli.Context) error { } svc := tracing.Create(db) - if err := svc.PeriodicCleanup(); err != nil { + if err := svc.PeriodicCleanup(time.Duration(cmd.Float64("days")) * time.Hour * 24); err != nil { return fmt.Errorf("could not perform database cleanup: %w", err) } diff --git a/services/tracing/cmd/root.go b/services/tracing/cmd/root.go index 25ab77f..efcc95f 100644 --- a/services/tracing/cmd/root.go +++ b/services/tracing/cmd/root.go @@ -12,6 +12,13 @@ var App = &cli.App{ { Name: "cleanup", Action: cmdCleanDatabase, + Flags: []cli.Flag{ + &cli.Float64Flag{ + Name: "days", + Usage: "Keep only data since ago. Fractions allowed, a day is specified as 24 hours", + Required: true, + }, + }, }, }, } diff --git a/services/tracing/tracing/tracing.go b/services/tracing/tracing/tracing.go index 6593e59..61dc4aa 100644 --- a/services/tracing/tracing/tracing.go +++ b/services/tracing/tracing/tracing.go @@ -272,12 +272,12 @@ func (svc *Service) Query(ctx context.Context, filters TraceFilter, r pagination return &page, nil } -func (svc *Service) PeriodicCleanup() error { +func (svc *Service) PeriodicCleanup(duration time.Duration) error { tx, err := svc.db.Beginx() if err != nil { return fmt.Errorf("") } - n, err := tx.Exec(`DELETE FROM traces WHERE created_at < (NOW() - $1::INTERVAL)`, "1 day") + n, err := tx.Exec(`DELETE FROM traces WHERE created_at < (NOW() - $1::INTERVAL)`, duration) if err != nil { var rollbackErr error if rbErr := tx.Rollback(); rbErr != nil { @@ -290,7 +290,7 @@ func (svc *Service) PeriodicCleanup() error { log.Printf("Cleaned %d traces\n", n) } - n, err = tx.Exec(`DELETE FROM trace_steps WHERE queue_time < (NOW() - $1::INTERVAL)`, "1 day") + n, err = tx.Exec(`DELETE FROM trace_steps WHERE queue_time < (NOW() - $1::INTERVAL)`, duration) if err != nil { var rollbackErr error if rbErr := tx.Rollback(); rbErr != nil { @@ -303,7 +303,7 @@ func (svc *Service) PeriodicCleanup() error { log.Printf("Cleaned %d trace steps\n", n) } - n, err = tx.Exec(`DELETE FROM trace_ingress WHERE archived_at < (NOW() - $1::INTERVAL)`, "1 day") + n, err = tx.Exec(`DELETE FROM trace_ingress WHERE archived_at < (NOW() - $1::INTERVAL)`, duration) if err != nil { var rollbackErr error if rbErr := tx.Rollback(); rbErr != nil {