feat (api): update character activity and api to allow date range (#299)
Some checks are pending
Build / 🚀 Deploy to test env (fly.io) (push) Waiting to run
Build / Manual Approval (push) Blocked by required conditions
Build / 🛠 Build (1.17, 18.x, 27) (push) Blocked by required conditions
Build / 🛠 Build Docker Images (linux/amd64) (push) Blocked by required conditions
Build / 🛠 Build Docker Images (linux/arm64) (push) Blocked by required conditions
Build / merge (push) Blocked by required conditions
Build / 🏷 Create Release (push) Blocked by required conditions

* feat (api): update character activity and api to allow date range
This commit is contained in:
guarzo
2025-03-21 11:05:48 -06:00
committed by GitHub
parent 999a702291
commit 06fef2296f
3 changed files with 99 additions and 21 deletions

View File

@@ -648,15 +648,17 @@ defmodule WandererAppWeb.MapAPIController do
Returns character activity data for a map.
Requires either `?map_id=<UUID>` or `?slug=<map-slug>`.
Optional `days` parameter to filter activity to a specific time period.
Example:
GET /api/map/character_activity?map_id=<uuid>
GET /api/map/character_activity?slug=<map-slug>
GET /api/map/character_activity?map_id=<uuid>&days=7
"""
@spec character_activity(Plug.Conn.t(), map()) :: Plug.Conn.t()
operation :character_activity,
summary: "Get Character Activity",
description: "Returns character activity data for a map. Requires either 'map_id' or 'slug' as a query parameter to identify the map.",
description: "Returns character activity data for a map. If days parameter is provided, filters activity to that time period, otherwise returns all activity. Requires either 'map_id' or 'slug' as a query parameter to identify the map.",
parameters: [
map_id: [
in: :query,
@@ -671,6 +673,13 @@ defmodule WandererAppWeb.MapAPIController do
type: :string,
required: false,
example: "map-name"
],
days: [
in: :query,
description: "Optional: Number of days to look back for activity data. If not provided, returns all activity history.",
type: :integer,
required: false,
example: "7"
]
],
responses: [
@@ -691,9 +700,10 @@ defmodule WandererAppWeb.MapAPIController do
}}
]
def character_activity(conn, params) do
with {:ok, map_id} <- Util.fetch_map_id(params) do
# Get raw activity data directly from the Map module instead of the Activity processor
raw_activity = WandererApp.Map.get_character_activity(map_id)
with {:ok, map_id} <- Util.fetch_map_id(params),
{:ok, days} <- parse_days(params["days"]) do
# Get raw activity data (filtered by days if provided, otherwise all activity)
raw_activity = WandererApp.Map.get_character_activity(map_id, days)
# Group activities by user_id and summarize
summarized_result =
@@ -744,6 +754,15 @@ defmodule WandererAppWeb.MapAPIController do
end
end
# Parse days parameter, return nil if not provided to show all activity
defp parse_days(nil), do: {:ok, nil}
defp parse_days(days_str) do
case Integer.parse(days_str) do
{days, ""} when days > 0 -> {:ok, days}
_ -> {:ok, nil} # Return nil if invalid to show all activity
end
end
# If hours_str is present and valid, parse it. Otherwise return nil (no filter).
defp parse_hours_ago(nil), do: nil
defp parse_hours_ago(hours_str) do