Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support order_by in URL params of breakdown endpoints #4502

Merged
merged 2 commits into from
Sep 2, 2024
Merged

Conversation

apata
Copy link
Contributor

@apata apata commented Sep 2, 2024

Changes

  • Adds support for order_by in URL params of breakdown endpoints

Tests

  • Automated tests have been added

Changelog

  • Does not need a changelog entry

Documentation

  • This change does not need a documentation update

Dark mode

  • This PR does not change the UI

Comment on lines +502 to +525
test "shows sources for a page (using new filters)", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, pathname: "/page1", referrer_source: "Google"),
build(:pageview, pathname: "/page1", referrer_source: "Google"),
build(:pageview,
user_id: 1,
pathname: "/page2",
referrer_source: "DuckDuckGo"
),
build(:pageview,
user_id: 1,
pathname: "/page1",
referrer_source: "DuckDuckGo"
)
])

filters = Jason.encode!([["is", "event:page", ["/page1"]]])
conn = get(conn, "/api/stats/#{site.domain}/sources?filters=#{filters}")

assert json_response(conn, 200)["results"] == [
%{"name" => "Google", "visitors" => 2},
%{"name" => "DuckDuckGo", "visitors" => 1}
]
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an additional sanity check for already existing functionality :)

@apata apata force-pushed the list-sort branch 2 times, most recently from d609428 to 68ebb7a Compare September 2, 2024 07:39
@@ -164,6 +165,11 @@ defmodule Plausible.Stats.Legacy.QueryBuilder do
end
end

defp put_order_by(query, %{} = params) do
order_by = Map.get(params, "order_by", nil) |> Filters.parse_order_by()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't clear Filters.parse_order_by is part of 'legacy' parsing code that we can delete soon and shouldn't be used outside of it.

Instead of calling Filters module we should either:
a) Make Plausible.Stats.Filters.QueryParser.parse_order_by public
or
b) Make it a defp in this class instead of in the filters module

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil is also default when no value is found, so Map.get(params, "order_by") suffices, as well as params["order_by"]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions! Went with B, but got a warning on doc string (and therefore my doctests) being ignored on private functions, had to keep it def.

lib/plausible/stats/filters/filters.ex Outdated Show resolved Hide resolved
lib/plausible/stats/breakdown.ex Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@apata apata force-pushed the list-sort branch 2 times, most recently from e93f9a8 to 8581b1f Compare September 2, 2024 10:51
Copy link
Contributor

@macobo macobo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good (bar missing comment). 🥇

@apata apata merged commit 9b59e95 into master Sep 2, 2024
10 checks passed
@macobo macobo deleted the list-sort branch September 2, 2024 12:11
@ruslandoga
Copy link
Contributor

ruslandoga commented Sep 10, 2024

👋 @apata

The new "order_by [[visit_duration, asc], [visit:source, desc]]] is respected and flipping the sort orders works" test fails CI in #4491: https://github.com/plausible/analytics/actions/runs/10785402818/job/29910533968

order_by_asc = Jason.encode!([["visit_duration", "asc"], ["visit:source", "desc"]])

conn1 =
get(conn, "/api/stats/#{site.domain}/sources?detailed=true&order_by=#{order_by_asc}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any time filtering happening here? If so, should the timestamps in pageviews be dynamic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it filters the last month by default, since it started failing today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants