mirror of
https://github.com/wanderer-industries/wanderer
synced 2026-09-22 21:16:11 +00:00
feat(Map): Ability to store/view audit logs up to 3 months
This commit is contained in:
@@ -9,20 +9,38 @@ defmodule WandererApp.Map.Audit do
|
||||
@logger Application.compile_env(:wanderer_app, :logger)
|
||||
|
||||
@week_seconds :timer.hours(24 * 7)
|
||||
@month_seconds @week_seconds * 4
|
||||
@audit_expired_seconds @month_seconds * 3
|
||||
|
||||
def track_map_subscription_event(event_type, metadata) do
|
||||
case event_type do
|
||||
"subscription.created" ->
|
||||
track_map_event(event_type, metadata)
|
||||
|
||||
"subscription.updated" ->
|
||||
track_map_event(event_type, metadata)
|
||||
|
||||
"subscription.deleted" ->
|
||||
track_map_event(event_type, metadata)
|
||||
|
||||
_ ->
|
||||
{:ok, nil}
|
||||
end
|
||||
end
|
||||
|
||||
def archive() do
|
||||
@logger.info("Start map audit arhiving...")
|
||||
Logger.info("Start map audit arhiving...")
|
||||
|
||||
WandererApp.Api.UserActivity
|
||||
|> Ash.Query.filter(inserted_at: [less_than: _get_expired_at()])
|
||||
|> Ash.Query.filter(inserted_at: [less_than: get_expired_at()])
|
||||
|> Ash.bulk_destroy!(:archive, %{}, batch_size: 100)
|
||||
|
||||
@logger.info(fn -> "Audit arhived" end)
|
||||
Logger.info(fn -> "Audit arhived" end)
|
||||
:ok
|
||||
end
|
||||
|
||||
def get_activity_page(map_id, page, per_page, period, activity) do
|
||||
{from, to} = period |> _get_period()
|
||||
{from, to} = period |> get_period()
|
||||
|
||||
query =
|
||||
WandererApp.Api.UserActivity
|
||||
@@ -84,52 +102,43 @@ defmodule WandererApp.Map.Audit do
|
||||
|
||||
def track_map_event(_event_type, _metadata), do: {:ok, nil}
|
||||
|
||||
defp _get_period("1H") do
|
||||
defp get_period("1H") do
|
||||
now = DateTime.utc_now()
|
||||
start_date = now |> DateTime.add(-1 * 3600, :second)
|
||||
{start_date, now}
|
||||
end
|
||||
|
||||
defp _get_period("1D") do
|
||||
defp get_period("1D") do
|
||||
now = DateTime.utc_now()
|
||||
start_date = now |> DateTime.add(-24 * 3600, :second)
|
||||
{start_date, now}
|
||||
end
|
||||
|
||||
defp _get_period("1W") do
|
||||
defp get_period("1W") do
|
||||
now = DateTime.utc_now()
|
||||
start_date = now |> DateTime.add(-24 * 3600 * 7, :second)
|
||||
{start_date, now}
|
||||
end
|
||||
|
||||
# defp _get_period("1M") do
|
||||
# now = DateTime.utc_now()
|
||||
# start_date = now |> DateTime.add(-24 * 3600 * 31, :second)
|
||||
# {start_date, now}
|
||||
# end
|
||||
|
||||
# defp _get_period("ALL") do
|
||||
# now = DateTime.utc_now()
|
||||
|
||||
# start_date = %{
|
||||
# now
|
||||
# | year: 2000,
|
||||
# month: 1,
|
||||
# day: 1,
|
||||
# hour: 00,
|
||||
# minute: 00,
|
||||
# second: 00,
|
||||
# microsecond: {0, 0}
|
||||
# }
|
||||
|
||||
# {start_date, now}
|
||||
# end
|
||||
|
||||
defp _get_period(_) do
|
||||
defp get_period("1M") do
|
||||
now = DateTime.utc_now()
|
||||
start_date = now |> DateTime.add(-1 * 3600, :second)
|
||||
start_date = now |> DateTime.add(-24 * 3600 * 31, :second)
|
||||
{start_date, now}
|
||||
end
|
||||
|
||||
defp _get_expired_at(), do: DateTime.utc_now() |> DateTime.add(-@week_seconds, :second)
|
||||
defp get_period("2M") do
|
||||
now = DateTime.utc_now()
|
||||
start_date = now |> DateTime.add(-24 * 3600 * 31 * 2, :second)
|
||||
{start_date, now}
|
||||
end
|
||||
|
||||
defp get_period("3M") do
|
||||
now = DateTime.utc_now()
|
||||
start_date = now |> DateTime.add(-24 * 3600 * 31 * 3, :second)
|
||||
{start_date, now}
|
||||
end
|
||||
|
||||
defp get_period(_), do: get_period("1H")
|
||||
|
||||
defp get_expired_at(), do: DateTime.utc_now() |> DateTime.add(-@audit_expired_seconds, :second)
|
||||
end
|
||||
|
||||
@@ -5,6 +5,8 @@ defmodule WandererAppWeb.MapAuditLive do
|
||||
|
||||
alias WandererAppWeb.UserActivity
|
||||
|
||||
@active_subscription_periods ["2M", "3M"]
|
||||
|
||||
def mount(
|
||||
%{"slug" => map_slug, "period" => period, "activity" => activity} = _params,
|
||||
_session,
|
||||
@@ -38,6 +40,7 @@ defmodule WandererAppWeb.MapAuditLive do
|
||||
map_id: map_id,
|
||||
map_name: map_name,
|
||||
map_slug: map_slug,
|
||||
map_subscription_active: WandererApp.Map.is_subscription_active?(map_id),
|
||||
activity: activity,
|
||||
can_undo_types: [:systems_removed],
|
||||
period: period || "1H",
|
||||
@@ -117,23 +120,26 @@ defmodule WandererAppWeb.MapAuditLive do
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("undo", %{"event-data" => event_data, "event-type" => "systems_removed"}, %{assigns: %{map_id: map_id, current_user: current_user}} = socket) do
|
||||
def handle_event(
|
||||
"undo",
|
||||
%{"event-data" => event_data, "event-type" => "systems_removed"},
|
||||
%{assigns: %{map_id: map_id, current_user: current_user}} = socket
|
||||
) do
|
||||
{:ok, %{"solar_system_ids" => solar_system_ids}} = Jason.decode(event_data)
|
||||
|
||||
solar_system_ids
|
||||
|> Enum.each(fn solar_system_id ->
|
||||
WandererApp.Map.Server.add_system(
|
||||
map_id,
|
||||
%{
|
||||
solar_system_id: solar_system_id,
|
||||
coordinates: nil,
|
||||
use_old_coordinates: true
|
||||
},
|
||||
current_user.id,
|
||||
nil
|
||||
)
|
||||
end)
|
||||
|
||||
|> Enum.each(fn solar_system_id ->
|
||||
WandererApp.Map.Server.add_system(
|
||||
map_id,
|
||||
%{
|
||||
solar_system_id: solar_system_id,
|
||||
coordinates: nil,
|
||||
use_old_coordinates: true
|
||||
},
|
||||
current_user.id,
|
||||
nil
|
||||
)
|
||||
end)
|
||||
|
||||
{:noreply, socket |> put_flash(:info, "Systems restored!")}
|
||||
end
|
||||
@@ -171,9 +177,18 @@ defmodule WandererAppWeb.MapAuditLive do
|
||||
end
|
||||
|
||||
defp load_activity(socket, new_page) when new_page >= 1 do
|
||||
%{activity: activity, per_page: per_page, page: cur_page, map_id: map_id, period: period} =
|
||||
%{
|
||||
activity: activity,
|
||||
per_page: per_page,
|
||||
page: cur_page,
|
||||
map_id: map_id,
|
||||
map_subscription_active: map_subscription_active,
|
||||
period: period
|
||||
} =
|
||||
socket.assigns
|
||||
|
||||
period = get_valid_period(period, map_subscription_active)
|
||||
|
||||
with {:ok, page} <-
|
||||
WandererApp.Map.Audit.get_activity_page(map_id, new_page, per_page, period, activity) do
|
||||
{activity, at, limit} =
|
||||
@@ -199,4 +214,14 @@ defmodule WandererAppWeb.MapAuditLive do
|
||||
_ -> socket
|
||||
end
|
||||
end
|
||||
|
||||
defp get_valid_period(period, true), do: period
|
||||
|
||||
defp get_valid_period(period, _map_subscription_active) do
|
||||
if period in @active_subscription_periods do
|
||||
"1H"
|
||||
else
|
||||
period
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -65,7 +65,8 @@
|
||||
>
|
||||
WEEK
|
||||
</button>
|
||||
<%!-- <button
|
||||
<button
|
||||
:if={@map_subscription_active}
|
||||
phx-click="set_period"
|
||||
phx-value-period="1M"
|
||||
class={[
|
||||
@@ -73,8 +74,31 @@
|
||||
classes(" !bg-neutral-800 !text-white": @period == "1M")
|
||||
]}
|
||||
>
|
||||
M
|
||||
1M
|
||||
</button>
|
||||
<button
|
||||
:if={@map_subscription_active}
|
||||
phx-click="set_period"
|
||||
phx-value-period="2M"
|
||||
class={[
|
||||
"btn btn-sm join-item font-medium text-neutral bg-neutral-700",
|
||||
classes(" !bg-neutral-800 !text-white": @period == "2M")
|
||||
]}
|
||||
>
|
||||
2M
|
||||
</button>
|
||||
<button
|
||||
:if={@map_subscription_active}
|
||||
phx-click="set_period"
|
||||
phx-value-period="3M"
|
||||
class={[
|
||||
"btn btn-sm join-item font-medium text-neutral bg-neutral-700",
|
||||
classes(" !bg-neutral-800 !text-white": @period == "3M")
|
||||
]}
|
||||
>
|
||||
3M
|
||||
</button>
|
||||
<%!--
|
||||
<button
|
||||
phx-click="set_period"
|
||||
phx-value-period="ALL"
|
||||
|
||||
Reference in New Issue
Block a user