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

Commit

Permalink
Schema functions for rollup rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Gromov committed Jul 11, 2017
1 parent 4375598 commit 17389b5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
69 changes: 69 additions & 0 deletions rules/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type rollupRuleSnapshot struct {
cutoverNanos int64
filter filters.Filter
targets []rollupTarget
rawFilters map[string]string
}

func newRollupRuleSnapshot(
Expand Down Expand Up @@ -129,6 +130,7 @@ func newRollupRuleSnapshot(
cutoverNanos: r.CutoverTime,
filter: filter,
targets: targets,
rawFilters: r.TagFilters,
}, nil
}

Expand Down Expand Up @@ -195,6 +197,14 @@ func bytesArrayFromStringArray(values []string) [][]byte {
return result
}

func stringArrayFromBytesArray(values [][]byte) []string {
result := make([]string, len(values))
for i, bytes := range values {
result[i] = string(bytes)
}
return result
}

func bytesArrayCopy(values [][]byte) [][]byte {
result := make([][]byte, len(values))
for i, b := range values {
Expand All @@ -203,3 +213,62 @@ func bytesArrayCopy(values [][]byte) [][]byte {
}
return result
}

func (t rollupTarget) Schema() (*schema.RollupTarget, error) {
res := &schema.RollupTarget{
Name: string(t.Name),
}

policies := make([]*schema.Policy, len(t.Policies))
for i, p := range t.Policies {
policy, err := p.Schema()
if err != nil {
return nil, err
}
policies[i] = policy
}
res.Policies = policies
res.Tags = stringArrayFromBytesArray(t.Tags)

return res, nil
}

// Schema returns the given MappingRuleSnapshot in protobuf form.
func (rrs rollupRuleSnapshot) Schema() (*schema.RollupRuleSnapshot, error) {
res := &schema.RollupRuleSnapshot{
Name: rrs.name,
Tombstoned: rrs.tombstoned,
CutoverTime: rrs.cutoverNanos,
TagFilters: rrs.rawFilters,
}
targets := make([]*schema.RollupTarget, len(rrs.targets))
for i, t := range rrs.targets {
target, err := t.Schema()
if err != nil {
return nil, err
}
targets[i] = target
}
res.Targets = targets

return res, nil
}

// Schema returns the given RollupRule in protobuf form.
func (rc rollupRule) Schema() (*schema.RollupRule, error) {
res := &schema.RollupRule{
Uuid: rc.uuid,
}

snapshots := make([]*schema.RollupRuleSnapshot, len(rc.snapshots))
for i, s := range rc.snapshots {
snapshot, err := s.Schema()
if err != nil {
return nil, err
}
snapshots[i] = snapshot
}
res.Snapshots = snapshots

return res, nil
}
21 changes: 21 additions & 0 deletions rules/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,24 @@ type testRollupTargetData struct {
target rollupTarget
result bool
}

// func TestRollupTargetSchema(t *testing.T) {
// expectedSchema := testRollupRuleSchema.Snapshots[0][0]
// }

func TestRollupRuleSnapshotSchema(t *testing.T) {
expectedSchema := testRollupRuleSchema.Snapshots[0]
rr, err := newRollupRule(testRollupRuleSchema, testTagsFilterOptions())
require.NoError(t, err)
schema, err := rr.snapshots[0].Schema()
require.NoError(t, err)
require.EqualValues(t, expectedSchema, schema)
}

func TestRollupRuleSchema(t *testing.T) {
rr, err := newRollupRule(testRollupRuleSchema, testTagsFilterOptions())
require.NoError(t, err)
schema, err := rr.Schema()
require.NoError(t, err)
require.Equal(t, testRollupRuleSchema, schema)
}

0 comments on commit 17389b5

Please sign in to comment.