Skip to content

Commit

Permalink
feat(tracing): must specify days as flag in cleanup cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
TimVosch committed Jan 23, 2025
1 parent 150a4ce commit 5f673a1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
3 changes: 2 additions & 1 deletion services/tracing/cmd/cleanDatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"time"

"github.com/urfave/cli/v2"
"sensorbucket.nl/sensorbucket/services/tracing/tracing"
Expand All @@ -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)
}

Expand Down
7 changes: 7 additions & 0 deletions services/tracing/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ var App = &cli.App{
{
Name: "cleanup",
Action: cmdCleanDatabase,
Flags: []cli.Flag{
&cli.Float64Flag{
Name: "days",
Usage: "Keep only data since <value> ago. Fractions allowed, a day is specified as 24 hours",
Required: true,
},
},
},
},
}
8 changes: 4 additions & 4 deletions services/tracing/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 5f673a1

Please sign in to comment.