From 7c7bdc6ef66ff19d996ebbc34a6f8b1acce1cee5 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 15 Oct 2019 15:37:55 +0800 Subject: [PATCH] Proper timestamp --- lib/plausible/billing/billing.ex | 2 +- lib/plausible/pageview/schema.ex | 2 +- lib/plausible/stats/stats.ex | 10 +++++----- ...191015073507_proper_timestamp_for_pageviews.exs | 11 +++++++++++ test/plausible/stats/stats_test.exs | 14 +++++++------- 5 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 priv/repo/migrations/20191015073507_proper_timestamp_for_pageviews.exs diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index cea0b107f..f752dbef1 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -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 diff --git a/lib/plausible/pageview/schema.ex b/lib/plausible/pageview/schema.ex index f5b80932f..7132b7075 100644 --- a/lib/plausible/pageview/schema.ex +++ b/lib/plausible/pageview/schema.ex @@ -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 diff --git a/lib/plausible/stats/stats.ex b/lib/plausible/stats/stats.ex index 8719c0db7..8d72f5bd3 100644 --- a/lib/plausible/stats/stats.ex +++ b/lib/plausible/stats/stats.ex @@ -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 diff --git a/priv/repo/migrations/20191015073507_proper_timestamp_for_pageviews.exs b/priv/repo/migrations/20191015073507_proper_timestamp_for_pageviews.exs new file mode 100644 index 000000000..f06a9428e --- /dev/null +++ b/priv/repo/migrations/20191015073507_proper_timestamp_for_pageviews.exs @@ -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 diff --git a/test/plausible/stats/stats_test.exs b/test/plausible/stats/stats_test.exs index 57e4f21ad..2499cce2a 100644 --- a/test/plausible/stats/stats_test.exs +++ b/test/plausible/stats/stats_test.exs @@ -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