Fix present_index for daily, weekly and monthly intervals (#4358)

* Fix present_index for daily, weekly and monthly intervals

* Add test for present_index
This commit is contained in:
Uku Taht 2024-07-16 12:12:38 +03:00 committed by GitHub
parent ba77f0513e
commit 28787198b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 0 deletions

View File

@ -276,6 +276,7 @@ defmodule PlausibleWeb.Api.StatsController do
current_date =
Timex.now(site.timezone)
|> Timex.to_date()
|> Date.to_string()
Enum.find_index(dates, &(&1 == current_date))
@ -284,6 +285,7 @@ defmodule PlausibleWeb.Api.StatsController do
Timex.now(site.timezone)
|> Timex.to_date()
|> date_or_weekstart(query)
|> Date.to_string()
Enum.find_index(dates, &(&1 == current_date))
@ -292,6 +294,7 @@ defmodule PlausibleWeb.Api.StatsController do
Timex.now(site.timezone)
|> Timex.to_date()
|> Timex.beginning_of_month()
|> Date.to_string()
Enum.find_index(dates, &(&1 == current_date))

View File

@ -1426,4 +1426,43 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
]
end
end
describe "present_index" do
setup [:create_user, :log_in, :create_new_site]
test "exists for a date range that includes the current day", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview)
])
conn =
get(
conn,
"/api/stats/#{site.domain}/main-graph?period=month&metric=pageviews"
)
assert %{"present_index" => present_index} = json_response(conn, 200)
assert present_index >= 0
end
test "is null for a date range that does not include the current day", %{
conn: conn,
site: site
} do
populate_stats(site, [
build(:pageview)
])
conn =
get(
conn,
"/api/stats/#{site.domain}/main-graph?period=month&date=2021-01-01&metric=pageviews"
)
assert %{"present_index" => present_index} = json_response(conn, 200)
refute present_index
end
end
end