github.com/go-graphite/carbonapi@v0.17.0/expr/functions/integral/function.go (about) 1 package integral 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 integral 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 := &integral{} 22 functions := []string{"integral"} 23 for _, n := range functions { 24 res = append(res, interfaces.FunctionMetadata{Name: n, F: f}) 25 } 26 return res 27 } 28 29 // integral(seriesList) 30 func (f *integral) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) { 31 return helper.ForEachSeriesDo(ctx, eval, e, from, until, values, func(a *types.MetricData, r *types.MetricData) *types.MetricData { 32 current := 0.0 33 for i, v := range a.Values { 34 if math.IsNaN(v) { 35 r.Values[i] = math.NaN() 36 continue 37 } 38 current += v 39 r.Values[i] = current 40 } 41 r.Tags["integral"] = "1" 42 return r 43 }) 44 } 45 46 // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web 47 func (f *integral) Description() map[string]types.FunctionDescription { 48 return map[string]types.FunctionDescription{ 49 "integral": { 50 Description: "This will show the sum over time, sort of like a continuous addition function.\nUseful for finding totals or trends in metrics that are collected per minute.\n\nExample:\n\n.. code-block:: none\n\n &target=integral(company.sales.perMinute)\n\nThis would start at zero on the left side of the graph, adding the sales each\nminute, and show the total sales for the time period selected at the right\nside, (time now, or the time specified by '&until=').", 51 Function: "integral(seriesList)", 52 Group: "Transform", 53 Module: "graphite.render.functions", 54 Name: "integral", 55 Params: []types.FunctionParam{ 56 { 57 Name: "seriesList", 58 Required: true, 59 Type: types.SeriesList, 60 }, 61 }, 62 NameChange: true, // name changed 63 ValuesChange: true, // values changed 64 }, 65 } 66 }