From d9fd114066b7db643d9cf34f92a6b49cebed360a Mon Sep 17 00:00:00 2001 From: felixgateru Date: Thu, 27 Jul 2023 23:25:34 +0300 Subject: [PATCH 1/3] add country filter to telemetry filters Signed-off-by: felixgateru --- api/endpoint.go | 15 +++++++++------ api/requests.go | 9 +++++---- api/transport.go | 15 +++++++++++---- telemetry.go | 5 +++-- timescale/timescale.go | 3 +++ 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/api/endpoint.go b/api/endpoint.go index d962dcd..522a1c2 100644 --- a/api/endpoint.go +++ b/api/endpoint.go @@ -40,8 +40,9 @@ func retrieveEndpoint(svc callhome.Service) endpoint.Endpoint { Limit: req.limit, } filter := callhome.TelemetryFilters{ - From: req.from, - To: req.to, + From: req.from, + To: req.to, + Country: req.country, } tm, err := svc.Retrieve(ctx, pm, filter) if err != nil { @@ -66,8 +67,9 @@ func retrieveSummaryEndpoint(svc callhome.Service) endpoint.Endpoint { return nil, err } filter := callhome.TelemetryFilters{ - From: req.from, - To: req.to, + From: req.from, + To: req.to, + Country: req.country, } summary, err := svc.RetrieveSummary(ctx, filter) if err != nil { @@ -87,8 +89,9 @@ func serveUI(svc callhome.Service) endpoint.Endpoint { return nil, err } filter := callhome.TelemetryFilters{ - From: req.from, - To: req.to, + From: req.from, + To: req.to, + Country: req.country, } res, err := svc.ServeUI(ctx, filter) return uiRes{ diff --git a/api/requests.go b/api/requests.go index fd44875..ee8fef3 100644 --- a/api/requests.go +++ b/api/requests.go @@ -40,10 +40,11 @@ func (req saveTelemetryReq) validate() error { } type listTelemetryReq struct { - offset uint64 - limit uint64 - from time.Time - to time.Time + offset uint64 + limit uint64 + from time.Time + to time.Time + country string } func (req listTelemetryReq) validate() error { diff --git a/api/transport.go b/api/transport.go index 17a55fb..44c6178 100644 --- a/api/transport.go +++ b/api/transport.go @@ -26,6 +26,7 @@ const ( limitKey = "limit" fromKey = "from" toKey = "to" + countryKey = "country" defOffset = 0 defLimit = 10 staticDir = "./web/static" @@ -179,11 +180,17 @@ func decodeRetrieve(_ context.Context, r *http.Request) (interface{}, error) { } } + co, err := ReadStringQuery(r, countryKey, "") + if err != nil { + return nil, err + } + req := listTelemetryReq{ - offset: o, - limit: l, - from: from, - to: to, + offset: o, + limit: l, + from: from, + to: to, + country: co, } return req, nil } diff --git a/telemetry.go b/telemetry.go index 0a40604..2b4f98b 100644 --- a/telemetry.go +++ b/telemetry.go @@ -21,8 +21,9 @@ type Telemetry struct { } type TelemetryFilters struct { - From time.Time - To time.Time + From time.Time + To time.Time + Country string } type PageMetadata struct { diff --git a/timescale/timescale.go b/timescale/timescale.go index 037e03a..71c6259 100644 --- a/timescale/timescale.go +++ b/timescale/timescale.go @@ -160,6 +160,9 @@ func generateQuery(filters callhome.TelemetryFilters) (string, map[string]interf case !filters.To.IsZero(): queries = append(queries, "time <= :to") params["to"] = filters.To + case filters.Country != "": + queries = append(queries, "country = :country") + params["country"] = filters.Country } switch len(queries) { From 58d1b5ce6146a43bb54a913005505b17f3ec17b6 Mon Sep 17 00:00:00 2001 From: felixgateru Date: Fri, 28 Jul 2023 13:18:14 +0300 Subject: [PATCH 2/3] Update openAPI spec Signed-off-by: felixgateru --- openapi/openapi.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/openapi/openapi.yaml b/openapi/openapi.yaml index bf43f99..51bc138 100644 --- a/openapi/openapi.yaml +++ b/openapi/openapi.yaml @@ -27,6 +27,7 @@ paths: - $ref: "#/components/parameters/Offset" - $ref: "#/components/parameters/From" - $ref: "#/components/parameters/To" + - $ref: "#/components/parameters/Country" responses: "200": description: found @@ -45,6 +46,7 @@ paths: - $ref: "#/components/parameters/Offset" - $ref: "#/components/parameters/From" - $ref: "#/components/parameters/To" + - $ref: "#/components/parameters/Country" responses: "200": description: found @@ -78,6 +80,7 @@ paths: - $ref: "#/components/parameters/Offset" - $ref: "#/components/parameters/From" - $ref: "#/components/parameters/To" + - $ref: "#/components/parameters/Country" tags: - telemetry summary: Retrieve telemetry events @@ -137,6 +140,14 @@ components: type: string default: "" required: false + Country: + name: country + description: From country filter. + in: query + schema: + type: string + default: "" + required: false requestBodies: TelemetryReq: content: From cad3e4f2e7ada7b63c040d4a1618ed3839a84c1a Mon Sep 17 00:00:00 2001 From: felixgateru Date: Fri, 28 Jul 2023 13:23:43 +0300 Subject: [PATCH 3/3] Update country filter Signed-off-by: felixgateru --- timescale/timescale.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/timescale/timescale.go b/timescale/timescale.go index 71c6259..929da47 100644 --- a/timescale/timescale.go +++ b/timescale/timescale.go @@ -160,7 +160,9 @@ func generateQuery(filters callhome.TelemetryFilters) (string, map[string]interf case !filters.To.IsZero(): queries = append(queries, "time <= :to") params["to"] = filters.To - case filters.Country != "": + } + + if filters.Country != "" { queries = append(queries, "country = :country") params["country"] = filters.Country }