Skip to content

Commit

Permalink
fix: day returned by InfluxDB query
Browse files Browse the repository at this point in the history
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 cbb3bf1 (#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š <[email protected]>
  • Loading branch information
bajtos committed Feb 12, 2025
1 parent 2cd5489 commit e724cd9
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion observer/lib/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) }))
`)
Expand Down

0 comments on commit e724cd9

Please sign in to comment.