2022-04-28 12:24:29 +03:00
|
|
|
defimpl FunWithFlags.Actor, for: BitString do
|
|
|
|
def id(str) do
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.Api.ExternalController do
|
|
|
|
use PlausibleWeb, :controller
|
|
|
|
require Logger
|
|
|
|
|
2022-11-23 16:05:44 +03:00
|
|
|
alias Plausible.Ingestion
|
|
|
|
|
2019-10-31 06:49:46 +03:00
|
|
|
def event(conn, _params) do
|
2022-11-23 16:05:44 +03:00
|
|
|
with {:ok, request} <- Ingestion.Request.build(conn),
|
2023-07-24 15:46:57 +03:00
|
|
|
_ <- Sentry.Context.set_extra_context(%{request: request}) do
|
2022-11-23 16:05:44 +03:00
|
|
|
case Ingestion.Event.build_and_buffer(request) do
|
|
|
|
{:ok, %{dropped: [], buffered: _buffered}} ->
|
|
|
|
conn
|
|
|
|
|> put_status(202)
|
|
|
|
|> text("ok")
|
|
|
|
|
|
|
|
{:ok, %{dropped: dropped, buffered: _}} ->
|
|
|
|
first_invalid_changeset = find_first_invalid_changeset(dropped)
|
2022-08-16 14:43:10 +03:00
|
|
|
|
2022-11-23 16:05:44 +03:00
|
|
|
if first_invalid_changeset do
|
|
|
|
conn
|
|
|
|
|> put_resp_header("x-plausible-dropped", "#{Enum.count(dropped)}")
|
|
|
|
|> put_status(400)
|
2023-02-13 17:25:17 +03:00
|
|
|
|> json(%{errors: Plausible.ChangesetHelpers.traverse_errors(first_invalid_changeset)})
|
2022-11-23 16:05:44 +03:00
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> put_resp_header("x-plausible-dropped", "#{Enum.count(dropped)}")
|
|
|
|
|> put_status(202)
|
|
|
|
|> text("ok")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2022-11-28 17:50:55 +03:00
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
2021-11-18 11:36:16 +03:00
|
|
|
conn
|
|
|
|
|> put_status(400)
|
2023-02-13 17:25:17 +03:00
|
|
|
|> json(%{errors: Plausible.ChangesetHelpers.traverse_errors(changeset)})
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def error(conn, _params) do
|
2021-03-31 11:38:14 +03:00
|
|
|
Sentry.capture_message("JS snippet error")
|
2019-09-02 14:29:19 +03:00
|
|
|
send_resp(conn, 200, "")
|
|
|
|
end
|
|
|
|
|
2020-07-20 10:34:35 +03:00
|
|
|
def health(conn, _params) do
|
2020-07-21 09:58:00 +03:00
|
|
|
postgres_health =
|
|
|
|
case Ecto.Adapters.SQL.query(Plausible.Repo, "SELECT 1", []) do
|
|
|
|
{:ok, _} -> "ok"
|
|
|
|
e -> "error: #{inspect(e)}"
|
|
|
|
end
|
2020-07-20 10:34:35 +03:00
|
|
|
|
2020-07-21 09:58:00 +03:00
|
|
|
clickhouse_health =
|
2020-09-17 16:36:01 +03:00
|
|
|
case Ecto.Adapters.SQL.query(Plausible.ClickhouseRepo, "SELECT 1", []) do
|
2020-07-21 09:58:00 +03:00
|
|
|
{:ok, _} -> "ok"
|
|
|
|
e -> "error: #{inspect(e)}"
|
|
|
|
end
|
2020-07-20 10:34:35 +03:00
|
|
|
|
2024-02-12 16:55:20 +03:00
|
|
|
cache_health =
|
|
|
|
if postgres_health == "ok" and Plausible.Site.Cache.ready?() and
|
|
|
|
Plausible.Shield.IPRuleCache.ready?() do
|
2022-11-29 12:46:49 +03:00
|
|
|
"ok"
|
|
|
|
end
|
|
|
|
|
2020-07-21 09:58:00 +03:00
|
|
|
status =
|
2024-02-12 16:55:20 +03:00
|
|
|
case {postgres_health, clickhouse_health, cache_health} do
|
2022-11-29 12:46:49 +03:00
|
|
|
{"ok", "ok", "ok"} -> 200
|
2020-07-21 09:58:00 +03:00
|
|
|
_ -> 500
|
|
|
|
end
|
2020-07-20 10:34:35 +03:00
|
|
|
|
|
|
|
put_status(conn, status)
|
|
|
|
|> json(%{
|
|
|
|
postgres: postgres_health,
|
2022-11-29 12:46:49 +03:00
|
|
|
clickhouse: clickhouse_health,
|
2024-02-12 16:55:20 +03:00
|
|
|
sites_cache: cache_health
|
2020-07-20 10:34:35 +03:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-05-27 10:49:04 +03:00
|
|
|
def info(conn, _params) do
|
2022-10-18 18:11:30 +03:00
|
|
|
build =
|
|
|
|
:plausible
|
|
|
|
|> Application.get_env(:runtime_metadata)
|
|
|
|
|> Keyword.take([:version, :commit, :created, :tags])
|
|
|
|
|> Map.new()
|
2022-05-27 14:30:21 +03:00
|
|
|
|
2023-01-17 18:05:09 +03:00
|
|
|
geo_database = Plausible.Geo.database_type() || "(not configured)"
|
2022-05-27 10:49:04 +03:00
|
|
|
|
2022-10-18 18:11:30 +03:00
|
|
|
json(conn, %{
|
2022-05-27 15:51:24 +03:00
|
|
|
geo_database: geo_database,
|
2022-10-18 18:11:30 +03:00
|
|
|
build: build
|
|
|
|
})
|
2022-05-27 10:49:04 +03:00
|
|
|
end
|
2022-11-23 16:05:44 +03:00
|
|
|
|
|
|
|
defp find_first_invalid_changeset(dropped) do
|
|
|
|
Enum.find_value(dropped, nil, fn dropped_event ->
|
|
|
|
case dropped_event.drop_reason do
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} -> changeset
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|