Skip to content

Commit

Permalink
fix: change to hour per discussion with Kang
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Stockton committed Sep 25, 2024
1 parent a02b4aa commit 00c0439
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions internal/conf/rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ import (
"time"
)

const defaultOverTime = time.Hour

type Rate struct {
Events float64 `json:"events,omitempty"`
OverTime time.Duration `json:"over_time,omitempty"`
}

func (r *Rate) EventsPerSecond() float64 {
if int64(r.OverTime) == 0 {
return r.Events
d := r.OverTime
if d == 0 {
d = defaultOverTime
}

return r.Events / r.OverTime.Seconds()
return r.Events / d.Seconds()
}

// Decode is used by envconfig to parse the env-config string to a Rate value.
func (r *Rate) Decode(value string) error {
if f, err := strconv.ParseFloat(value, 64); err == nil {
r.Events = f
r.OverTime = time.Second
r.OverTime = defaultOverTime
return nil
}
parts := strings.Split(value, "/")
Expand Down
4 changes: 2 additions & 2 deletions internal/conf/rate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func TestRateDecode(t *testing.T) {
exp Rate
err string
}{
{str: "100", eps: 100, exp: Rate{Events: 100, OverTime: time.Second}},
{str: "100.0", eps: 100, exp: Rate{Events: 100, OverTime: time.Second}},
{str: "1800", eps: 0.5, exp: Rate{Events: 1800, OverTime: time.Hour}},
{str: "1800.0", eps: 0.5, exp: Rate{Events: 1800, OverTime: time.Hour}},
{str: "3600/1h", eps: 1, exp: Rate{Events: 3600, OverTime: time.Hour}},
{str: "100/24h",
eps: 0.0011574074074074073,
Expand Down

0 comments on commit 00c0439

Please sign in to comment.