Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
more working tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Skelcy committed Mar 15, 2018
1 parent b8f7618 commit 036dd4d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 60 deletions.
23 changes: 12 additions & 11 deletions matcher/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/m3db/m3metrics/matcher/cache"
"github.com/m3db/m3metrics/metric"
"github.com/m3db/m3metrics/rules"
"github.com/m3db/m3metrics/rules/models"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -256,17 +257,17 @@ type mockRuleSet struct {
matcher *mockMatcher
}

func (r *mockRuleSet) Namespace() []byte { return []byte(r.namespace) }
func (r *mockRuleSet) Version() int { return r.version }
func (r *mockRuleSet) CutoverNanos() int64 { return r.cutoverNanos }
func (r *mockRuleSet) LastUpdatedAtNanos() int64 { return 0 }
func (r *mockRuleSet) CreatedAtNanos() int64 { return 0 }
func (r *mockRuleSet) Tombstoned() bool { return r.tombstoned }
func (r *mockRuleSet) ActiveSet(timeNanos int64) rules.Matcher { return r.matcher }
func (r *mockRuleSet) ToMutableRuleSet() rules.MutableRuleSet { return nil }
func (r *mockRuleSet) MappingRules() (rules.MappingRules, error) { return nil, nil }
func (r *mockRuleSet) RollupRules() (rules.RollupRules, error) { return nil, nil }
func (r *mockRuleSet) Latest() (*rules.RuleSetSnapshot, error) { return nil, nil }
func (r *mockRuleSet) Namespace() []byte { return []byte(r.namespace) }
func (r *mockRuleSet) Version() int { return r.version }
func (r *mockRuleSet) CutoverNanos() int64 { return r.cutoverNanos }
func (r *mockRuleSet) LastUpdatedAtNanos() int64 { return 0 }
func (r *mockRuleSet) CreatedAtNanos() int64 { return 0 }
func (r *mockRuleSet) Tombstoned() bool { return r.tombstoned }
func (r *mockRuleSet) ActiveSet(timeNanos int64) rules.Matcher { return r.matcher }
func (r *mockRuleSet) ToMutableRuleSet() rules.MutableRuleSet { return nil }
func (r *mockRuleSet) MappingRules() (models.MappingRuleViews, error) { return nil, nil }
func (r *mockRuleSet) RollupRules() (models.RollupRuleViews, error) { return nil, nil }
func (r *mockRuleSet) Latest() (*models.RuleSetSnapshotView, error) { return nil, nil }

func testRuleSet() (kv.Store, cache.Cache, *ruleSet) {
store := mem.NewStore()
Expand Down
4 changes: 4 additions & 0 deletions rules/models/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type MappingRuleView struct {
LastUpdatedAtNanos int64
}

// MappingRuleViews belonging to a ruleset indexed by uuid.
// Each value contains the entire snapshot history of the rule.
type MappingRuleViews map[string][]*MappingRuleView

// NewMappingRule takes a MappingRuleView and returns the equivalent MappingRule.
func NewMappingRule(mrv *MappingRuleView) MappingRule {
return MappingRule{
Expand Down
49 changes: 24 additions & 25 deletions rules/models/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,6 @@ type RollupRule struct {
LastUpdatedAtMillis int64 `json:"lastUpdatedAtMillis"`
}

// RollupTargetView is a human friendly representation of a rollup rule target at a given point in time.
type RollupTargetView struct {
Name string
Tags []string
Policies []policy.Policy
}

// RollupRuleView is a human friendly representation of a rollup rule at a given point in time.
type RollupRuleView struct {
ID string
Name string
Tombstoned bool
CutoverNanos int64
Filter string
Targets []RollupTargetView
LastUpdatedBy string
LastUpdatedAtNanos int64
}

// MappingRuleViews belonging to a ruleset indexed by uuid.
// Each value contains the entire snapshot history of the rule.
type MappingRuleViews map[string][]*MappingRuleView

// RollupRuleViews belonging to a ruleset indexed by uuid.
// Each value contains the entire snapshot history of the rule.
type RollupRuleViews map[string][]*RollupRuleView
Expand Down Expand Up @@ -162,16 +139,38 @@ func (r *RollupRule) Sort() {
sort.Sort(rollupTargetsByNameTagsAsc(r.Targets))
}

// RollupTargetView is a human friendly representation of a rollup rule target at a given point in time.
type RollupTargetView struct {
Name string
Tags []string
Policies []policy.Policy
}

// RollupRuleView is a human friendly representation of a rollup rule at a given point in time.
type RollupRuleView struct {
ID string
Name string
Tombstoned bool
CutoverNanos int64
Filter string
Targets []RollupTargetView
LastUpdatedBy string
LastUpdatedAtNanos int64
}

// SameTransform returns whether two rollup targets have the same transformation.
func (rtv *RollupTargetView) SameTransform(other RollupTargetView) bool {
if rtv.Name != other.Name {
return false
}
if len(rtv.Tags) != len(other.Tags) {
return false
}
clonedTags := rtv.Tags
clonedTags := make([]string, len(rtv.Tags))
otherClonedTags := make([]string, len(other.Tags))
copy(clonedTags, rtv.Tags)
sort.Strings(clonedTags)
otherClonedTags := other.Tags
copy(otherClonedTags, other.Tags)
sort.Strings(otherClonedTags)
for i := 0; i < len(clonedTags); i++ {
if clonedTags[i] != otherClonedTags[i] {
Expand Down
42 changes: 21 additions & 21 deletions rules/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,14 @@ var (
errNilRollupRuleSchema = errors.New("nil rollup rule schema")
)

func newRollupTargetView(target rollupTarget) models.RollupTargetView {
return models.RollupTargetView{
Name: string(target.Name),
Tags: stringArrayFromBytesArray(target.Tags),
Policies: target.Policies,
}
}

func rollupTargetViewsToTargets(views []models.RollupTargetView) []rollupTarget {
targets := make([]rollupTarget, len(views))
for i, t := range views {
targets[i] = rollupTargetFromView(t)
targets[i] = newRollupTargetFromView(t)
}
return targets
}

func rollupTargetFromView(rtv models.RollupTargetView) rollupTarget {
return rollupTarget{
Name: []byte(rtv.Name),
Tags: bytesArrayFromStringArray(rtv.Tags),
Policies: rtv.Policies,
}
}

// RollupTarget dictates how to roll up metrics. Metrics associated with a rollup
// target will be grouped and rolled up across the provided set of tags, named
// with the provided name, and aggregated and retained under the provided policies.
Expand Down Expand Up @@ -94,10 +78,26 @@ func newRollupTarget(target *schema.RollupTarget) (rollupTarget, error) {
}, nil
}

func newRollupTargetFromView(rtv models.RollupTargetView) rollupTarget {
return rollupTarget{
Name: []byte(rtv.Name),
Tags: bytesArrayFromStringArray(rtv.Tags),
Policies: rtv.Policies,
}
}

func (t rollupTarget) rollupTargetView() models.RollupTargetView {
return models.RollupTargetView{
Name: string(t.Name),
Tags: stringArrayFromBytesArray(t.Tags),
Policies: t.Policies,
}
}

// SameTransform returns whether two rollup targets have the same transformation.
func (t *rollupTarget) SameTransform(other rollupTarget) bool {
tView := newRollupTargetView(*t)
otherView := newRollupTargetView(other)
func (t rollupTarget) SameTransform(other rollupTarget) bool {
tView := t.rollupTargetView()
otherView := other.rollupTargetView()
return tView.SameTransform(otherView)
}

Expand Down Expand Up @@ -284,7 +284,7 @@ func (rc *rollupRule) rollupRuleView(snapshotIdx int) (*models.RollupRuleView, e
rrs := rc.snapshots[snapshotIdx].clone()
targets := make([]models.RollupTargetView, len(rrs.targets))
for i, t := range rrs.targets {
targets[i] = newRollupTargetView(t)
targets[i] = t.rollupTargetView()
}

return &models.RollupRuleView{
Expand Down
7 changes: 4 additions & 3 deletions rules/store/kv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/m3db/m3cluster/kv/mem"
"github.com/m3db/m3metrics/generated/proto/schema"
"github.com/m3db/m3metrics/rules"
"github.com/m3db/m3metrics/rules/models"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -585,6 +586,6 @@ type mockValidator struct {
validateFn validateFn
}

func (v *mockValidator) Validate(rs rules.RuleSet) error { return v.validateFn(rs) }
func (v *mockValidator) ValidateSnapshot(snapshot *rules.RuleSetSnapshot) error { return nil }
func (v *mockValidator) Close() {}
func (v *mockValidator) Validate(rs rules.RuleSet) error { return v.validateFn(rs) }
func (v *mockValidator) ValidateSnapshot(snapshot *models.RuleSetSnapshotView) error { return nil }
func (v *mockValidator) Close() {}

0 comments on commit 036dd4d

Please sign in to comment.