From 10e46be562e109dd0d15b717f0e0503fd107073c Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Thu, 7 Nov 2024 17:05:17 +0100 Subject: [PATCH] add little filter capabilities (#19) Signed-off-by: Augustin Husson --- README.md | 5 +++++ go.mod | 1 + go.sum | 3 +++ source/metric/endpoint.go | 32 +++++++++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b16ea26..611792d 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,11 @@ The tool provides an API endpoint, `/api/v1/metrics`, which returns the usage da } ``` +You can used the following query parameter to filter the list returned: + +* **metric_name**: when used, it will trigger a fuzzy search on the metric_name based on the pattern provided. +* **used**: when used, will return only the metric used or not (depending if you set this boolean to true or to false). Leave it empty if you want both. + ## How to use it ### Central instance diff --git a/go.mod b/go.mod index e799d49..2b622c4 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/go-openapi/strfmt v0.23.0 github.com/grafana/grafana-openapi-client-go v0.0.0-20241101140420-bc381928ae6e github.com/labstack/echo/v4 v4.12.0 + github.com/lithammer/fuzzysearch v1.1.8 github.com/perses/common v0.26.0 github.com/perses/perses v0.49.0-rc.1.0.20241104091321-0a0482386006 github.com/prometheus/client_golang v1.20.5 diff --git a/go.sum b/go.sum index eff655f..a2a3464 100644 --- a/go.sum +++ b/go.sum @@ -204,6 +204,8 @@ github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+k github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= +github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4= +github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= @@ -392,6 +394,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U= diff --git a/source/metric/endpoint.go b/source/metric/endpoint.go index 0a50ed3..4975587 100644 --- a/source/metric/endpoint.go +++ b/source/metric/endpoint.go @@ -18,6 +18,7 @@ import ( "net/http" "github.com/labstack/echo/v4" + "github.com/lithammer/fuzzysearch/fuzzy" persesEcho "github.com/perses/common/echo" "github.com/perses/metrics-usage/database" v1 "github.com/perses/metrics-usage/pkg/api/v1" @@ -49,8 +50,37 @@ func (e *endpoint) GetMetric(ctx echo.Context) error { return ctx.JSON(http.StatusOK, metric) } +type request struct { + MetricName string `query:"metric_name"` + Used *bool `query:"used"` +} + +func (r *request) filter(list map[string]*v1.Metric) map[string]*v1.Metric { + result := make(map[string]*v1.Metric) + if len(r.MetricName) == 0 && r.Used == nil { + return list + } + for k, v := range list { + if len(r.MetricName) == 0 || fuzzy.Match(r.MetricName, k) { + if r.Used == nil { + result[k] = v + } else if *r.Used && list[k].Usage != nil { + result[k] = v + } else if !*r.Used && list[k].Usage == nil { + result[k] = v + } + } + } + return result +} + func (e *endpoint) ListMetrics(ctx echo.Context) error { - return ctx.JSON(http.StatusOK, e.db.ListMetrics()) + req := &request{} + err := ctx.Bind(req) + if err != nil { + return ctx.JSON(http.StatusBadRequest, echo.Map{"message": err.Error()}) + } + return ctx.JSON(http.StatusOK, req.filter(e.db.ListMetrics())) } func (e *endpoint) PushMetricsUsage(ctx echo.Context) error {