From e724cd9dd2875d459662f8589f5ee971ed74409c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Wed, 12 Feb 2025 14:10:14 +0100 Subject: [PATCH] fix: day returned by InfluxDB query MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The function `observeRetrievalResultCodes()` includes the following statement in the InfluxDB query fetching `(day, code, rate)` data: |> aggregateWindow(every: 1d, fn: mean, createEmpty: false) Such query produces a list of values like this: ``` 2024-11-15T00:00:00Z,CONNECTION_REFUSED,0.0022313570194142725 2024-11-16T00:00:00Z,CONNECTION_REFUSED,0.002153071995819862 (...) 2025-02-12T00:00:00Z,CONNECTION_REFUSED,0.021266890041248942 2025-02-12T13:08:20.817239423Z,CONNECTION_REFUSED,0.02153170594662248 ``` Notice there are two rows for today (2025-02-12). One row contains data from yesterday (full day) and another row contain partial data from today. In this commit, I am fixing the query to correctly assign data points from yesterday to yesterday's date: |> aggregateWindow(every: 1d, fn: mean, createEmpty: false, timeSrc: "_start") The new query produces a list like this: ``` 2024-11-14T00:00:00Z,CONNECTION_REFUSED,0.0022313570194142725 2024-11-15T00:00:00Z,CONNECTION_REFUSED,0.002153071995819862 (...) 2025-02-11T00:00:00Z,CONNECTION_REFUSED,0.021266890041248942 2025-02-12T00:00:00Z,CONNECTION_REFUSED,0.02153170594662248 ``` This fixed the error introduced by cbb3bf17 (#316), where the SQL query fails with the following message: ``` ON CONFLICT DO UPDATE command cannot affect row a second time. Ensure that no rows proposed for insertion within the same command have duplicate constrained values. ``` See also InfluxDB documentation for `aggregateWindow()`: https://docs.influxdata.com/flux/v0/stdlib/universe/aggregatewindow/#timesrc Signed-off-by: Miroslav Bajtoš --- observer/lib/observer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/observer/lib/observer.js b/observer/lib/observer.js index 2a1bbbe..d28aa9c 100644 --- a/observer/lib/observer.js +++ b/observer/lib/observer.js @@ -113,7 +113,7 @@ export const observeRetrievalResultCodes = async (pgPoolStats, influxQueryApi) = |> range(start: 0) |> filter(fn: (r) => r["_measurement"] == "retrieval_stats_honest") |> filter(fn: (r) => strings.hasPrefix(v: r._field, prefix: "result_rate_")) - |> aggregateWindow(every: 1d, fn: mean, createEmpty: false) + |> aggregateWindow(every: 1d, fn: mean, createEmpty: false, timeSrc: "_start") |> keep(columns: ["_value", "_time", "_field"]) |> map(fn: (r) => ({ r with _field: strings.replace(v: r._field, t: "result_rate_", u: "", i: 1) })) `)