|
|
|
|
@ -383,4 +383,273 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
|
|
|
|
|
assert List.first(plot) == 200
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "GET /api/stats/main-graph - varying intervals" do
|
|
|
|
|
setup [:create_user, :log_in, :create_new_site]
|
|
|
|
|
|
|
|
|
|
test "displays visitors for a month on an hourly scale", %{conn: conn, site: site} do
|
|
|
|
|
populate_stats(site, [
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 01:01:00])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=month&date=2021-01-01&metric=visitors&interval=hour"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{"plot" => plot} = json_response(conn, 200)
|
|
|
|
|
|
|
|
|
|
assert Enum.count(plot) == 721
|
|
|
|
|
assert List.first(plot) == 1
|
|
|
|
|
assert Enum.at(plot, 1) == 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "displays visitors for a day on a minute scale", %{conn: conn, site: site} do
|
|
|
|
|
populate_stats(site, [
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:15:01]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:15:02])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=day&date=2021-01-01&metric=visitors&interval=minute"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{"plot" => plot} = json_response(conn, 200)
|
|
|
|
|
|
|
|
|
|
assert Enum.count(plot) == 1440
|
|
|
|
|
assert List.first(plot) == 1
|
|
|
|
|
assert Enum.at(plot, 15) == 2
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "displays visitors for date range on a minute scale", %{conn: conn, site: site} do
|
|
|
|
|
populate_stats(site, [
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:15:01]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:15:02]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-02 00:10:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-02 00:11:01]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-02 01:00:02]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-04 03:10:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-04 04:11:01]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-04 05:00:02])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=custom&from=2021-01-01&to=2021-01-04&metric=visitors&interval=minute"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{"plot" => plot} = json_response(conn, 200)
|
|
|
|
|
|
|
|
|
|
assert Enum.count(plot) == 4321
|
|
|
|
|
assert List.first(plot) == 1
|
|
|
|
|
assert Enum.at(plot, 15) == 2
|
|
|
|
|
assert Enum.at(plot, 1450) == 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "displays visitors for 6mo on a day scale", %{conn: conn, site: site} do
|
|
|
|
|
populate_stats(site, [
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-15 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-15 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-02-15 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-06-30 01:00:00])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=6mo&date=2021-06-01&metric=visitors&interval=date"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{"plot" => plot} = json_response(conn, 200)
|
|
|
|
|
|
|
|
|
|
assert Enum.count(plot) == 181
|
|
|
|
|
assert List.first(plot) == 1
|
|
|
|
|
assert Enum.at(plot, 14) == 2
|
|
|
|
|
assert Enum.at(plot, 45) == 1
|
|
|
|
|
assert List.last(plot) == 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "displays visitors for a custom period on a monthly scale", %{conn: conn, site: site} do
|
|
|
|
|
populate_stats(site, [
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-15 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-02-15 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-06-01 00:00:00])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=custom&from=2021-01-01&to=2021-06-30&metric=visitors&interval=month"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{"plot" => plot} = json_response(conn, 200)
|
|
|
|
|
|
|
|
|
|
assert Enum.count(plot) == 6
|
|
|
|
|
assert List.first(plot) == 2
|
|
|
|
|
assert Enum.at(plot, 1) == 1
|
|
|
|
|
assert List.last(plot) == 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "returns error when requesting an interval longer than the time period", %{
|
|
|
|
|
conn: conn,
|
|
|
|
|
site: site
|
|
|
|
|
} do
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=day&date=2021-01-01&metric=visitors&interval=month"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{
|
|
|
|
|
"error" =>
|
|
|
|
|
"Invalid combination of interval and period. Interval must be smaller than the selected period, e.g. `period=day,interval=minute`"
|
|
|
|
|
} == json_response(conn, 400)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "returns error when the interval is not valid", %{
|
|
|
|
|
conn: conn,
|
|
|
|
|
site: site
|
|
|
|
|
} do
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=day&date=2021-01-01&metric=visitors&interval=biweekly"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{
|
|
|
|
|
"error" =>
|
|
|
|
|
"Invalid value for interval. Accepted values are: minute, hour, date, week, month"
|
|
|
|
|
} == json_response(conn, 400)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "displays visitors for a month on a weekly scale", %{conn: conn, site: site} do
|
|
|
|
|
populate_stats(site, [
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-01 00:15:01]),
|
|
|
|
|
build(:pageview, timestamp: ~N[2021-01-05 00:15:02])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=month&date=2021-01-01&metric=visitors&interval=week"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{"plot" => plot} = json_response(conn, 200)
|
|
|
|
|
|
|
|
|
|
assert Enum.count(plot) == 5
|
|
|
|
|
assert List.first(plot) == 2
|
|
|
|
|
assert Enum.at(plot, 1) == 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "shows imperfect week-split month on week scale with full week indicators", %{
|
|
|
|
|
conn: conn,
|
|
|
|
|
site: site
|
|
|
|
|
} do
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=month&metric=visitors&interval=week&date=2021-09-01"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{"labels" => labels, "full_intervals" => full_intervals} = json_response(conn, 200)
|
|
|
|
|
|
|
|
|
|
assert labels == ["2021-09-01", "2021-09-06", "2021-09-13", "2021-09-20", "2021-09-27"]
|
|
|
|
|
|
|
|
|
|
assert full_intervals == %{
|
|
|
|
|
"2021-09-01" => false,
|
|
|
|
|
"2021-09-06" => true,
|
|
|
|
|
"2021-09-13" => true,
|
|
|
|
|
"2021-09-20" => true,
|
|
|
|
|
"2021-09-27" => false
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "shows half-perfect week-split month on week scale with full week indicators", %{
|
|
|
|
|
conn: conn,
|
|
|
|
|
site: site
|
|
|
|
|
} do
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=month&metric=visitors&interval=week&date=2021-10-01"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{"labels" => labels, "full_intervals" => full_intervals} = json_response(conn, 200)
|
|
|
|
|
|
|
|
|
|
assert labels == ["2021-10-01", "2021-10-04", "2021-10-11", "2021-10-18", "2021-10-25"]
|
|
|
|
|
|
|
|
|
|
assert full_intervals == %{
|
|
|
|
|
"2021-10-01" => false,
|
|
|
|
|
"2021-10-04" => true,
|
|
|
|
|
"2021-10-11" => true,
|
|
|
|
|
"2021-10-18" => true,
|
|
|
|
|
"2021-10-25" => true
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "shows perfect week-split range on week scale with full week indicators", %{
|
|
|
|
|
conn: conn,
|
|
|
|
|
site: site
|
|
|
|
|
} do
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=custom&metric=visitors&interval=week&from=2020-12-21&to=2021-02-07"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{"labels" => labels, "full_intervals" => full_intervals} = json_response(conn, 200)
|
|
|
|
|
|
|
|
|
|
assert labels == [
|
|
|
|
|
"2020-12-21",
|
|
|
|
|
"2020-12-28",
|
|
|
|
|
"2021-01-04",
|
|
|
|
|
"2021-01-11",
|
|
|
|
|
"2021-01-18",
|
|
|
|
|
"2021-01-25",
|
|
|
|
|
"2021-02-01"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
assert full_intervals == %{
|
|
|
|
|
"2020-12-21" => true,
|
|
|
|
|
"2020-12-28" => true,
|
|
|
|
|
"2021-01-04" => true,
|
|
|
|
|
"2021-01-11" => true,
|
|
|
|
|
"2021-01-18" => true,
|
|
|
|
|
"2021-01-25" => true,
|
|
|
|
|
"2021-02-01" => true
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "shows imperfect month-split period on month scale with full month indicators", %{
|
|
|
|
|
conn: conn,
|
|
|
|
|
site: site
|
|
|
|
|
} do
|
|
|
|
|
conn =
|
|
|
|
|
get(
|
|
|
|
|
conn,
|
|
|
|
|
"/api/stats/#{site.domain}/main-graph?period=custom&metric=visitors&interval=month&from=2021-09-06&to=2021-12-13"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert %{"labels" => labels, "full_intervals" => full_intervals} = json_response(conn, 200)
|
|
|
|
|
|
|
|
|
|
assert labels == ["2021-09-01", "2021-10-01", "2021-11-01", "2021-12-01"]
|
|
|
|
|
|
|
|
|
|
assert full_intervals == %{
|
|
|
|
|
"2021-09-01" => false,
|
|
|
|
|
"2021-10-01" => true,
|
|
|
|
|
"2021-11-01" => true,
|
|
|
|
|
"2021-12-01" => false
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|