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

Commit

Permalink
Add String() method for various types, move types to their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Jan 11, 2017
1 parent ffa77a9 commit 4381592
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 61 deletions.
16 changes: 16 additions & 0 deletions metric/aggregated/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package aggregated

import (
"fmt"
"time"

"github.com/m3db/m3metrics/metric"
Expand All @@ -34,6 +35,16 @@ type Metric struct {
Value float64
}

// String is the string representation of a metric
func (m Metric) String() string {
return fmt.Sprintf(
"{id:%s,timestamp:%s,value:%f}",
m.ID.String(),
m.Timestamp.String(),
m.Value,
)
}

// ChunkedMetric is a metric with a chunked ID
type ChunkedMetric struct {
metric.ChunkedID
Expand Down Expand Up @@ -69,6 +80,11 @@ type MetricWithPolicy struct {
policy.Policy
}

// String is the string representation of a metric with policy
func (mp MetricWithPolicy) String() string {
return fmt.Sprintf("{metric:%s,policy:%s}", mp.Metric.String(), mp.Policy.String())
}

// ChunkedMetricWithPolicy is a chunked metric with applicable policy
type ChunkedMetricWithPolicy struct {
ChunkedMetric
Expand Down
32 changes: 32 additions & 0 deletions metric/unaggregated/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package unaggregated

import (
"fmt"

"github.com/m3db/m3metrics/metric"
"github.com/m3db/m3metrics/policy"
)
Expand All @@ -36,6 +38,19 @@ const (
GaugeType
)

func (t Type) String() string {
switch t {
case CounterType:
return "counter"
case BatchTimerType:
return "batchTimer"
case GaugeType:
return "gauge"
default:
return "unknown"
}
}

// Counter is a counter containing the counter ID and the counter value
type Counter struct {
ID metric.ID
Expand Down Expand Up @@ -87,6 +102,23 @@ type MetricUnion struct {

var emptyMetricUnion MetricUnion

// String is the string representation of a metric union
func (m *MetricUnion) String() string {
switch m.Type {
case CounterType:
return fmt.Sprintf("{type:%s,id:%s,value:%d}", m.Type, m.ID.String(), m.CounterVal)
case BatchTimerType:
return fmt.Sprintf("{type:%s,id:%s,value:%v}", m.Type, m.ID.String(), m.BatchTimerVal)
case GaugeType:
return fmt.Sprintf("{type:%s,id:%s,value:%f}", m.Type, m.ID.String(), m.GaugeVal)
default:
return fmt.Sprintf(
"{type:%d,id:%s,counterVal:%d,batchTimerVal:%v,gaugeVal:%f}",
m.Type, m.ID.String(), m.CounterVal, m.BatchTimerVal, m.GaugeVal,
)
}
}

// Reset resets the metric union
func (m *MetricUnion) Reset() { *m = emptyMetricUnion }

Expand Down
43 changes: 43 additions & 0 deletions policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package policy

import (
"bytes"
"fmt"
"time"

"github.com/m3db/m3x/time"
Expand Down Expand Up @@ -53,6 +55,47 @@ var (
}
)

// Policy represents the resolution and retention period metric datapoints
// are stored at
type Policy struct {
// Resolution is the resolution datapoints are stored at
Resolution Resolution

// Retention is the period datatpoints are retained for
Retention Retention
}

// String is the string representation of a policy
func (p Policy) String() string {
return fmt.Sprintf("{resolution:%s,retention:%s}", p.Resolution.String(), p.Retention.String())
}

// VersionedPolicies represent a list of policies at a specified version
type VersionedPolicies struct {
// Version is the version of the policies
Version int

// Cutover is when the policies take effect
Cutover time.Time

// Policies represent the list of policies
Policies []Policy
}

// String is the representation of versioned policies
func (vp VersionedPolicies) String() string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("{version:%d,cutover:%s,policies:[", vp.Version, vp.Cutover.String()))
for i := range vp.Policies {
buf.WriteString(vp.Policies[i].String())
if i < len(vp.Policies)-1 {
buf.WriteString(",")
}
}
buf.WriteString("]}")
return buf.String()
}

// Reset resets the versioned policies
func (vp *VersionedPolicies) Reset() {
*vp = emptyVersionedPolicies
Expand Down
15 changes: 15 additions & 0 deletions policy/resolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,26 @@ package policy

import (
"errors"
"fmt"
"time"

"github.com/m3db/m3x/time"
)

// Resolution is the sampling resolution for datapoints
type Resolution struct {
// Window is the bucket size represented by the resolution
Window time.Duration

// Precision is the precision of datapoints stored at this resoluion
Precision xtime.Unit
}

// String is the string representation of a resolution
func (r Resolution) String() string {
return fmt.Sprintf("%s@1%s", r.Window.String(), r.Precision.String())
}

// ResolutionValue is the resolution value
type ResolutionValue int

Expand Down
8 changes: 8 additions & 0 deletions policy/retention.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ import (
"time"
)

// Retention is the retention period for datapoints
type Retention time.Duration

// String is the string representation of a retention period
func (r Retention) String() string {
return time.Duration(r).String()
}

// RetentionValue is the retention value
type RetentionValue int

Expand Down
61 changes: 0 additions & 61 deletions policy/types.go

This file was deleted.

0 comments on commit 4381592

Please sign in to comment.