-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Scroll depth: update site.scroll_depth_visible_at
in ingestion
#5035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9261cb3
WIP: Update scroll_depth_visible_at in ingestion
macobo 76d5dc9
Simplify code and test genserver directly
macobo 37eae52
No more check_scroll_depth_visible!
macobo 865d65b
Update a test
macobo 5893494
Update a test
macobo 7da10b9
GenServer -> ets
macobo 7e873c0
Additional where
macobo 7bc91dc
Fix a test
macobo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test can never fail. We should assert on the return value of |
||
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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.