Skip to content

Commit

Permalink
MAKE-986: add document marshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
tychoish committed Jan 6, 2020
1 parent ace4001 commit bf56838
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions events/performance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/evergreen-ci/birch"
"github.com/pkg/errors"
)

// Performance represents a single raw event in a metrics collection system for
Expand Down Expand Up @@ -85,6 +86,83 @@ func (p *Performance) MarshalDocument() (*birch.Document, error) {
), nil
}

func (p *Performance) UnmarshalDocument(doc *birch.Document) error {
iter := doc.Iterator()
for iter.Next() {
elem := iter.Element()
switch elem.Key() {
case "ts":
p.Timestamp = elem.Value().Time()
case "id":
case "counters":
if err := p.Counters.UnmarshalDocument(elem.Value().MutableDocument()); err != nil {
return errors.WithStack(err)
}
case "timers":
if err := p.Timers.UnmarshalDocument(elem.Value().MutableDocument()); err != nil {
return errors.WithStack(err)
}
case "gauges":
if err := p.Gauges.UnmarshalDocument(elem.Value().MutableDocument()); err != nil {
return errors.WithStack(err)
}
}
}

return errors.WithStack(iter.Err())
}

func (p *PerformanceCounters) UnmarshalDocument(doc *birch.Document) error {
iter := doc.Iterator()
for iter.Next() {
elem := iter.Element()
switch elem.Key() {
case "n":
p.Number = elem.Value().Int64()
case "opts":
p.Operations = elem.Value().Int64()
case "size":
p.Size = elem.Value().Int64()
case "errors":
p.Errors = elem.Value().Int64()
}
}

return errors.WithStack(iter.Err())
}

func (p *PerformanceTimers) UnmarshalDocument(doc *birch.Document) error {
iter := doc.Iterator()
for iter.Next() {
elem := iter.Element()
switch elem.Key() {
case "dur":
p.Duration = time.Duration(elem.Value().Int64())
case "total":
p.Total = time.Duration(elem.Value().Int64())
}
}

return errors.WithStack(iter.Err())
}

func (p *PerformanceGauges) UnmarshalDocument(doc *birch.Document) error {
iter := doc.Iterator()
for iter.Next() {
elem := iter.Element()
switch elem.Key() {
case "state":
p.State = elem.Value().Int64()
case "workers":
p.Workers = elem.Value().Int64()
case "failed":
p.Failed = elem.Value().Boolean()
}
}

return errors.WithStack(iter.Err())
}

// Add combines the values of the input Performance struct into this struct,
// logically, overriding the Gauges values as well as the timestamp and ID
// ID values, while summing the Counters and Timers values.
Expand Down

0 comments on commit bf56838

Please sign in to comment.