-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scroll depth: update
site.scroll_depth_visible_at
in ingestion (#5035)
* WIP: Update scroll_depth_visible_at in ingestion * Simplify code and test genserver directly * No more check_scroll_depth_visible! * Update a test * Update a test * GenServer -> ets * Additional where * Fix a test
- Loading branch information
Showing
11 changed files
with
109 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule Plausible.Ingestion.ScrollDepthVisibleAt do | ||
@moduledoc """ | ||
Module that updates the `scroll_depth_visible_at` column for a site when needed. | ||
This is called in a hot loop in ingestion, so it: | ||
1. Only updates the database once per site async (if SiteCache doesn't know about visibility yet) | ||
2. Does not retry the update if it fails, to be retried on server restart | ||
""" | ||
|
||
require Logger | ||
alias Plausible.Repo | ||
|
||
import Ecto.Query | ||
|
||
def init() do | ||
:ets.new(__MODULE__, [ | ||
:named_table, | ||
:set, | ||
:public, | ||
{:read_concurrency, true}, | ||
{:write_concurrency, true} | ||
]) | ||
end | ||
|
||
def mark_scroll_depth_visible(site_id) do | ||
if :ets.insert_new(__MODULE__, {site_id}) do | ||
Task.start(fn -> attempt_update_repo(site_id) end) | ||
end | ||
end | ||
|
||
defp attempt_update_repo(site_id) do | ||
Repo.update_all( | ||
from(s in Plausible.Site, where: s.id == ^site_id and is_nil(s.scroll_depth_visible_at)), | ||
set: [ | ||
scroll_depth_visible_at: DateTime.utc_now() | ||
] | ||
) | ||
rescue | ||
error -> | ||
Logger.error( | ||
"Error updating scroll_depth_visible_at for site #{site_id}: #{inspect(error)}. This will not be retried until server restart." | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Plausible.Ingestion.ScrollDepthVisibleAtTest do | ||
use Plausible.DataCase | ||
use Plausible.Teams.Test | ||
|
||
test "mark_scroll_depth_visible" do | ||
site = new_site() | ||
Plausible.Ingestion.ScrollDepthVisibleAt.mark_scroll_depth_visible(site.id) | ||
|
||
assert Plausible.TestUtils.eventually(fn -> | ||
site = Plausible.Repo.reload!(site) | ||
success = not is_nil(site.scroll_depth_visible_at) | ||
{success, success} | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters