diff --git a/CHANGELOG.md b/CHANGELOG.md index 24b1b01e8..51cab5425 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/tick/stateful/functions.go b/tick/stateful/functions.go index e4614ed0f..7de86958b 100644 --- a/tick/stateful/functions.go +++ b/tick/stateful/functions.go @@ -7,6 +7,7 @@ import ( "strconv" "time" + "github.com/dustin/go-humanize" "github.com/influxdata/influxdb/influxql" ) @@ -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 @@ -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 +}