From 5012e0c0ec7e0dbbcdb7fd423a1d0abf25e80937 Mon Sep 17 00:00:00 2001 From: Vini Brasil Date: Mon, 19 Jun 2023 11:02:31 +0100 Subject: [PATCH] Set with_imported=false in realtime mode (#3053) This commit fixes a bug where timeseries queries - such as the main graph - would fail with imported data in realtime mode. Realtime mode `date` field is not actually a date, but minutes from now. This would cause the imported tables join to fail with this error: ``` (Ch.Error Code: 53. DB::Exception: Can't infer common type for joined columns: date: Int64 at left, s1.date: Date at right. There is no supertype for types Int64, Date because some of them are Date/Date32/DateTime/DateTime64 and some of them are not. (TYPE_MISMATCH) ``` This commit removes imported data from realtime queries, as it doesn't make sense to include it. Imported data does not have time precision, and would only appear in the first day the data was imported anyways. --- CHANGELOG.md | 1 + lib/plausible/stats/query.ex | 1 + .../api/stats_controller/main_graph_test.exs | 19 ++++++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ee50f8aa..76156021a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ All notable changes to this project will be documented in this file. - Fix bug when combining goal and prop filters plausible/analytics#2654 - Fix broken favicons when domain includes a slash - Fix bug when using multiple [wildcard goal filters](https://github.com/plausible/analytics/pull/3015) +- Fix a bug where realtime would fail with imported data ### Changed - Treat page filter as entry page filter for `bounce_rate` diff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.ex index 092178b6a..1b71b71cc 100644 --- a/lib/plausible/stats/query.ex +++ b/lib/plausible/stats/query.ex @@ -235,6 +235,7 @@ defmodule Plausible.Stats.Query do site.imported_data.status != "ok" -> false Timex.after?(query.date_range.first, site.imported_data.end_date) -> false Enum.any?(query.filters) -> false + query.period == "realtime" -> false true -> requested? end end diff --git a/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs b/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs index b077bf369..d985185f3 100644 --- a/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs @@ -33,12 +33,29 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do assert %{"plot" => plot} = json_response(conn, 200) - zeroes = Stream.repeatedly(fn -> 0 end) |> Stream.take(22) |> Enum.into([]) + zeroes = List.duplicate(0, 22) assert Enum.count(plot) == 24 assert plot == [1] ++ zeroes ++ [1] end + test "returns empty plot with no native data and recently imported from ga in realtime graph", + %{conn: conn, site: site} do + populate_stats(site, [ + build(:imported_visitors, date: Date.utc_today()), + build(:imported_visitors, date: Date.utc_today()) + ]) + + conn = + get( + conn, + "/api/stats/#{site.domain}/main-graph?period=realtime&with_imported=true" + ) + + zeroes = List.duplicate(0, 30) + assert %{"plot" => ^zeroes, "with_imported" => false} = json_response(conn, 200) + end + test "displays visitors for a day with imported data", %{conn: conn, site: site} do populate_stats(site, [ build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),