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

     1  //go:build cairo
     2  // +build cairo
     3  
     4  package verticalLine
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"time"
    10  
    11  	"github.com/go-graphite/carbonapi/expr/interfaces"
    12  	"github.com/go-graphite/carbonapi/expr/tags"
    13  	"github.com/go-graphite/carbonapi/expr/types"
    14  	"github.com/go-graphite/carbonapi/pkg/parser"
    15  	pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
    16  )
    17  
    18  var TsOutOfRangeError = fmt.Errorf("timestamp out of range")
    19  
    20  type verticalLine struct{}
    21  
    22  func GetOrder() interfaces.Order {
    23  	return interfaces.Any
    24  }
    25  
    26  func New(_ string) []interfaces.FunctionMetadata {
    27  	res := make([]interfaces.FunctionMetadata, 0)
    28  
    29  	f := &verticalLine{}
    30  	functions := []string{"verticalLine"}
    31  	for _, n := range functions {
    32  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    33  	}
    34  
    35  	return res
    36  }
    37  
    38  func (f *verticalLine) Do(_ context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, _ map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    39  	start, err := e.GetIntervalArg(0, -1)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	ts := until + int64(start)
    45  
    46  	if ts < from {
    47  		return nil, fmt.Errorf("ts %s is before start %s: %w", time.Unix(ts, 0), time.Unix(from, 0), TsOutOfRangeError)
    48  	} else if ts > until {
    49  		return nil, fmt.Errorf("ts %s is after end %s: %w", time.Unix(ts, 0), time.Unix(until, 0), TsOutOfRangeError)
    50  	}
    51  
    52  	label, err := e.GetStringArgDefault(1, "")
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	color, err := e.GetStringArgDefault(2, "")
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	md := &types.MetricData{
    63  		FetchResponse: pb.FetchResponse{
    64  			Name:      label,
    65  			Values:    []float64{1.0, 1.0},
    66  			StartTime: ts,
    67  			StepTime:  1,
    68  			StopTime:  ts,
    69  		},
    70  		Tags: tags.ExtractTags(label),
    71  		GraphOptions: types.GraphOptions{
    72  			DrawAsInfinite: true,
    73  			Color:          color,
    74  		},
    75  	}
    76  
    77  	return []*types.MetricData{md}, nil
    78  }
    79  
    80  func (f *verticalLine) Description() map[string]types.FunctionDescription {
    81  	return map[string]types.FunctionDescription{
    82  		"verticalLine": {
    83  			Description: "Draws a vertical line at the designated timestamp with optional\n  'label' and 'color'. Supported timestamp formats include both\n  relative (e.g. -3h) and absolute (e.g. 16:00_20110501) strings,\n  such as those used with ``from`` and ``until`` parameters. When\n  set, the 'label' will appear in the graph legend.",
    84  			Function:    "verticalLine(ts, label=None, color=None)",
    85  			Group:       "Graph",
    86  			Module:      "graphite.render.functions",
    87  			Name:        "verticalLine",
    88  			Params: []types.FunctionParam{
    89  				{
    90  					Name:     "ts",
    91  					Required: true,
    92  					Type:     types.Date,
    93  				},
    94  				{
    95  					Name:     "label",
    96  					Required: false,
    97  					Type:     types.String,
    98  				},
    99  				{
   100  					Name:     "color",
   101  					Required: false,
   102  					Type:     types.String,
   103  				},
   104  			},
   105  		},
   106  	}
   107  }