bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/web/search.go (about)

     1  package web
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strconv"
     7  	"time"
     8  
     9  	"bosun.org/opentsdb"
    10  	"github.com/MiniProfiler/go/miniprofiler"
    11  	"github.com/gorilla/mux"
    12  )
    13  
    14  // UniqueMetrics returns a sorted list of available metrics.
    15  func UniqueMetrics(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) {
    16  	q := r.URL.Query()
    17  	var epoch int64
    18  	if v := q.Get("since"); v != "" {
    19  		var err error
    20  		epoch, err = strconv.ParseInt(v, 10, 64)
    21  		if err != nil {
    22  			serveError(w, fmt.Errorf("could not convert since parameter (expecting epoch value): %v", err))
    23  		}
    24  	}
    25  	values, err := schedule.Search.UniqueMetrics(epoch)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	// remove anything starting with double underscore.
    30  	if v := q.Get("unfiltered"); v != "" {
    31  		return values, nil
    32  	}
    33  	filtered := []string{}
    34  	for _, v := range values {
    35  		if len(v) < 2 || v[0:2] != "__" {
    36  			filtered = append(filtered, v)
    37  		}
    38  	}
    39  	return filtered, nil
    40  }
    41  
    42  func TagKeysByMetric(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) {
    43  	vars := mux.Vars(r)
    44  	metric := vars["metric"]
    45  	return schedule.Search.TagKeysByMetric(metric)
    46  }
    47  
    48  func TagValuesByMetricTagKey(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) {
    49  	vars := mux.Vars(r)
    50  	metric := vars["metric"]
    51  	tagk := vars["tagk"]
    52  	since, err := getSince(r)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return schedule.Search.TagValuesByMetricTagKey(metric, tagk, since)
    57  }
    58  
    59  func getSince(r *http.Request) (time.Duration, error) {
    60  	s := r.FormValue("since")
    61  	since := schedule.SystemConf.GetSearchSince()
    62  
    63  	if s != "" && s != "default" {
    64  		td, err := opentsdb.ParseDuration(s)
    65  		if err != nil {
    66  			return 0, err
    67  		}
    68  		since = time.Duration(td)
    69  	}
    70  	return since, nil
    71  }
    72  
    73  func FilteredTagsetsByMetric(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) {
    74  	vars := mux.Vars(r)
    75  	metric := vars["metric"]
    76  	tagset := opentsdb.TagSet{}
    77  	var err error
    78  	ts := r.FormValue("tags")
    79  	if ts != "" {
    80  		if tagset, err = opentsdb.ParseTags(ts); err != nil {
    81  			return nil, err
    82  		}
    83  	}
    84  
    85  	since := int64(0)
    86  	sinceStr := r.FormValue("since")
    87  	if sinceStr != "" {
    88  		since, err = strconv.ParseInt(sinceStr, 10, 64) //since will be set to 0 again in case of errors
    89  		if err != nil {
    90  			return nil, err
    91  		}
    92  	}
    93  	return schedule.Search.FilteredTagSets(metric, tagset, since)
    94  }
    95  
    96  func MetricsByTagPair(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) {
    97  	vars := mux.Vars(r)
    98  	since, err := getSince(r)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	tagk := vars["tagk"]
   103  	tagv := vars["tagv"]
   104  	return schedule.Search.MetricsByTagPair(tagk, tagv, since)
   105  }
   106  
   107  func MetricsByTagKey(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) {
   108  	vars := mux.Vars(r)
   109  	tagk := vars["tagk"]
   110  	since, err := getSince(r)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  	tagValues, err := schedule.Search.TagValuesByTagKey(tagk, since)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	// map[tagv][metrics...]
   119  	tagvMetrics := make(map[string][]string)
   120  	for _, tagv := range tagValues {
   121  		metrics, err := schedule.Search.MetricsByTagPair(tagk, tagv, since)
   122  		if err != nil {
   123  			return nil, err
   124  		}
   125  		tagvMetrics[tagv] = metrics
   126  	}
   127  	return tagvMetrics, nil
   128  }
   129  
   130  func TagValuesByTagKey(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) {
   131  	vars := mux.Vars(r)
   132  	tagk := vars["tagk"]
   133  	since, err := getSince(r)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	return schedule.Search.TagValuesByTagKey(tagk, since)
   138  }