Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Exploration rate limit (#6337)
Adam Rutkowski6544b0563ebe
diff --git a/assets/js/dashboard/api.ts b/assets/js/dashboard/api.tsindex 91e76fab66..482af38483 100644--- a/assets/js/dashboard/api.ts+++ b/assets/js/dashboard/api.ts@@ -45,10 +45,12 @@ export type QueryApiResponse = {export class ApiError extends Error {payload: unknown- constructor(message: string, payload: unknown) {+ status: number+ constructor(message: string, payload: unknown, status: number) {super(message)this.name = 'ApiError'this.payload = payload+ this.status = status}}@@ -129,7 +131,7 @@ function getHeaders(): Record<string, string> {async function handleApiResponse(response: Response) {const payload = await response.json()if (!response.ok) {- throw new ApiError(payload.error, payload)+ throw new ApiError(payload.error, payload, response.status)}return payloaddiff --git a/assets/js/dashboard/extra/exploration.js b/assets/js/dashboard/extra/exploration.jsindex e962985628..ee9f85b232 100644--- a/assets/js/dashboard/extra/exploration.js+++ b/assets/js/dashboard/extra/exploration.js@@ -7,6 +7,7 @@ import React, {} from 'react'import LazyLoader from '../components/lazy-loader'import * as api from '../api'+import { ApiError } from '../api'import * as url from '../util/url'import { Tooltip } from '../util/tooltip'import { useDebounce } from '../custom-hooks'@@ -42,7 +43,8 @@ const EMPTY_JOURNEY_STATE = {activeFilter: '',// list of suggestions the user saw when picking stepfrozen: {},- provisional: {}+ provisional: {},+ rateLimited: false}const EMPTY_SVG_DATA = {@@ -60,6 +62,10 @@ function roundedPercentage(value, total) {return Math.round((percentage + Number.EPSILON) * 100) / 100}+function isRateLimitedError(err) {+ return err instanceof ApiError && err.status === 429+}+// Two steps are identical when their identity fields match.function stepsEqual(a, b) {return (@@ -135,9 +141,9 @@ function fetchNextWithFunnel()}-function fetchInterestingFunnel(site, dashboardState) {+function fetchFeaturedFunnel(site, dashboardState) {return api.post(- url.apiPath(site, '/exploration/interesting-funnel'),+ url.apiPath(site, '/exploration/featured-funnel'),dashboardState,{ max_steps: PRELOAD_MAX_STEPS, max_candidates: PRELOAD_MAX_CANDIDATES })@@ -444,7 +450,28 @@ function VisitorsMetric({ visitors }) {}}-function ColumnEmptyState({ active, filter, colIndex, direction }) {+function ColumnEmptyState({+ active,+ filter,+ colIndex,+ direction,+ rateLimited,+ onRetry+}) {+ if (active && rateLimited) {+ return (+ <span>+ Too many requests, please wait a moment and{' '}+ <button+ onClick={onRetry}+ className="underline hover:text-gray-600 dark:hover:text-gray-300 focus:outline-none"+ >+ try again+ </button>+ </span>+ )+ }+if (!active) {const prompt =colIndex === 1@@ -493,7 +520,9 @@ function ExplorationColumn({maxVisitors,filter,onFilterChange,- onSelect+ onSelect,+ rateLimited,+ onRetry}) {const debouncedFilterChange = useDebounce((e) =>onFilterChange(e.target.value)@@ -559,6 +588,8 @@ function ExplorationColumn({filter={filter}colIndex={colIndex}direction={direction}+ rateLimited={rateLimited}+ onRetry={onRetry}/></div>) : (@@ -604,6 +635,7 @@ function provisionalEntry(step, columnIndex, sourceResults, existingFunnel) {function useExplorationData(site, dashboardState, inViewport) {const [state, setState] = useState(EMPTY_JOURNEY_STATE)const [activeLoading, setActiveLoading] = useState(false)+ const [retryCount, setRetryCount] = useState(0)// Incremented whenever the dashboardState or site changes so that// PathConnectors re-runs its layout effect and recalculates connector// geometry against the freshly rendered DOM. Steps alone do not change@@ -643,7 +675,8 @@ function useExplorationData(site, dashboardState, inViewport) {activeResults: [],activeFilter: '',frozen: truncateFrozenAt(prev.frozen, columnIndex),- provisional: {}+ provisional: {},+ rateLimited: false}}@@ -672,13 +705,15 @@ function useExplorationData(site, dashboardState, inViewport) {columnIndex,sourceResults,prev.funnel- )+ ),+ rateLimited: false}})}, [])const reset = useCallback(() => {++journeyVersionRef.current+ setActiveLoading(true)setState(EMPTY_JOURNEY_STATE)}, [])@@ -732,13 +767,13 @@ function useExplorationData(site, dashboardState, inViewport) {setActiveLoading(true)- // On first render fire the interesting-funnel preload. Once the preload+ // On first render fire the featured-funnel preload. Once the preload// resolves it sets steps and funnel, which re-triggers this effect for// the active-column candidate fetch.if (!preloadFiredRef.current) {preloadFiredRef.current = true- fetchInterestingFunnel(site, dashboardState)+ fetchFeaturedFunnel(site, dashboardState).then((response) => {if (isStale()) returnif (response?.funnel?.length > 0) {@@ -747,12 +782,13 @@ function useExplorationData(site, dashboardState, inViewport) {...prev,steps: response.funnel.map(({ step }) => step),funnel: response.funnel,- frozen: response.candidates ?? {}+ frozen: response.candidates ?? {},+ rateLimited: false}))// The preload populates steps, which re-triggers this effect for// the active-column candidate fetch, so leave loading=true.} else {- // No interesting funnel found; fall back to plain candidates for column 0.+ // No featured funnel found; fall back to plain candidates for column 0.fetchNextWithFunnel(site,dashboardState,@@ -765,20 +801,39 @@ function useExplorationData(site, dashboardState, inViewport) {if (!isStale())setState((prev) => ({...prev,- activeResults: r?.next ?? []+ activeResults: r?.next ?? [],+ rateLimited: false}))})- .catch(() => {- if (!isStale())- setState((prev) => ({ ...prev, activeResults: [] }))+ .catch((err) => {+ if (!isStale()) {+ if (isRateLimitedError(err)) {+ setState((prev) => ({+ ...prev,+ rateLimited: true,+ activeResults: []+ }))+ } else {+ setState((prev) => ({ ...prev, activeResults: [] }))+ }+ }}).finally(() => {if (!isStale()) setActiveLoading(false)})}})- .catch(() => {+ .catch((err) => {if (isStale()) return+ if (isRateLimitedError(err)) {+ setState((prev) => ({+ ...prev,+ rateLimited: true,+ activeResults: []+ }))+ setActiveLoading(false)+ return+ }fetchNextWithFunnel(site,dashboardState,@@ -789,11 +844,24 @@ function useExplorationData(site, dashboardState, inViewport) {).then((r) => {if (!isStale())- setState((prev) => ({ ...prev, activeResults: r?.next ?? [] }))+ setState((prev) => ({+ ...prev,+ activeResults: r?.next ?? [],+ rateLimited: false+ }))})- .catch(() => {- if (!isStale())- setState((prev) => ({ ...prev, activeResults: [] }))+ .catch((err) => {+ if (!isStale()) {+ if (isRateLimitedError(err)) {+ setState((prev) => ({+ ...prev,+ rateLimited: true,+ activeResults: []+ }))+ } else {+ setState((prev) => ({ ...prev, activeResults: [] }))+ }+ }}).finally(() => {if (!isStale()) setActiveLoading(false)@@ -827,7 +895,11 @@ function useExplorationData(site, dashboardState, inViewport) {.then((response) => {if (isStale()) returnsetState((prev) => {- const next = { ...prev, activeResults: response?.next ?? [] }+ const next = {+ ...prev,+ activeResults: response?.next ?? [],+ rateLimited: false+ }if (includeFunnel) {let newFunnel = response?.funnel ?? []next.provisional = {}@@ -870,29 +942,52 @@ function useExplorationData(site, dashboardState, inViewport) {return next})})- .catch(() => {+ .catch((err) => {if (isStale()) return- setState((prev) => ({- ...prev,- activeResults: [],- ...(includeFunnel ? { funnel: [] } : {})- }))+ if (isRateLimitedError(err)) {+ setState((prev) => ({+ ...prev,+ rateLimited: true,+ activeResults: [],+ ...(includeFunnel ? { provisional: {} } : {})+ }))+ } else {+ setState((prev) => ({+ ...prev,+ activeResults: [],+ ...(includeFunnel ? { funnel: [] } : {})+ }))+ }}).finally(() => {if (!isStale()) setActiveLoading(false)})- }, [site, dashboardState, state.steps, state.activeFilter, inViewport])+ }, [+ site,+ dashboardState,+ state.steps,+ state.activeFilter,+ inViewport,+ retryCount+ ])// direction is intentionally excluded from the dep array. It lives in a ref// and resets state, which does appear above, so the state update itself// drives the re-run without double-firing.+ const retry = useCallback(() => {+ setState((prev) => ({ ...prev, rateLimited: false }))+ setRetryCount((c) => c + 1)+ }, [])+return {state,direction: directionRef.current,activeLoading,layoutKey,+ rateLimited: state.rateLimited,selectStep,reset,+ retry,setDirection,setActiveFilter}@@ -940,8 +1035,10 @@ export function FunnelExploration() {direction,activeLoading,layoutKey,+ rateLimited,selectStep,reset,+ retry,setDirection,setActiveFilter} = useExplorationData(site, dashboardState, inViewport)@@ -971,7 +1068,8 @@ export function FunnelExploration() {steps.length === 0 &&funnel.length === 0 &&activeResults.length === 0 &&- !activeFilter+ !activeFilter &&+ !rateLimitedconst lastFunnelStep = funnel.length >= 2 ? funnel[funnel.length - 1] : nullconst overallConversionRate = lastFunnelStep?.conversion_rate ?? null@@ -1069,6 +1167,8 @@ export function FunnelExploration() {filter={isActive ? activeFilter : ''}onFilterChange={isActive ? setActiveFilter : () => {}}onSelect={(step) => selectStep(i, step)}+ rateLimited={isActive && rateLimited}+ onRetry={retry}/>)})}diff --git a/assets/js/dashboard/segments/segment-modals.tsx b/assets/js/dashboard/segments/segment-modals.tsxindex 63cd230038..30e9175dc8 100644--- a/assets/js/dashboard/segments/segment-modals.tsx+++ b/assets/js/dashboard/segments/segment-modals.tsx@@ -462,9 +462,13 @@ export const SegmentModal = ({ id }: { id: SavedSegment['id'] }) => {let error: ApiError | null = nullif (!segment) {- error = new ApiError(`Segment not found with with ID "${id}"`, {- error: `Segment not found with with ID "${id}"`- })+ error = new ApiError(+ `Segment not found with with ID "${id}"`,+ {+ error: `Segment not found with with ID "${id}"`+ },+ 404+ )}const data = !error ? segment : nulldiff --git a/extra/lib/plausible/stats/exploration.ex b/extra/lib/plausible/stats/exploration.exindex 287499953d..667c999056 100644--- a/extra/lib/plausible/stats/exploration.ex+++ b/extra/lib/plausible/stats/exploration.ex@@ -3,50 +3,11 @@ defmodule Plausible.Stats.Exploration doQuery logic for user journey exploration."""- defmodule Journey.Step do- @moduledoc false-- @type t() :: %__MODULE__{}-- @derive {Jason.Encoder,- only: [:name, :pathname, :label, :includes_subpaths, :subpaths_count, :is_goal]}- defstruct name: nil,- pathname: "",- label: nil,- includes_subpaths: false,- subpaths_count: 0,- is_goal: false-- @spec from(map()) :: t()- def from(step) do- new(step.name, step.pathname, step.includes_subpaths, step.subpaths_count, step.is_goal)- end-- @spec new(String.t(), String.t(), boolean(), non_neg_integer(), boolean()) :: t()- def new(name, pathname, includes_subpaths \\ false, subpaths_count \\ 0, is_goal \\ false)- when is_boolean(includes_subpaths) and is_integer(subpaths_count) do- label =- if name != "pageview" do- name- else- pathname- end-- %__MODULE__{- label: label,- name: name,- pathname: pathname,- includes_subpaths: includes_subpaths,- subpaths_count: subpaths_count,- is_goal: is_goal- }- end- end-import Ecto.Queryimport Plausible.Stats.SQL.Fragmentsimport Plausible.Stats.Util, only: [percentage: 2]+ alias Plausible.Stats.Exploration.Journeyalias Plausible.ClickhouseRepoalias Plausible.Stats.Basealias Plausible.Stats.Filters@@ -137,7 +98,7 @@ defmodule Plausible.Stats.Exploration do@doc """Builds a "teaser" funnel by greedily selecting steps.- We currently don't know what the "interesting" funnel might be,+ We currently don't know what the "featured" funnel might be,but blindly following the most visited cascade, oftentimes results witha repetitive back and forth between two pages.@@ -156,9 +117,9 @@ defmodule Plausible.Stats.Exploration doto include implicit wildcard pathnames in suggestions or not(default: true)"""- @spec interesting_funnel(Plausible.Site.t(), Query.t(), keyword()) ::+ @spec featured_funnel(Plausible.Site.t(), Query.t(), keyword()) ::{:ok, %{funnel: [funnel_step()], candidates: [next_step()]}} | {:error, :not_found}- def interesting_funnel(site, query, opts \\ []) do+ def featured_funnel(site, query, opts \\ []) domax_steps = min(Keyword.get(opts, :max_steps, 6), @max_steps)max_candidates = min(Keyword.get(opts, :max_candidates, 10), @max_candidates)@@ -170,13 +131,13 @@ defmodule Plausible.Stats.Exploration do)with {:ok, result} <-- build_interesting_journey(site, query, max_steps, max_candidates, include_wildcard?),+ build_featured_journey(site, query, max_steps, max_candidates, include_wildcard?),{:ok, funnel} <- journey_funnel(query, result.journey) do{:ok, %{funnel: funnel, candidates: result.candidates}}endend- defp build_interesting_journey(site, query, max_steps, max_candidates, include_wildcard?) do+ defp build_featured_journey(site, query, max_steps, max_candidates, include_wildcard?) docase do_build_journey(site,query,diff --git a/extra/lib/plausible/stats/journey/step.ex b/extra/lib/plausible/stats/journey/step.exnew file mode 100644index 0000000000..3ee3ede69e--- /dev/null+++ b/extra/lib/plausible/stats/journey/step.ex@@ -0,0 +1,39 @@+defmodule Plausible.Stats.Exploration.Journey.Step do+ @moduledoc false++ @type t() :: %__MODULE__{}++ @derive {Jason.Encoder,+ only: [:name, :pathname, :label, :includes_subpaths, :subpaths_count, :is_goal]}+ defstruct name: nil,+ pathname: "",+ label: nil,+ includes_subpaths: false,+ subpaths_count: 0,+ is_goal: false++ @spec from(map()) :: t()+ def from(step) do+ new(step.name, step.pathname, step.includes_subpaths, step.subpaths_count, step.is_goal)+ end++ @spec new(String.t(), String.t(), boolean(), non_neg_integer(), boolean()) :: t()+ def new(name, pathname, includes_subpaths \\ false, subpaths_count \\ 0, is_goal \\ false)+ when is_boolean(includes_subpaths) and is_integer(subpaths_count) do+ label =+ if name != "pageview" do+ name+ else+ pathname+ end++ %__MODULE__{+ label: label,+ name: name,+ pathname: pathname,+ includes_subpaths: includes_subpaths,+ subpaths_count: subpaths_count,+ is_goal: is_goal+ }+ end+enddiff --git a/lib/plausible_web/controllers/api/helpers.ex b/lib/plausible_web/controllers/api/helpers.exindex 109e3a1263..2e8bb0162a 100644--- a/lib/plausible_web/controllers/api/helpers.ex+++ b/lib/plausible_web/controllers/api/helpers.ex@@ -1,4 +1,8 @@defmodule PlausibleWeb.Api.Helpers do+ @moduledoc """+ Helper functions for API controllers.+ """+import Plug.Conndef unauthorized(conn, msg) do@@ -15,10 +19,12 @@ defmodule PlausibleWeb.Api.Helpers do|> halt()end- def bad_request(conn, msg) do+ def bad_request(conn, msg, extra \\ %{}) do+ payload = Map.merge(extra, %{error: msg})+conn|> put_status(400)- |> Phoenix.Controller.json(%{error: msg})+ |> Phoenix.Controller.json(payload)|> halt()enddiff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.exindex 8d723d33e7..eebc398dd7 100644--- a/lib/plausible_web/controllers/api/stats_controller.ex+++ b/lib/plausible_web/controllers/api/stats_controller.ex@@ -30,7 +30,7 @@ defmodule PlausibleWeb.Api.StatsController do:exploration_next,:exploration_funnel,:exploration_next_with_funnel,- :exploration_interesting_funnel+ :exploration_featured_funnel]end@@ -52,7 +52,7 @@ defmodule PlausibleWeb.Api.StatsController dojson(conn, Plausible.Stats.query(site, query))else- {:error, %QueryError{message: message}} -> bad_request(conn, message)+ {:error, %QueryError{message: message}} -> H.bad_request(conn, message)endend@@ -140,12 +140,28 @@ defmodule PlausibleWeb.Api.StatsController doalias Plausible.Stats.Exploration@exploration_wildcard_disabled_flag :exploration_wildcard_disabled+ @exploration_hourly_limit 600+ @exploration_burst_limit 10++ defp check_exploration_rate_limit(site) do+ key = "exploration:#{site.id}"++ with {:allow, _} <-+ Plausible.RateLimit.check_rate(key, :timer.hours(1), @exploration_hourly_limit),+ {:allow, _} <-+ Plausible.RateLimit.check_rate(key, :timer.seconds(10), @exploration_burst_limit) do+ :ok+ else+ {:deny, _} -> {:error, :rate_limit}+ end+ enddef exploration_next(conn, %{"journey" => steps} = params) dosite = conn.assigns.sitesearch_term = params["search_term"] || ""- with {:ok, journey} <- parse_journey(steps),+ with :ok <- check_exploration_rate_limit(site),+ {:ok, journey} <- parse_journey(steps),{:ok, direction} <- parse_exploration_direction(params["direction"]),query = Query.from(site, params, debug_metadata: debug_metadata(conn)),include_wildcard? =@@ -158,43 +174,57 @@ defmodule PlausibleWeb.Api.StatsController do) dojson(conn, next_steps)else+ {:error, :rate_limit} ->+ H.too_many_requests(conn, "Too many exploration requests")+{:error, :journey_too_long} ->- bad_request(conn, "The journey is too long")+ H.bad_request(conn, "The journey is too long")endenddef exploration_funnel(conn, %{"journey" => steps} = params) dosite = conn.assigns.site- with {:ok, journey} <- parse_journey(steps),+ with :ok <- check_exploration_rate_limit(site),+ {:ok, journey} <- parse_journey(steps),{:ok, direction} <- parse_exploration_direction(params["direction"]),query = Query.from(site, params, debug_metadata: debug_metadata(conn)),{:ok, funnel} <-Exploration.journey_funnel(query, journey, direction) dojson(conn, funnel)else+ {:error, :rate_limit} ->+ H.too_many_requests(conn, "Too many exploration requests")+{:error, :empty_journey} ->- bad_request(conn, "We are unable to show funnels when journey is empty")+ H.bad_request(conn, "We are unable to show funnels when journey is empty"){:error, :journey_too_long} ->- bad_request(conn, "The journey is too long")+ H.bad_request(conn, "The journey is too long")endend- def exploration_interesting_funnel(conn, params) do+ def exploration_featured_funnel(conn, params) dosite = conn.assigns.site- query = Query.from(site, params, debug_metadata: debug_metadata(conn))- include_wildcard? =- not FunWithFlags.enabled?(@exploration_wildcard_disabled_flag, for: site)+ case check_exploration_rate_limit(site) do+ :ok ->+ query = Query.from(site, params, debug_metadata: debug_metadata(conn))++ include_wildcard? =+ not FunWithFlags.enabled?(@exploration_wildcard_disabled_flag, for: site)- case Exploration.interesting_funnel(site, query,- max_steps: params["max_steps"],- max_candidates: params["max_candidates"],- include_wildcard?: include_wildcard?- ) do- {:ok, funnel_and_candidates} -> json(conn, funnel_and_candidates)- {:error, :not_found} -> json(conn, [])+ case Exploration.featured_funnel(site, query,+ max_steps: params["max_steps"],+ max_candidates: params["max_candidates"],+ include_wildcard?: include_wildcard?+ ) do+ {:ok, funnel_and_candidates} -> json(conn, funnel_and_candidates)+ {:error, :not_found} -> json(conn, [])+ end++ {:error, :rate_limit} ->+ H.too_many_requests(conn, "Too many exploration requests")endend@@ -203,7 +233,8 @@ defmodule PlausibleWeb.Api.StatsController dosearch_term = params["search_term"] || ""include_funnel? = params["include_funnel"] == true- with {:ok, journey} <- parse_journey(steps),+ with :ok <- check_exploration_rate_limit(site),+ {:ok, journey} <- parse_journey(steps),{:ok, direction} <- parse_exploration_direction(params["direction"]),query = Query.from(site, params, debug_metadata: debug_metadata(conn)),include_wildcard? =@@ -217,8 +248,11 @@ defmodule PlausibleWeb.Api.StatsController dofunnel <- maybe_include_funnel(include_funnel?, query, journey, direction) dojson(conn, %{next: next_steps, funnel: funnel})else+ {:error, :rate_limit} ->+ H.too_many_requests(conn, "Too many exploration requests")+_ ->- bad_request(conn, "There was an error with your request")+ H.bad_request(conn, "There was an error with your request")endend@@ -271,7 +305,7 @@ defmodule PlausibleWeb.Api.StatsController dojson(conn, funnel)else{:error, {:invalid_funnel_query, due_to}} ->- bad_request(+ H.bad_request(conn,"We are unable to show funnels when the dashboard is filtered by #{due_to}",%{@@ -292,7 +326,7 @@ defmodule PlausibleWeb.Api.StatsController do)_ ->- bad_request(conn, "There was an error with your request")+ H.bad_request(conn, "There was an error with your request")endend@@ -1326,7 +1360,7 @@ defmodule PlausibleWeb.Api.StatsController dodefp date_validation_plug(conn, _opts) docase parse_date_params(conn.params) do{:ok, _dates} -> conn- {:error, message} when is_binary(message) -> bad_request(conn, message)+ {:error, message} when is_binary(message) -> H.bad_request(conn, message)endend@@ -1343,7 +1377,7 @@ defmodule PlausibleWeb.Api.StatsController doconn:error ->- bad_request(+ H.bad_request(conn,"The first filter must be for the segment with id #{segment_id}")@@ -1398,15 +1432,6 @@ defmodule PlausibleWeb.Api.StatsController doend)end- defp bad_request(conn, message, extra \\ %{}) do- payload = Map.merge(extra, %{error: message})-- conn- |> put_status(400)- |> json(payload)- |> halt()- end-def comparison_query(query) doif query.include.compare doComparisons.get_comparison_query(query)diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.exindex dbf2882e8a..b8213b3a9a 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -278,9 +278,9 @@ defmodule PlausibleWeb.Router doStatsController,:exploration_next_with_funnel- post "/:domain/exploration/interesting-funnel",+ post "/:domain/exploration/featured-funnel",StatsController,- :exploration_interesting_funnel+ :exploration_featured_funnelendscope private: %{allow_consolidated_views: true} dodiff --git a/test/plausible/stats/exploration_test.exs b/test/plausible/stats/exploration_test.exsindex 16746f8663..25c37e0b4b 100644--- a/test/plausible/stats/exploration_test.exs+++ b/test/plausible/stats/exploration_test.exs@@ -346,12 +346,12 @@ defmodule Plausible.Stats.ExplorationTest doendend- describe "interesting_funnel" do+ describe "featured_funnel" dotest "builds a funnel starting with the most visited step", %{site: site} doquery = QueryBuilder.build!(site, input_date_range: :all)assert {:ok, %{funnel: [step1, step2, step3, step4]}} =- Exploration.interesting_funnel(site, query)+ Exploration.featured_funnel(site, query)assert step1.step.pathname == "/home"assert step1.visitors == 2@@ -370,7 +370,7 @@ defmodule Plausible.Stats.ExplorationTest doquery = QueryBuilder.build!(site, input_date_range: :all)assert {:ok, %{funnel: [step1, step2]}} =- Exploration.interesting_funnel(site, query, max_steps: 2)+ Exploration.featured_funnel(site, query, max_steps: 2)assert step1.step.pathname == "/home"assert step2.step.pathname == "/login"@@ -380,7 +380,7 @@ defmodule Plausible.Stats.ExplorationTest doempty_site = new_site()query = QueryBuilder.build!(empty_site, input_date_range: :all)- assert {:error, :not_found} = Exploration.interesting_funnel(empty_site, query)+ assert {:error, :not_found} = Exploration.featured_funnel(empty_site, query)endtest "stops when no more unseen steps are available" do@@ -398,7 +398,7 @@ defmodule Plausible.Stats.ExplorationTest doquery = QueryBuilder.build!(site, input_date_range: :all)assert {:ok, %{funnel: [step1]}} =- Exploration.interesting_funnel(site, query, max_steps: 6)+ Exploration.featured_funnel(site, query, max_steps: 6)assert step1.step.pathname == "/only-page"assert step1.visitors == 1@@ -411,7 +411,7 @@ defmodule Plausible.Stats.ExplorationTest dofilters: [[:is, "visit:browser", ["Firefox"]]])- assert {:ok, result} = Exploration.interesting_funnel(site, query)+ assert {:ok, result} = Exploration.featured_funnel(site, query)pathnames = Enum.map(result.funnel, & &1.step.pathname)assert pathnames == ["/docs", "/logout"]@@ -468,7 +468,7 @@ defmodule Plausible.Stats.ExplorationTest doquery = QueryBuilder.build!(site, input_date_range: :all)- assert {:ok, result} = Exploration.interesting_funnel(site, query)+ assert {:ok, result} = Exploration.featured_funnel(site, query)pathnames = Enum.map(result.funnel, & &1.step.pathname)assert pathnames == ["/a", "/b", "/c"]diff --git a/test/plausible_web/controllers/api/stats_controller/exploration_test.exs b/test/plausible_web/controllers/api/stats_controller/exploration_test.exsindex 589a0cc99c..b69617f2a9 100644--- a/test/plausible_web/controllers/api/stats_controller/exploration_test.exs+++ b/test/plausible_web/controllers/api/stats_controller/exploration_test.exs@@ -438,5 +438,86 @@ defmodule PlausibleWeb.Api.StatsController.ExplorationTest doassert resp["funnel"] == []endend++ describe "rate limiting" do+ defp exhaust_exploration_rate_limit(site) do+ Plausible.RateLimit.check_rate(+ Plausible.RateLimit,+ "exploration:#{site.id}",+ :timer.hours(1),+ 600,+ 601+ )+ end++ test "returns 429 for exploration_next when rate limit exceeded", %{+ conn: conn,+ site: site+ } do+ exhaust_exploration_rate_limit(site)++ journey = Jason.encode!([Journey.Step.new("pageview", "/home")])++ resp =+ conn+ |> post("/api/stats/#{site.domain}/exploration/next/", %{+ "journey" => journey,+ "period" => "all"+ })++ assert json_response(resp, 429) == %{"error" => "Too many exploration requests"}+ end++ test "returns 429 for exploration_funnel when rate limit exceeded", %{+ conn: conn,+ site: site+ } do+ exhaust_exploration_rate_limit(site)++ journey = Jason.encode!([Journey.Step.new("pageview", "/home")])++ resp =+ conn+ |> post("/api/stats/#{site.domain}/exploration/funnel/", %{+ "journey" => journey,+ "period" => "all"+ })++ assert json_response(resp, 429) == %{"error" => "Too many exploration requests"}+ end++ test "returns 429 for exploration_featured_funnel when rate limit exceeded", %{+ conn: conn,+ site: site+ } do+ exhaust_exploration_rate_limit(site)++ resp =+ conn+ |> post("/api/stats/#{site.domain}/exploration/featured-funnel/", %{+ "period" => "all"+ })++ assert json_response(resp, 429) == %{"error" => "Too many exploration requests"}+ end++ test "returns 429 for exploration_next_with_funnel when rate limit exceeded", %{+ conn: conn,+ site: site+ } do+ exhaust_exploration_rate_limit(site)++ journey = Jason.encode!([Journey.Step.new("pageview", "/home")])++ resp =+ conn+ |> post("/api/stats/#{site.domain}/exploration/next-with-funnel/", %{+ "journey" => journey,+ "period" => "all"+ })++ assert json_response(resp, 429) == %{"error" => "Too many exploration requests"}+ end+ endendend