Skip to content

Commit

Permalink
webapp: Fix handling out of range segment on graphs
Browse files Browse the repository at this point in the history
Before this, graphs didn't include the segment which intersects left
boundary of a graph (21 day boundary in other words). This was not
visible because on 21 day range, updates are rather frequent, and the
leftmost segment starts just after the boundary.

However, if there's huge gap in updates, or history is deduplicated
to remove points which do not have any counter changes (see #120),
we'll see a missing segment. Fix that by fetching an extra data point
just before the graph range.
  • Loading branch information
AMDmi3 committed Nov 19, 2024
1 parent c09a5be commit 45b6c71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
24 changes: 18 additions & 6 deletions repology-webapp/src/views/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@ const GRAPH_PERIOD: Duration = Duration::from_days(21);

async fn graph_total_generic(pool: &PgPool, field: &str, stroke: &str) -> EndpointResult {
let points: Vec<(DateTime<Utc>, f32)> = sqlx::query_as(indoc! {r#"
SELECT
ts AS timestamp,
(snapshot->>$1)::real AS value
FROM statistics_history
WHERE ts >= now() - $2
ORDER BY ts
(
SELECT
ts AS timestamp,
(snapshot->>$1)::real AS value
FROM statistics_history
WHERE ts < now() - $2
ORDER BY ts DESC
LIMIT 1
)
UNION ALL
(
SELECT
ts AS timestamp,
(snapshot->>$1)::real AS value
FROM statistics_history
WHERE ts >= now() - $2
ORDER BY ts
)
"#})
.bind(&field)
.bind(&GRAPH_PERIOD)
Expand Down
3 changes: 2 additions & 1 deletion repology-webapp/tests/fixtures/graphs_data.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
INSERT INTO statistics_history(ts, snapshot) VALUES
(now() - interval '1 day', '{"num_packages": 0, "num_problems": 0, "num_maintainers": 0, "num_metapackages": 0}'),
-- point is intentionally long in the past to check whether it's handled properly producing a line
(now() - interval '128 day', '{"num_packages": 0, "num_problems": 0, "num_maintainers": 0, "num_metapackages": 0}'),
(now(), '{"num_packages": 10, "num_problems": 10, "num_maintainers": 10, "num_metapackages": 10}');

0 comments on commit 45b6c71

Please sign in to comment.