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 Jun 23, 2016
1 parent 82755a1 commit 34e86f0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Features

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

### Bugfixes

- [#656](https://github.com/influxdata/kapacitor/pull/656): Fix issues where an expression could not be passed as a function parameter in TICKscript.
Expand Down
28 changes: 28 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["size"] = &size{}
}

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

type size struct {
}

func (*size) Reset() {

}

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

0 comments on commit 34e86f0

Please sign in to comment.