Crop big consolidated views (#5835)
* Crop big consolidated views * number format
This commit is contained in:
parent
0e0415fd6b
commit
ce02ab0799
|
|
@ -12,6 +12,8 @@ defmodule Plausible.ConsolidatedView.Cache do
|
|||
use Plausible.Cache
|
||||
|
||||
@cache_name :consolidated_views
|
||||
@large_view_alert_threshold 12_000
|
||||
@max_sites_per_view 14_000
|
||||
|
||||
@impl true
|
||||
def name(), do: @cache_name
|
||||
|
|
@ -82,4 +84,21 @@ defmodule Plausible.ConsolidatedView.Cache do
|
|||
[{row.consolidated_view_id, row.site_ids} | acc]
|
||||
end)
|
||||
end
|
||||
|
||||
def get(key, opts) do
|
||||
case super(key, opts) do
|
||||
nil ->
|
||||
[]
|
||||
|
||||
site_ids when length(site_ids) > @large_view_alert_threshold ->
|
||||
Sentry.capture_message("Consolidated View crop warning",
|
||||
extra: %{sites: length(site_ids), key: key}
|
||||
)
|
||||
|
||||
Enum.take(site_ids, @max_sites_per_view)
|
||||
|
||||
site_ids ->
|
||||
site_ids
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
defmodule Plausible.CondolidatedView.CacheTestSync do
|
||||
use Plausible.DataCase, async: false
|
||||
use Plausible.Teams.Test
|
||||
|
||||
on_ee do
|
||||
alias Plausible.ConsolidatedView.Cache
|
||||
|
||||
setup do
|
||||
Sentry.put_config(:test_mode, true)
|
||||
|
||||
on_exit(fn ->
|
||||
Sentry.put_config(:test_mode, false)
|
||||
end)
|
||||
end
|
||||
|
||||
test "big views get cropped up to 14k", %{test: test} do
|
||||
assert :ok = Sentry.Test.start_collecting_sentry_reports()
|
||||
{:ok, _pid} = start_test_cache(test)
|
||||
|
||||
Plausible.Cache.Adapter.put(test, "key", Enum.to_list(1..20_000))
|
||||
site_ids = Cache.get("key", cache_name: test, force?: true)
|
||||
assert length(site_ids) == 14_000
|
||||
|
||||
assert [
|
||||
%{
|
||||
extra: %{key: "key", sites: 20_000},
|
||||
message: %{formatted: "Consolidated View crop warning"}
|
||||
}
|
||||
] = Sentry.Test.pop_sentry_reports()
|
||||
end
|
||||
end
|
||||
|
||||
defp start_test_cache(cache_name) do
|
||||
%{start: {m, f, a}} = Cache.child_spec(cache_name: cache_name)
|
||||
apply(m, f, a)
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue