github.com/go-graphite/carbonapi@v0.17.0/expr/functions/isNotNull/function.go (about) 1 package isNotNull 2 3 import ( 4 "context" 5 "math" 6 7 "github.com/go-graphite/carbonapi/expr/helper" 8 "github.com/go-graphite/carbonapi/expr/interfaces" 9 "github.com/go-graphite/carbonapi/expr/types" 10 "github.com/go-graphite/carbonapi/pkg/parser" 11 ) 12 13 type isNotNull struct{} 14 15 func GetOrder() interfaces.Order { 16 return interfaces.Any 17 } 18 19 func New(configFile string) []interfaces.FunctionMetadata { 20 res := make([]interfaces.FunctionMetadata, 0) 21 f := &isNotNull{} 22 functions := []string{"isNotNull", "isNonNull"} 23 for _, n := range functions { 24 res = append(res, interfaces.FunctionMetadata{Name: n, F: f}) 25 } 26 return res 27 } 28 29 // isNonNull(seriesList) 30 // alias: isNotNull(seriesList) 31 func (f *isNotNull) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) { 32 e.SetTarget("isNonNull") 33 34 return helper.ForEachSeriesDo(ctx, eval, e, from, until, values, func(a *types.MetricData, r *types.MetricData) *types.MetricData { 35 for i, v := range a.Values { 36 if math.IsNaN(v) { 37 r.Values[i] = 0 38 } else { 39 r.Values[i] = 1 40 } 41 42 } 43 r.Tags["isNonNull"] = "1" 44 return r 45 }) 46 } 47 48 // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web 49 func (f *isNotNull) Description() map[string]types.FunctionDescription { 50 return map[string]types.FunctionDescription{ 51 "isNotNull": { 52 Description: "Takes a metric or wildcard seriesList and counts up the number of non-null\nvalues. This is useful for understanding the number of metrics that have data\nat a given point in time (i.e. to count which servers are alive).\n\nExample:\n\n.. code-block:: none\n\n &target=isNotNull(webapp.pages.*.views)\n\nReturns a seriesList where 1 is specified for non-null values, and\n0 is specified for null values.", 53 Function: "isNotNull(seriesList)", 54 Group: "Combine", 55 Module: "graphite.render.functions", 56 Name: "isNotNull", 57 Params: []types.FunctionParam{ 58 { 59 Name: "seriesList", 60 Required: true, 61 Type: types.SeriesList, 62 }, 63 }, 64 NameChange: true, // name changed 65 ValuesChange: true, // values changed 66 }, 67 "isNonNull": { 68 Description: "Takes a metric or wildcard seriesList and counts up the number of non-null\nvalues. This is useful for understanding the number of metrics that have data\nat a given point in time (i.e. to count which servers are alive).\n\nExample:\n\n.. code-block:: none\n\n &target=isNonNull(webapp.pages.*.views)\n\nReturns a seriesList where 1 is specified for non-null values, and\n0 is specified for null values.", 69 Function: "isNonNull(seriesList)", 70 Group: "Combine", 71 Module: "graphite.render.functions", 72 Name: "isNonNull", 73 Params: []types.FunctionParam{ 74 { 75 Name: "seriesList", 76 Required: true, 77 Type: types.SeriesList, 78 }, 79 }, 80 NameChange: true, // name changed 81 ValuesChange: true, // values changed 82 }, 83 } 84 }