mirror of
https://github.com/wanderer-industries/wanderer
synced 2025-12-12 18:56:01 +00:00
feat(Api): added map audit base API. Added comments server validations.
This commit is contained in:
172
lib/wanderer_app_web/controllers/map_audit_api_controller.ex
Normal file
172
lib/wanderer_app_web/controllers/map_audit_api_controller.ex
Normal file
@@ -0,0 +1,172 @@
|
||||
defmodule WandererAppWeb.MapAuditAPIController do
|
||||
use WandererAppWeb, :controller
|
||||
use OpenApiSpex.ControllerSpecs
|
||||
|
||||
import Ash.Query, only: [filter: 2]
|
||||
require Logger
|
||||
|
||||
alias WandererApp.Api
|
||||
alias WandererApp.Api.Character
|
||||
alias WandererApp.MapSystemRepo
|
||||
alias WandererApp.MapCharacterSettingsRepo
|
||||
|
||||
alias WandererApp.Zkb.KillsProvider.KillsCache
|
||||
|
||||
alias WandererAppWeb.UtilAPIController, as: Util
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Inline Schemas
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
@character_schema %OpenApiSpex.Schema{
|
||||
type: :object,
|
||||
properties: %{
|
||||
eve_id: %OpenApiSpex.Schema{type: :string},
|
||||
name: %OpenApiSpex.Schema{type: :string},
|
||||
corporation_id: %OpenApiSpex.Schema{type: :string},
|
||||
corporation_ticker: %OpenApiSpex.Schema{type: :string},
|
||||
alliance_id: %OpenApiSpex.Schema{type: :string},
|
||||
alliance_ticker: %OpenApiSpex.Schema{type: :string}
|
||||
},
|
||||
required: ["eve_id", "name"]
|
||||
}
|
||||
|
||||
@map_audit_event_schema %OpenApiSpex.Schema{
|
||||
type: :object,
|
||||
properties: %{
|
||||
entity_type: %OpenApiSpex.Schema{type: :string},
|
||||
event_name: %OpenApiSpex.Schema{type: :string},
|
||||
event_data: %OpenApiSpex.Schema{type: :string},
|
||||
character: @character_schema,
|
||||
inserted_at: %OpenApiSpex.Schema{type: :string, format: :date_time}
|
||||
},
|
||||
required: ["entity_type", "event_name", "event_data", "inserted_at"]
|
||||
}
|
||||
|
||||
@map_audit_response_schema %OpenApiSpex.Schema{
|
||||
type: :object,
|
||||
properties: %{
|
||||
data: %OpenApiSpex.Schema{
|
||||
type: :array,
|
||||
items: @map_audit_event_schema
|
||||
}
|
||||
},
|
||||
required: ["data"]
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# MAP endpoints
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
@doc """
|
||||
GET /api/map/audit
|
||||
|
||||
Requires either `?map_id=<UUID>` **OR** `?slug=<map-slug>` in the query params.
|
||||
|
||||
Examples:
|
||||
GET /api/map/audit?map_id=466e922b-e758-485e-9b86-afae06b88363&period=1H
|
||||
GET /api/map/audit?slug=my-unique-wormhole-map&period=1H
|
||||
"""
|
||||
@spec index(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
||||
operation(:index,
|
||||
summary: "List Map Audit events",
|
||||
description:
|
||||
"Lists all audit events for a map. Requires either 'map_id' or 'slug' as a query parameter to identify the map.",
|
||||
parameters: [
|
||||
map_id: [
|
||||
in: :query,
|
||||
description: "Map identifier (UUID) - Either map_id or slug must be provided",
|
||||
type: :string,
|
||||
required: false,
|
||||
example: ""
|
||||
],
|
||||
slug: [
|
||||
in: :query,
|
||||
description: "Map slug - Either map_id or slug must be provided",
|
||||
type: :string,
|
||||
required: false,
|
||||
example: "map-name"
|
||||
],
|
||||
period: [
|
||||
in: :query,
|
||||
description: "Activity period (1H, 1D, 1W, 1M, 2M, 3M)",
|
||||
type: :string,
|
||||
required: true,
|
||||
example: "1D"
|
||||
]
|
||||
],
|
||||
responses: [
|
||||
ok: {
|
||||
"List of map audit events",
|
||||
"application/json",
|
||||
@map_audit_response_schema
|
||||
},
|
||||
bad_request:
|
||||
{"Error", "application/json",
|
||||
%OpenApiSpex.Schema{
|
||||
type: :object,
|
||||
properties: %{
|
||||
error: %OpenApiSpex.Schema{type: :string}
|
||||
},
|
||||
required: ["error"],
|
||||
example: %{
|
||||
"error" => "Must provide either ?map_id=UUID or ?slug=SLUG"
|
||||
}
|
||||
}}
|
||||
]
|
||||
)
|
||||
|
||||
def index(conn, params) do
|
||||
with {:ok, map_id} <- Util.fetch_map_id(params),
|
||||
{:ok, period} <- Util.require_param(params, "period"),
|
||||
query <- WandererApp.Map.Audit.get_activity_query(map_id, period, "all"),
|
||||
{:ok, data} <-
|
||||
Api.read(query) do
|
||||
data = Enum.map(data, &map_audit_event_to_json/1)
|
||||
json(conn, %{data: data})
|
||||
else
|
||||
{:error, msg} when is_binary(msg) ->
|
||||
conn
|
||||
|> put_status(:bad_request)
|
||||
|> json(%{error: msg})
|
||||
|
||||
{:error, reason} ->
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> json(%{error: "Request failed: #{inspect(reason)}"})
|
||||
end
|
||||
end
|
||||
|
||||
defp map_audit_event_to_json(
|
||||
%{event_type: event_type, event_data: event_data, character: character} = event
|
||||
) do
|
||||
# Start with the basic system data
|
||||
result =
|
||||
Map.take(event, [
|
||||
:entity_type,
|
||||
:inserted_at
|
||||
])
|
||||
|
||||
result
|
||||
|> Map.put(:character, WandererAppWeb.MapEventHandler.map_ui_character_stat(character))
|
||||
|> Map.put(:event_name, WandererAppWeb.UserActivity.get_event_name(event_type))
|
||||
|> Map.put(
|
||||
:event_data,
|
||||
WandererAppWeb.UserActivity.get_event_data(
|
||||
event_type,
|
||||
Jason.decode!(event_data) |> Map.drop(["character_id"])
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
defp get_original_system_name(solar_system_id) do
|
||||
# Fetch the original system name from the MapSolarSystem resource
|
||||
case WandererApp.Api.MapSolarSystem.by_solar_system_id(solar_system_id) do
|
||||
{:ok, system} ->
|
||||
system.solar_system_name
|
||||
|
||||
_error ->
|
||||
"Unknown System"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user