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

Commit

Permalink
Addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Gromov committed Jul 12, 2017
1 parent 777693f commit 88479c4
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 114 deletions.
44 changes: 22 additions & 22 deletions rules/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ func newMappingRuleSnapshot(
}, nil
}

// Schema returns the given MappingRuleSnapshot in protobuf form.
func (mrs mappingRuleSnapshot) Schema() (*schema.MappingRuleSnapshot, error) {
res := &schema.MappingRuleSnapshot{
Name: mrs.name,
Tombstoned: mrs.tombstoned,
CutoverTime: mrs.cutoverNanos,
TagFilters: mrs.rawFilters,
}

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

return res, nil
}

// mappingRule stores mapping rule snapshots.
type mappingRule struct {
uuid string
Expand Down Expand Up @@ -126,28 +148,6 @@ func (mc *mappingRule) activeIndex(timeNanos int64) int {
return idx
}

// Schema returns the given MappingRuleSnapshot in protobuf form.
func (mrs mappingRuleSnapshot) Schema() (*schema.MappingRuleSnapshot, error) {
res := &schema.MappingRuleSnapshot{
Name: mrs.name,
Tombstoned: mrs.tombstoned,
CutoverTime: mrs.cutoverNanos,
TagFilters: mrs.rawFilters,
}

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

return res, nil
}

// Schema returns the given MappingRule in protobuf form.
func (mc mappingRule) Schema() (*schema.MappingRule, error) {
res := &schema.MappingRule{
Expand Down
61 changes: 29 additions & 32 deletions rules/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ var (
errNilNamespaceSnapshotSchema = errors.New("nil namespace snapshot schema")
errNilNamespaceSchema = errors.New("nil namespace schema")
errNilNamespacesSchema = errors.New("nil namespaces schema")

errNilNamespace = errors.New("nil namespace")
errNilNamespaceSnapshot = errors.New("nil namespace snapshot")
errNilNamespace = errors.New("nil namespace")
errNilNamespaceSnapshot = errors.New("nil namespace snapshot")
)

// NamespaceSnapshot defines a namespace snapshot for which rules are defined.
Expand All @@ -61,6 +60,14 @@ func (s NamespaceSnapshot) ForRuleSetVersion() int { return s.forRuleSetVersion
// Tombstoned determines whether the namespace has been tombstoned.
func (s NamespaceSnapshot) Tombstoned() bool { return s.tombstoned }

// Schema returns the given Namespace in protobuf form
func (s NamespaceSnapshot) Schema() *schema.NamespaceSnapshot {
return &schema.NamespaceSnapshot{
ForRulesetVersion: int32(s.forRuleSetVersion),
Tombstoned: s.tombstoned,
}
}

// Namespace stores namespace snapshots.
type Namespace struct {
name []byte
Expand Down Expand Up @@ -92,6 +99,25 @@ func (n Namespace) Name() []byte { return n.name }
// Snapshots return the namespace snapshots.
func (n Namespace) Snapshots() []NamespaceSnapshot { return n.snapshots }

// Schema returns the given Namespace in protobuf form
func (n Namespace) Schema() (*schema.Namespace, error) {
if n.snapshots == nil {
return nil, errNilNamespaceSnapshot
}

res := &schema.Namespace{
Name: string(n.name),
}

snapshots := make([]*schema.NamespaceSnapshot, len(n.snapshots))
for i, s := range n.snapshots {
snapshots[i] = s.Schema()
}
res.Snapshots = snapshots

return res, nil
}

// Namespaces store the list of namespaces for which rules are defined.
type Namespaces struct {
version int
Expand Down Expand Up @@ -123,35 +149,6 @@ func (nss Namespaces) Version() int { return nss.version }
// Namespaces returns the list of namespaces.
func (nss Namespaces) Namespaces() []Namespace { return nss.namespaces }

// Schema returns the given Namespace in protobuf form
func (s NamespaceSnapshot) Schema() *schema.NamespaceSnapshot {
res := &schema.NamespaceSnapshot{
ForRulesetVersion: int32(s.forRuleSetVersion),
Tombstoned: s.tombstoned,
}

return res
}

// Schema returns the given Namespace in protobuf form
func (n Namespace) Schema() (*schema.Namespace, error) {
if n.snapshots == nil {
return nil, errNilNamespaceSnapshot
}

res := &schema.Namespace{
Name: string(n.name),
}

snapshots := make([]*schema.NamespaceSnapshot, len(n.snapshots))
for i, s := range n.snapshots {
snapshots[i] = s.Schema()
}
res.Snapshots = snapshots

return res, nil
}

// Schema returns the given Namespaces slice in protobuf form.
func (nss Namespaces) Schema() (*schema.Namespaces, error) {
res := &schema.Namespaces{}
Expand Down
120 changes: 60 additions & 60 deletions rules/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ func (t *rollupTarget) clone() rollupTarget {
}
}

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
}

// rollupRuleSnapshot defines a rule snapshot such that if a metric matches the
// provided filters, it is rolled up using the provided list of rollup targets.
type rollupRuleSnapshot struct {
Expand Down Expand Up @@ -134,6 +153,28 @@ func newRollupRuleSnapshot(
}, 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
}

// rollupRule stores rollup rule snapshots.
type rollupRule struct {
uuid string
Expand Down Expand Up @@ -189,6 +230,25 @@ func (rc *rollupRule) activeIndex(timeNanos int64) int {
return idx
}

// 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
}

func bytesArrayFromStringArray(values []string) [][]byte {
result := make([][]byte, len(values))
for i, str := range values {
Expand All @@ -213,63 +273,3 @@ 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
}

0 comments on commit 88479c4

Please sign in to comment.