diff --git a/rules/rollup.go b/rules/rollup.go index 4af507c..5bb1f96 100644 --- a/rules/rollup.go +++ b/rules/rollup.go @@ -102,6 +102,7 @@ type rollupRuleSnapshot struct { cutoverNanos int64 filter filters.Filter targets []rollupTarget + rawFilters map[string]string } func newRollupRuleSnapshot( @@ -129,6 +130,7 @@ func newRollupRuleSnapshot( cutoverNanos: r.CutoverTime, filter: filter, targets: targets, + rawFilters: r.TagFilters, }, nil } @@ -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 { @@ -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 +} diff --git a/rules/rollup_test.go b/rules/rollup_test.go index f7a1ef9..925ef7d 100644 --- a/rules/rollup_test.go +++ b/rules/rollup_test.go @@ -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) +}