Proper timestamp

This commit is contained in:
Uku Taht 2019-10-15 15:37:55 +08:00
parent 71edf2c86d
commit 7c7bdc6ef6
5 changed files with 25 additions and 14 deletions

View File

@ -120,7 +120,7 @@ defmodule Plausible.Billing do
Repo.aggregate(from(
p in Plausible.Pageview,
where: p.hostname == ^site.domain,
where: p.inserted_at >= fragment("now() - '30 days'::interval")
where: p.timestamp >= fragment("now() - '30 days'::interval")
), :count, :id
)
end

View File

@ -15,7 +15,7 @@ defmodule Plausible.Pageview do
field :browser, :string
field :referrer_source, :string
timestamps()
timestamps(inserted_at: :timestamp, updated_at: false)
end
def changeset(pageview, attrs) do

View File

@ -27,7 +27,7 @@ defmodule Plausible.Stats do
from p in base_query(site, query),
group_by: 1,
order_by: 1,
select: {fragment("date_trunc('month', ? at time zone 'utc' at time zone ?)", p.inserted_at, ^site.timezone), count(p.user_id, :distinct)}
select: {fragment("date_trunc('month', ? at time zone 'utc' at time zone ?)", p.timestamp, ^site.timezone), count(p.user_id, :distinct)}
) |> Enum.into(%{})
|> transform_keys(fn dt -> NaiveDateTime.to_date(dt) end)
@ -45,7 +45,7 @@ defmodule Plausible.Stats do
from p in base_query(site, query),
group_by: 1,
order_by: 1,
select: {fragment("date_trunc('day', ? at time zone 'utc' at time zone ?)", p.inserted_at, ^site.timezone), count(p.user_id, :distinct)}
select: {fragment("date_trunc('day', ? at time zone 'utc' at time zone ?)", p.timestamp, ^site.timezone), count(p.user_id, :distinct)}
) |> Enum.into(%{})
|> transform_keys(fn dt -> NaiveDateTime.to_date(dt) end)
@ -71,7 +71,7 @@ defmodule Plausible.Stats do
from p in base_query(site, query),
group_by: 1,
order_by: 1,
select: {fragment("date_trunc('hour', ? at time zone 'utc' at time zone ?)", p.inserted_at, ^site.timezone), count(p.user_id, :distinct)}
select: {fragment("date_trunc('hour', ? at time zone 'utc' at time zone ?)", p.timestamp, ^site.timezone), count(p.user_id, :distinct)}
)
|> Enum.into(%{})
|> transform_keys(fn dt -> NaiveDateTime.truncate(dt, :second) end)
@ -186,7 +186,7 @@ defmodule Plausible.Stats do
def current_visitors(site) do
Repo.one(
from p in Plausible.Pageview,
where: p.inserted_at >= fragment("(now() at time zone 'utc') - '5 minutes'::interval"),
where: p.timestamp >= fragment("(now() at time zone 'utc') - '5 minutes'::interval"),
where: p.hostname == ^site.domain,
select: count(p.user_id, :distinct)
)
@ -201,7 +201,7 @@ defmodule Plausible.Stats do
from(p in Plausible.Pageview,
where: p.hostname == ^site.domain,
where: p.inserted_at >= ^first_datetime and p.inserted_at < ^last_datetime
where: p.timestamp >= ^first_datetime and p.timestamp < ^last_datetime
)
end

View File

@ -0,0 +1,11 @@
defmodule Plausible.Repo.Migrations.ProperTimestampForPageviews do
use Ecto.Migration
def change do
alter table(:pageviews) do
remove :updated_at
end
rename table(:pageviews), :inserted_at, to: :timestamp
end
end

View File

@ -5,8 +5,8 @@ defmodule Plausible.StatsTest do
describe "calculate_plot" do
test "displays pageviews for a day" do
site = insert(:site)
insert(:pageview, hostname: site.domain, inserted_at: ~N[2019-01-01 00:00:00])
insert(:pageview, hostname: site.domain, inserted_at: ~N[2019-01-01 23:59:00])
insert(:pageview, hostname: site.domain, timestamp: ~N[2019-01-01 00:00:00])
insert(:pageview, hostname: site.domain, timestamp: ~N[2019-01-01 23:59:00])
query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"})
@ -20,8 +20,8 @@ defmodule Plausible.StatsTest do
test "displays pageviews for a month" do
site = insert(:site)
insert(:pageview, hostname: site.domain, inserted_at: ~N[2019-01-01 12:00:00])
insert(:pageview, hostname: site.domain, inserted_at: ~N[2019-01-31 12:00:00])
insert(:pageview, hostname: site.domain, timestamp: ~N[2019-01-01 12:00:00])
insert(:pageview, hostname: site.domain, timestamp: ~N[2019-01-31 12:00:00])
query = Stats.Query.from(site.timezone, %{"period" => "month", "date" => "2019-01-01"})
{plot, _labels, _index} = Stats.calculate_plot(site, query)
@ -34,7 +34,7 @@ defmodule Plausible.StatsTest do
test "displays pageviews for a 3 months" do
site = insert(:site)
insert(:pageview, hostname: site.domain)
insert(:pageview, hostname: site.domain, inserted_at: months_ago(2))
insert(:pageview, hostname: site.domain, timestamp: months_ago(2))
query = Stats.Query.from(site.timezone, %{"period" => "3mo"})
{plot, _labels, _index} = Stats.calculate_plot(site, query)
@ -155,13 +155,13 @@ defmodule Plausible.StatsTest do
insert(:pageview, %{
hostname: site.domain,
user_id: UUID.uuid4(),
inserted_at: Timex.now() |> Timex.shift(minutes: -4)
timestamp: Timex.now() |> Timex.shift(minutes: -4)
})
insert(:pageview, %{
hostname: site.domain,
user_id: UUID.uuid4(),
inserted_at: Timex.now() |> Timex.shift(minutes: -6)
timestamp: Timex.now() |> Timex.shift(minutes: -6)
})
assert Stats.current_visitors(site) == 2