-
Notifications
You must be signed in to change notification settings - Fork 1
/
clean.go
53 lines (44 loc) · 1.13 KB
/
clean.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
package cmd
import (
"time"
"github.com/lib/pq"
"github.com/spf13/cobra"
"github.com/diamnet/go/services/ticker/internal/tickerdb"
)
var DaysToKeep int
func init() {
rootCmd.AddCommand(cmdClean)
cmdClean.AddCommand(cmdCleanTrades)
cmdCleanTrades.Flags().IntVarP(
&DaysToKeep,
"keep-days",
"k",
7,
"Trade entries older than keep-days will be deleted",
)
}
var cmdClean = &cobra.Command{
Use: "clean [data type]",
Short: "Cleans up the database for a given data type",
}
var cmdCleanTrades = &cobra.Command{
Use: "trades",
Short: "Cleans up old trades from the database",
Run: func(cmd *cobra.Command, args []string) {
dbInfo, err := pq.ParseURL(DatabaseURL)
if err != nil {
Logger.Fatal("could not parse db-url:", err)
}
session, err := tickerdb.CreateSession("postgres", dbInfo)
if err != nil {
Logger.Fatal("could not connect to db:", err)
}
now := time.Now()
minDate := now.AddDate(0, 0, -DaysToKeep)
Logger.Infof("Deleting trade entries older than %d days", DaysToKeep)
err = session.DeleteOldTrades(minDate)
if err != nil {
Logger.Fatal("could not delete trade entries:", err)
}
},
}