Skip to content

Commit

Permalink
[policy] Add ByRetentionAscResolutionAsc for StoragePolicies (#1477)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnikola authored Mar 19, 2019
1 parent dec08fe commit bcc109b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/metrics/policy/storage_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,31 @@ func (sp ByResolutionAscRetentionDesc) Swap(i, j int) { sp[i], sp[j] = sp[j], sp

func (sp ByResolutionAscRetentionDesc) Less(i, j int) bool {
rw1, rw2 := sp[i].Resolution().Window, sp[j].Resolution().Window
if rw1 < rw2 {
return true
if rw1 != rw2 {
return rw1 < rw2
}
if rw1 > rw2 {
return false
rt1, rt2 := sp[i].Retention(), sp[j].Retention()
if rt1 != rt2 {
return rt1 > rt2
}
return sp[i].Resolution().Precision < sp[j].Resolution().Precision
}

// ByRetentionAscResolutionAsc implements the sort.Sort interface that enables sorting
// storage policies by retention in ascending order and then by resolution in ascending
// order.
type ByRetentionAscResolutionAsc StoragePolicies

func (sp ByRetentionAscResolutionAsc) Len() int { return len(sp) }
func (sp ByRetentionAscResolutionAsc) Swap(i, j int) { sp[i], sp[j] = sp[j], sp[i] }
func (sp ByRetentionAscResolutionAsc) Less(i, j int) bool {
rt1, rt2 := sp[i].Retention(), sp[j].Retention()
if rt1 > rt2 {
return true
if rt1 != rt2 {
return rt1 < rt2
}
if rt1 < rt2 {
return false
rw1, rw2 := sp[i].Resolution().Window, sp[j].Resolution().Window
if rw1 != rw2 {
return rw1 < rw2
}
return sp[i].Resolution().Precision < sp[j].Resolution().Precision
}
32 changes: 32 additions & 0 deletions src/metrics/policy/storage_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,35 @@ func TestStoragePoliciesByResolutionAscRetentionDesc(t *testing.T) {
}
require.Equal(t, expected, inputs)
}

func TestStoragePoliciesByRetentionAscResolutionAsc(t *testing.T) {
inputs := StoragePolicies{
NewStoragePolicy(10*time.Second, xtime.Second, 6*time.Hour),
NewStoragePolicy(10*time.Second, xtime.Second, 2*time.Hour),
NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
NewStoragePolicy(5*time.Minute, xtime.Minute, 48*time.Hour),
NewStoragePolicy(time.Minute, xtime.Minute, time.Hour),
NewStoragePolicy(time.Minute, xtime.Minute, 24*time.Hour),
NewStoragePolicy(10*time.Minute, xtime.Minute, 48*time.Hour),
NewStoragePolicy(10*time.Minute, xtime.Minute, 48*time.Hour),
NewStoragePolicy(10*time.Second, xtime.Minute, 12*time.Hour),
NewStoragePolicy(10*time.Minute, xtime.Minute, 48*time.Hour),
NewStoragePolicy(11*time.Second, xtime.Second, 2*time.Hour),
}
sort.Sort(ByRetentionAscResolutionAsc(inputs))

expected := StoragePolicies{
NewStoragePolicy(time.Minute, xtime.Minute, time.Hour),
NewStoragePolicy(10*time.Second, xtime.Second, 2*time.Hour),
NewStoragePolicy(11*time.Second, xtime.Second, 2*time.Hour),
NewStoragePolicy(10*time.Second, xtime.Second, 6*time.Hour),
NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
NewStoragePolicy(10*time.Second, xtime.Minute, 12*time.Hour),
NewStoragePolicy(time.Minute, xtime.Minute, 24*time.Hour),
NewStoragePolicy(5*time.Minute, xtime.Minute, 48*time.Hour),
NewStoragePolicy(10*time.Minute, xtime.Minute, 48*time.Hour),
NewStoragePolicy(10*time.Minute, xtime.Minute, 48*time.Hour),
NewStoragePolicy(10*time.Minute, xtime.Minute, 48*time.Hour),
}
require.Equal(t, expected, inputs)
}

0 comments on commit bcc109b

Please sign in to comment.