Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
163 lines
2.8 KiB
Go
163 lines
2.8 KiB
Go
package expr
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"math/rand"
|
|
"strconv"
|
|
|
|
"github.com/PaesslerAG/gval"
|
|
"github.com/cortezaproject/corteza/server/pkg/gvalfnc"
|
|
"github.com/spf13/cast"
|
|
)
|
|
|
|
func NumericFunctions() []gval.Language {
|
|
return []gval.Language{
|
|
gval.Function("min", min),
|
|
gval.Function("max", max),
|
|
gval.Function("round", round),
|
|
gval.Function("floor", floor),
|
|
gval.Function("ceil", ceil),
|
|
gval.Function("abs", math.Abs),
|
|
gval.Function("log", math.Log10),
|
|
gval.Function("pow", math.Pow),
|
|
gval.Function("sqrt", math.Sqrt),
|
|
gval.Function("sum", sum),
|
|
gval.Function("average", average),
|
|
gval.Function("random", random),
|
|
gval.Function("int", toInt64),
|
|
gval.Function("float", gvalfnc.CastFloat),
|
|
}
|
|
}
|
|
|
|
func min(aa ...interface{}) (min float64) {
|
|
return findMinMax(-1, aa...)
|
|
}
|
|
|
|
func max(aa ...interface{}) (min float64) {
|
|
return findMinMax(1, aa...)
|
|
}
|
|
|
|
func findMinMax(dir int, aa ...interface{}) (mm float64) {
|
|
var (
|
|
set bool
|
|
flt float64
|
|
)
|
|
for i := range aa {
|
|
switch conv := aa[i].(type) {
|
|
case int:
|
|
flt = float64(conv)
|
|
case int64:
|
|
flt = float64(conv)
|
|
case uint:
|
|
flt = float64(conv)
|
|
case uint64:
|
|
flt = float64(conv)
|
|
case float32:
|
|
flt = float64(conv)
|
|
case float64:
|
|
flt = conv
|
|
default:
|
|
continue
|
|
}
|
|
|
|
if !set {
|
|
set = true
|
|
mm = flt
|
|
} else if dir < 0 {
|
|
mm = math.Min(mm, flt)
|
|
} else if dir > 0 {
|
|
mm = math.Max(mm, flt)
|
|
}
|
|
}
|
|
|
|
return mm
|
|
}
|
|
|
|
func round(f float64, d float64) float64 {
|
|
p := math.Pow(10, d)
|
|
return math.Round(f*p) / p
|
|
}
|
|
|
|
func floor(f float64) float64 {
|
|
return math.Floor(f)
|
|
}
|
|
|
|
func ceil(f float64) float64 {
|
|
return math.Ceil(f)
|
|
}
|
|
|
|
func sum(v ...interface{}) float64 {
|
|
var sum float64
|
|
|
|
for _, vv := range v {
|
|
c, err := CastToFloat(vv)
|
|
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
sum += c
|
|
}
|
|
|
|
return sum
|
|
}
|
|
|
|
func average(v ...interface{}) float64 {
|
|
var i int
|
|
var j, sum float64
|
|
|
|
for ; i < len(v); i++ {
|
|
c, err := CastToFloat(v[i])
|
|
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
j++
|
|
sum += c
|
|
}
|
|
|
|
return sum / j
|
|
}
|
|
|
|
func random(v ...float64) (out float64, err error) {
|
|
var (
|
|
from, to float64
|
|
totalArgs = len(v)
|
|
)
|
|
|
|
if totalArgs == 0 || totalArgs > 2 {
|
|
return 0, fmt.Errorf("expecting 1 or 2 parameter, got %d", totalArgs)
|
|
}
|
|
|
|
if totalArgs > 0 {
|
|
if to = v[0]; to < 0 {
|
|
return 0, fmt.Errorf("unexpected input type of 1st parameter")
|
|
}
|
|
}
|
|
|
|
if totalArgs > 1 {
|
|
if to = v[1]; to < 0 {
|
|
return 0, fmt.Errorf("unexpected input type of 2nd parameter")
|
|
}
|
|
from = v[0]
|
|
}
|
|
|
|
out = from + rand.Float64()*(to-from)
|
|
return
|
|
}
|
|
|
|
func toInt64(aa interface{}) (i int64) {
|
|
// Parse strings as base-10 to avoid cast.ToInt64's octal interpretation of leading zeros
|
|
if str, ok := aa.(string); ok {
|
|
parsed, err := strconv.ParseInt(str, 10, 64)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
return cast.ToInt64(aa)
|
|
}
|