github.com/go-graphite/carbonapi@v0.17.0/expr/functions/constantLine/function.go (about)

     1  package constantLine
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  
     7  	"github.com/go-graphite/carbonapi/expr/interfaces"
     8  	"github.com/go-graphite/carbonapi/expr/types"
     9  	"github.com/go-graphite/carbonapi/pkg/parser"
    10  	pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
    11  )
    12  
    13  type constantLine 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 := &constantLine{}
    22  	functions := []string{"constantLine"}
    23  	for _, n := range functions {
    24  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    25  	}
    26  	return res
    27  }
    28  
    29  func (f *constantLine) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    30  	value, err := e.GetFloatArg(0)
    31  
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	newValues := []float64{value, value}
    36  	stepTime := until - from
    37  	stopTime := from + stepTime*int64(len(newValues))
    38  	name := strconv.FormatFloat(value, 'g', -1, 64)
    39  	p := &types.MetricData{
    40  		FetchResponse: pb.FetchResponse{
    41  			Name:      name,
    42  			StartTime: from,
    43  			StopTime:  stopTime,
    44  			StepTime:  stepTime,
    45  			Values:    newValues,
    46  		},
    47  		Tags: map[string]string{"name": name},
    48  	}
    49  
    50  	return []*types.MetricData{p}, nil
    51  }
    52  
    53  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    54  func (f *constantLine) Description() map[string]types.FunctionDescription {
    55  	return map[string]types.FunctionDescription{
    56  		"constantLine": {
    57  			Description: "Takes a float F.\n\nDraws a horizontal line at value F across the graph.\n\nExample:\n\n.. code-block:: none\n\n  &target=constantLine(123.456)",
    58  			Function:    "constantLine(value)",
    59  			Group:       "Special",
    60  			Module:      "graphite.render.functions",
    61  			Name:        "constantLine",
    62  			Params: []types.FunctionParam{
    63  				{
    64  					Name:     "value",
    65  					Required: true,
    66  					Type:     types.Float,
    67  				},
    68  			},
    69  		},
    70  	}
    71  }