Skip to content

Commit

Permalink
added custom counters
Browse files Browse the repository at this point in the history
  • Loading branch information
awitas committed Apr 4, 2023
1 parent 7e61efc commit c8a6501
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import "sync/atomic"

//Counter represents a counter
type Counter struct {
Count int64 `json:",omitempty"`
Count int64 `json:",omitempty"`
Custom CustomCounter
}

//CountValue returns count
Expand Down
7 changes: 6 additions & 1 deletion counter/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ func (c *MultiCounter) incrementValueBy(value interface{}, count, i int64) int64
}
if _, ok := value.(string); !ok {
c.locker.Lock()
c.Counters[idx].Value = toolbox.AsString(value)
if custom, ok := value.(CustomCounter); ok {
c.Counters[idx].Custom.Aggregate(custom)
value = c.Counters[idx].Custom
} else {
c.Counters[idx].Value = toolbox.AsString(value)
}
c.locker.Unlock()
}

Expand Down
9 changes: 9 additions & 0 deletions counter/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,20 @@ func NewOperation(timeUnit time.Duration, provider Provider) *Operation {
locker: &sync.Mutex{},
},
}
customProvider, _ := provider.(CustomProvider)
if provider != nil {
op.Counters = make([]*Value, len(provider.Keys()))
for i, val := range provider.Keys() {
var customCounter CustomCounter
if customProvider != nil {
customCounter = customProvider.NewCounter()
}
op.Counters[i] = &Value{
Value: val,
Counter: Counter{
Count: 0,
Custom: customCounter,
},
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions counter/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ type Provider interface {
//Keys returns mapped keys
Keys() []string
}

type CustomCounter interface {
Aggregate(value interface{})
}

type CustomProvider interface {
NewCounter() CustomCounter
}

0 comments on commit c8a6501

Please sign in to comment.