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

     1  package unique
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/go-graphite/carbonapi/expr/helper"
     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  )
    11  
    12  type unique struct{}
    13  
    14  func GetOrder() interfaces.Order {
    15  	return interfaces.Any
    16  }
    17  
    18  func New(_ string) []interfaces.FunctionMetadata {
    19  	res := make([]interfaces.FunctionMetadata, 0)
    20  	f := &unique{}
    21  	functions := []string{"unique"}
    22  	for _, n := range functions {
    23  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    24  	}
    25  	return res
    26  }
    27  
    28  // unique(seriesList)
    29  func (f *unique) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    30  	arg, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	var results []*types.MetricData
    36  	seenNames := make(map[string]bool)
    37  
    38  	for _, a := range arg {
    39  		if _, ok := seenNames[a.Name]; !ok {
    40  			seenNames[a.Name] = true
    41  			results = append(results, a)
    42  		}
    43  	}
    44  	return results, nil
    45  }
    46  
    47  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    48  func (f *unique) Description() map[string]types.FunctionDescription {
    49  	return map[string]types.FunctionDescription{
    50  		"unique": {
    51  			Description: "Takes an arbitrary number of seriesLists and returns unique series, filtered by name.\n\nExample:\n\n.. code-block:: none\n\n  &target=unique(mostDeviant(server.*.disk_free,5),lowestCurrent(server.*.disk_free,5))\n\n  Draws servers with low disk space, and servers with highly deviant disk space, but never the same series twice.",
    52  			Function:    "unique(seriesList)",
    53  			Group:       "Transform",
    54  			Module:      "graphite.render.functions",
    55  			Name:        "unique",
    56  			Params: []types.FunctionParam{
    57  				{
    58  					Name:     "seriesList",
    59  					Required: true,
    60  					Type:     types.SeriesList,
    61  				},
    62  			},
    63  		},
    64  	}
    65  }