Skip to content

Commit 040e49b

Browse files
committed
Loop over each bucket when finding keepers
1 parent f31694e commit 040e49b

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

app/prune.go

+21-29
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ type snapshot struct {
2424
Time time.Time // Parsed timestamp from the dataset name
2525
}
2626

27+
// FIXME PruneSnapshots does not actually perform any destructive operations
28+
// on your datasets at this time.
2729
func PruneSnapshots(job *config.JobConfig) {
2830
var host = job.Host()
2931

30-
// Set defaults if config is not set
32+
// Defaults: if config is not set
3133
if job.Retention == nil {
3234
job.Retention = &config.RetentionConfig{
3335
Daily: 100000,
@@ -37,7 +39,7 @@ func PruneSnapshots(job *config.JobConfig) {
3739
}
3840
}
3941

40-
// This catches any gaps in the config
42+
// Defaults: catch any gaps in the config
4143
if job.Retention.Daily == 0 {
4244
job.Retention.Daily = 100000
4345
}
@@ -51,51 +53,41 @@ func PruneSnapshots(job *config.JobConfig) {
5153
job.Retention.Yearly = 100000
5254
}
5355

54-
// FIXME probably should iterate over a list instead here
55-
for _, snapshot := range listKeepers(host, "daily", job.Retention.Daily) {
56-
log.WithFields(logrus.Fields{
57-
"snapshot": snapshot,
58-
"period": "daily",
59-
}).Debug("keeping snapshot")
56+
var keep_counts = map[string]uint{
57+
"daily": job.Retention.Daily,
58+
"weekly": job.Retention.Weekly,
59+
"monthly": job.Retention.Monthly,
60+
"yearly": job.Retention.Yearly,
6061
}
61-
for _, snapshot := range listKeepers(host, "weekly", job.Retention.Weekly) {
62-
log.WithFields(logrus.Fields{
63-
"snapshot": snapshot,
64-
"period": "weekly",
65-
}).Debug("keeping snapshot")
66-
}
67-
for _, snapshot := range listKeepers(host, "monthly", job.Retention.Monthly) {
68-
log.WithFields(logrus.Fields{
69-
"snapshot": snapshot,
70-
"period": "monthly",
71-
}).Debug("keeping snapshot")
72-
}
73-
for _, snapshot := range listKeepers(host, "yearly", job.Retention.Yearly) {
74-
log.WithFields(logrus.Fields{
75-
"snapshot": snapshot,
76-
"period": "yearly",
77-
}).Debug("keeping snapshot")
62+
63+
for bucket, keep_count := range keep_counts {
64+
for _, snapshot := range listKeepers(host, bucket, keep_count) {
65+
log.WithFields(logrus.Fields{
66+
"snapshot": snapshot,
67+
"bucket": bucket,
68+
}).Debug("keeping snapshot")
69+
}
7870
}
7971

8072
// TODO subtract keepers from the list of snapshots and rm -rf them
8173
}
8274

8375
// listKeepers returns a list of snapshot that are not subject to deletion
8476
// for a given host, pattern, and keep_count.
85-
func listKeepers(host string, pattern string, keep_count uint) []snapshot {
77+
func listKeepers(host string, bucket string, keep_count uint) []snapshot {
8678
var keepers []snapshot
8779
var last string
8880

8981
for _, snapshot := range listSnapshots(host) {
9082
var period string
9183

9284
// Weekly is special because golang doesn't have support for "week number in year"
93-
// in Time.Format strings.
94-
if pattern == "weekly" {
85+
// as Time.Format string pattern.
86+
if bucket == "weekly" {
9587
year, week := snapshot.Time.Local().ISOWeek()
9688
period = fmt.Sprintf("%d-%d", year, week)
9789
} else {
98-
period = snapshot.Time.Local().Format(patterns[pattern])
90+
period = snapshot.Time.Local().Format(patterns[bucket])
9991
}
10092

10193
if period != last {

0 commit comments

Comments
 (0)