Skip to content

Commit

Permalink
Set with_imported=false in realtime mode (#3053)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vinibrsl authored Jun 19, 2023
1 parent f0bdf87 commit 5012e0c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
1 change: 1 addition & 0 deletions lib/plausible/stats/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down

0 comments on commit 5012e0c

Please sign in to comment.