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

Commit

Permalink
Merge c3eced2 into b2891d3
Browse files Browse the repository at this point in the history
  • Loading branch information
dgromov committed Sep 12, 2017
2 parents b2891d3 + c3eced2 commit 8c9c654
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 392 deletions.
4 changes: 4 additions & 0 deletions matcher/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,13 @@ type mockRuleSet struct {
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 { return nil }
func (r mockRuleSet) RollupRules() rules.RollupRules { return nil }

func testRuleSet() (kv.Store, Cache, *ruleSet) {
store := mem.NewStore()
Expand Down
90 changes: 38 additions & 52 deletions rules/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,6 @@ func (mrs *mappingRuleSnapshot) clone() mappingRuleSnapshot {
}
}

type mappingRuleSnapshotJSON struct {
Name string `json:"name"`
Tombstoned bool `json:"tombstoned"`
CutoverNanos int64 `json:"cutoverNanos"`
TagFilters map[string]string `json:"filters"`
Policies []policy.Policy `json:"policies"`
}

func newMappingRuleSnapshotJSON(mrs mappingRuleSnapshot) mappingRuleSnapshotJSON {
return mappingRuleSnapshotJSON{
Name: mrs.name,
Tombstoned: mrs.tombstoned,
CutoverNanos: mrs.cutoverNanos,
TagFilters: mrs.rawFilters,
Policies: mrs.policies,
}
}

func (mrsj mappingRuleSnapshotJSON) mappingRuleSnapshot() *mappingRuleSnapshot {
return newMappingRuleSnapshotFromFields(
mrsj.Name,
mrsj.Tombstoned,
mrsj.CutoverNanos,
mrsj.TagFilters,
mrsj.Policies,
nil,
)
}

// Schema returns the given MappingRuleSnapshot in protobuf form.
func (mrs *mappingRuleSnapshot) Schema() (*schema.MappingRuleSnapshot, error) {
res := &schema.MappingRuleSnapshot{
Expand All @@ -160,6 +131,40 @@ func (mrs *mappingRuleSnapshot) Schema() (*schema.MappingRuleSnapshot, error) {
return res, nil
}

// MappingRuleView is a human friendly representation of a mapping rule at a given point in time.
type MappingRuleView struct {
id string
name string
cutoverNanos int64
tagFilters map[string]string
policies []policy.Policy
}

func newMappingRuleView(uuid string, mrs mappingRuleSnapshot) MappingRuleView {
return MappingRuleView{
id: uuid,
name: mrs.name,
cutoverNanos: mrs.cutoverNanos,
tagFilters: mrs.rawFilters,
policies: mrs.policies,
}
}

// ID returns the ID in a mapping rule view
func (mrv MappingRuleView) ID() string { return mrv.id }

// Name returns the name in a mapping rule view
func (mrv MappingRuleView) Name() string { return mrv.name }

// CutoverNanos returnes the cutoverNanos in a mapping rule view
func (mrv MappingRuleView) CutoverNanos() int64 { return mrv.cutoverNanos }

// Filters returnes the filters in a mapping rule view
func (mrv MappingRuleView) Filters() map[string]string { return mrv.tagFilters }

// Policies returnes the policies in a mapping rule view
func (mrv MappingRuleView) Policies() []policy.Policy { return mrv.policies }

// mappingRule stores mapping rule snapshots.
type mappingRule struct {
uuid string
Expand Down Expand Up @@ -311,31 +316,12 @@ func (mc *mappingRule) activeIndex(timeNanos int64) int {
return idx
}

type mappingRuleJSON struct {
UUID string `json:"uuid"`
Snapshots []mappingRuleSnapshotJSON `json:"snapshots"`
}

func newMappingRuleJSON(mc mappingRule) mappingRuleJSON {
snapshots := make([]mappingRuleSnapshotJSON, len(mc.snapshots))
func (mc *mappingRule) history() []MappingRuleView {
views := make([]MappingRuleView, len(mc.snapshots))
for i, s := range mc.snapshots {
snapshots[i] = newMappingRuleSnapshotJSON(*s)
}
return mappingRuleJSON{
UUID: mc.uuid,
Snapshots: snapshots,
}
}

func (mrj mappingRuleJSON) mappingRule() mappingRule {
snapshots := make([]*mappingRuleSnapshot, len(mrj.Snapshots))
for i, s := range mrj.Snapshots {
snapshots[i] = s.mappingRuleSnapshot()
}
return mappingRule{
uuid: mrj.UUID,
snapshots: snapshots,
views[i] = newMappingRuleView(mc.uuid, *s)
}
return views
}

// Schema returns the given MappingRule in protobuf form.
Expand Down
131 changes: 61 additions & 70 deletions rules/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,29 @@ func NewRollupTargetFromFields(name string, tags []string, policies []policy.Pol
return RollupTarget{Name: []byte(name), Tags: bytesArrayFromStringArray(tags), Policies: policies}
}

type rollupTargetJSON struct {
Name string `json:"name"`
Tags []string `json:"tags"`
Policies []policy.Policy `json:"policies"`
// 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
}

func newRollupTargetJSON(rt RollupTarget) rollupTargetJSON {
return rollupTargetJSON{Name: string(rt.Name), Tags: stringArrayFromBytesArray(rt.Tags), Policies: rt.Policies}
func newRollupTargetView(target RollupTarget) RollupTargetView {
return RollupTargetView{
name: string(target.Name),
tags: stringArrayFromBytesArray(target.Tags),
policies: target.Policies,
}
}

func (tj rollupTargetJSON) rollupTarget() RollupTarget {
return NewRollupTargetFromFields(tj.Name, tj.Tags, tj.Policies)
}
// Name returnes the name in a rollup target view.
func (tv RollupTargetView) Name() string { return tv.name }

// Tags returnes the tags in a rollup target view.
func (tv RollupTargetView) Tags() []string { return tv.tags }

// Policies returnes the policies in a rollup target view.
func (tv RollupTargetView) Policies() []policy.Policy { return tv.policies }

// sameTransform determines whether two targets have the same transformation.
func (t *RollupTarget) sameTransform(other RollupTarget) bool {
Expand Down Expand Up @@ -116,45 +126,6 @@ func (t *RollupTarget) clone() RollupTarget {
}
}

type rollupRuleSnapshotJSON struct {
Name string `json:"name"`
Tombstoned bool `json:"tombstoned"`
CutoverNanos int64 `json:"cutoverNanos"`
TagFilters map[string]string `json:"filters"`
Targets []rollupTargetJSON `json:"targets"`
}

func newRollupRuleSnapshotJSON(rrs rollupRuleSnapshot) rollupRuleSnapshotJSON {
targets := make([]rollupTargetJSON, len(rrs.targets))
for i, t := range rrs.targets {
targets[i] = newRollupTargetJSON(t)
}

return rollupRuleSnapshotJSON{
Name: rrs.name,
Tombstoned: rrs.tombstoned,
CutoverNanos: rrs.cutoverNanos,
TagFilters: rrs.rawFilters,
Targets: targets,
}
}

func (rrsj rollupRuleSnapshotJSON) rollupRuleSnapshot() *rollupRuleSnapshot {
targets := make([]RollupTarget, len(rrsj.Targets))
for i, t := range rrsj.Targets {
targets[i] = t.rollupTarget()
}

return newRollupRuleSnapshotFromFields(
rrsj.Name,
rrsj.Tombstoned,
rrsj.CutoverNanos,
rrsj.TagFilters,
targets,
nil,
)
}

// Schema returns the schema representation of a rollup target.
func (t *RollupTarget) Schema() (*schema.RollupTarget, error) {
res := &schema.RollupTarget{
Expand Down Expand Up @@ -276,6 +247,44 @@ func (rrs *rollupRuleSnapshot) Schema() (*schema.RollupRuleSnapshot, error) {
return res, nil
}

// RollupRuleView is a human friendly representation of a rollup rule at a given point in time.
type RollupRuleView struct {
id string
name string
cutoverNanos int64
filters map[string]string
targets []RollupTargetView
}

func newRollupRuleView(uuid string, rrs rollupRuleSnapshot) RollupRuleView {
targets := make([]RollupTargetView, len(rrs.targets))
for i, t := range rrs.targets {
targets[i] = newRollupTargetView(t)
}
return RollupRuleView{
id: uuid,
name: rrs.name,
cutoverNanos: rrs.cutoverNanos,
filters: rrs.rawFilters,
targets: targets,
}
}

// ID returns the ID in a rollup rule view
func (rrv RollupRuleView) ID() string { return rrv.id }

// Name returns the name in a rollup rule view
func (rrv RollupRuleView) Name() string { return rrv.name }

// CutoverNanos returnes the cutoverNanos in a rollup rule view
func (rrv RollupRuleView) CutoverNanos() int64 { return rrv.cutoverNanos }

// Filters returnes the filters in a rollup rule view
func (rrv RollupRuleView) Filters() map[string]string { return rrv.filters }

// Targets returnes the policies in a rollup rule view
func (rrv RollupRuleView) Targets() []RollupTargetView { return rrv.targets }

// rollupRule stores rollup rule snapshots.
type rollupRule struct {
uuid string
Expand Down Expand Up @@ -429,31 +438,13 @@ func (rc *rollupRule) revive(
return rc.addSnapshot(name, rawFilters, targets, cutoverTime)
}

type rollupRuleJSON struct {
UUID string `json:"uuid"`
Snapshots []rollupRuleSnapshotJSON `json:"snapshots"`
}

func newRollupRuleJSON(rc rollupRule) rollupRuleJSON {
snapshots := make([]rollupRuleSnapshotJSON, len(rc.snapshots))
func (rc *rollupRule) history() []RollupRuleView {
views := make([]RollupRuleView, len(rc.snapshots))
for i, s := range rc.snapshots {
snapshots[i] = newRollupRuleSnapshotJSON(*s)
}
return rollupRuleJSON{
UUID: rc.uuid,
Snapshots: snapshots,
views[i] = newRollupRuleView(rc.uuid, *s)
}
}

func (rrj rollupRuleJSON) rollupRule() rollupRule {
snapshots := make([]*rollupRuleSnapshot, len(rrj.Snapshots))
for i, s := range rrj.Snapshots {
snapshots[i] = s.rollupRuleSnapshot()
}
return rollupRule{
uuid: rrj.UUID,
snapshots: snapshots,
}
return views
}

// Schema returns the given RollupRule in protobuf form.
Expand Down
Loading

0 comments on commit 8c9c654

Please sign in to comment.