Skip to content

Commit

Permalink
feature: add size for humanize byte size
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvisa committed Jul 4, 2016
1 parent 1803d58 commit 7bd32bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [#46](https://github.com/influxdata/kapacitor/issue/46): Can now create combinations of points within the same stream.
This is kind of like join but instead joining a stream with itself.

- [#669](https://github.com/influxdata/kapacitor/pull/669): Add size function for humanize byte size.

### Bugfixes

Expand Down
26 changes: 26 additions & 0 deletions tick/stateful/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"github.com/dustin/go-humanize"
"github.com/influxdata/influxdb/influxql"
)

Expand Down Expand Up @@ -83,6 +84,9 @@ func init() {
statelessFuncs["day"] = &day{}
statelessFuncs["month"] = &month{}
statelessFuncs["year"] = &year{}

// Humanize functions
statelessFuncs["humanBytes"] = &humanBytes{}
}

// Return set of built-in Funcs
Expand Down Expand Up @@ -598,3 +602,25 @@ func (*year) Call(args ...interface{}) (v interface{}, err error) {
}
return
}

type humanBytes struct {
}

func (*humanBytes) Reset() {

}

func (*humanBytes) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 1 {
return 0, errors.New("humanBytes expects exactly one argument")
}
switch a := args[0].(type) {
case float64:
v = humanize.Bytes(uint64(a))
case int64:
v = humanize.Bytes(uint64(a))
default:
err = fmt.Errorf("cannot convert %T to humanBytes", a)
}
return
}

0 comments on commit 7bd32bb

Please sign in to comment.