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

     1  package identity
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/go-graphite/carbonapi/expr/interfaces"
     7  	"github.com/go-graphite/carbonapi/expr/types"
     8  	"github.com/go-graphite/carbonapi/pkg/parser"
     9  	pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
    10  )
    11  
    12  type identity struct{}
    13  
    14  func GetOrder() interfaces.Order {
    15  	return interfaces.Any
    16  }
    17  
    18  func New(configFile string) []interfaces.FunctionMetadata {
    19  	res := make([]interfaces.FunctionMetadata, 0)
    20  	f := &identity{}
    21  	functions := []string{"identity"}
    22  	for _, n := range functions {
    23  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    24  	}
    25  	return res
    26  }
    27  
    28  // identity(name)
    29  func (f *identity) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    30  	name, err := e.GetStringArg(0)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	step := int64(60)
    36  
    37  	newValues := make([]float64, (until-from-1+step)/step)
    38  	value := from
    39  	for i := 0; i < len(newValues); i++ {
    40  		newValues[i] = float64(value)
    41  		value += step
    42  	}
    43  
    44  	p := types.MetricData{
    45  		FetchResponse: pb.FetchResponse{
    46  			Name:      "identity(" + name + ")",
    47  			StartTime: from,
    48  			StopTime:  until,
    49  			StepTime:  step,
    50  			Values:    newValues,
    51  		},
    52  		Tags: map[string]string{"name": name},
    53  	}
    54  
    55  	return []*types.MetricData{&p}, nil
    56  
    57  }
    58  
    59  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    60  func (f *identity) Description() map[string]types.FunctionDescription {
    61  	return map[string]types.FunctionDescription{
    62  		"identity": {
    63  			Description: "Identity function: Returns datapoints where the value equals the timestamp of the datapoint.\n Useful when you have another series where the value is a timestamp, and you want to compare it to the time of the datapoint, to render an age\n\nExample:\n\n.. code-block:: none\n\n  &target=identity(\"The.time.series\")\n This would create a series named “The.time.series” that contains points where x(t) == t.)",
    64  			Function:    "identity(name)",
    65  			Group:       "Calculate",
    66  			Module:      "graphite.render.functions",
    67  			Name:        "identity",
    68  			Params: []types.FunctionParam{
    69  				{
    70  					Name:     "name",
    71  					Required: true,
    72  					Type:     types.String,
    73  				},
    74  			},
    75  		},
    76  	}
    77  }