Skip to content

Commit

Permalink
Logging/telemetry fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Teifion committed May 27, 2024
1 parent e089e3d commit 23baf39
Show file tree
Hide file tree
Showing 19 changed files with 520 additions and 77 deletions.
37 changes: 20 additions & 17 deletions lib/angen/helpers/fake_data/fake_logging.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ defmodule Angen.FakeData.FakeLogging do
"spectator" => rand_int(0, config.max_users / 2, 10),
"player" => rand_int(0, config.max_users / 3, 10),
}
|> add_total_key(:total_non_bot, [:bot])
|> add_total_key(:total_bot, [:non_bot])
|> add_bot_totals

lobby = %{
"in_progress" => rand_int(0, config.max_users / 2, 10),
Expand Down Expand Up @@ -168,40 +167,44 @@ defmodule Angen.FakeData.FakeLogging do
)
|> Enum.count

minutes = %{
"lobby" => rand_int(config.max_users * 20, config.max_users * 600, get_in(last_day, ~w(minutes lobby))),
"menu" => rand_int(config.max_users * 20, config.max_users * 600, get_in(last_day, ~w(minutes menu))),
"player" => rand_int(config.max_users * 20, config.max_users * 600, get_in(last_day, ~w(minutes player))),
"spectator" => rand_int(config.max_users * 20, config.max_users * 600, get_in(last_day, ~w(minutes spectator))),
"bot" => rand_int(config.max_users * 20, config.max_users * 600, get_in(last_day, ~w(minutes bot)))
}
|> add_bot_totals

%{
"average_user_counts" => %{
"lobby" => rand_int_sequence(config.max_users / 10, config.max_users / 3, 0, 24),
"menu" => rand_int_sequence(config.max_users / 10, config.max_users / 3, 0, 24),
"player" => rand_int_sequence(config.max_users / 10, config.max_users / 3, 0, 24),
"spectator" => rand_int_sequence(config.max_users / 10, config.max_users / 3, 0, 24),
"total" => rand_int_sequence(config.max_users / 10, config.max_users / 3, 0, 24)
},
"minutes" => add_total_key(%{
"lobby" => rand_int(config.max_users * 20, config.max_users * 600, get_in(last_day, ~w(minutes lobby))),
"menu" => rand_int(config.max_users * 20, config.max_users * 600, get_in(last_day, ~w(minutes menu))),
"player" => rand_int(config.max_users * 20, config.max_users * 600, get_in(last_day, ~w(minutes player))),
"spectator" => rand_int(config.max_users * 20, config.max_users * 600, get_in(last_day, ~w(minutes spectator)))
}, "total", []),
"peak_user_counts" => %{
"lobby" => rand_int_sequence(config.max_users / 10, config.max_users / 4, 0, 24),
"menu" => rand_int_sequence(config.max_users / 10, config.max_users / 4, 0, 24),
"player" => rand_int_sequence(config.max_users / 10, config.max_users / 4, 0, 24),
"spectator" => rand_int_sequence(config.max_users / 10, config.max_users / 4, 0, 24),
"total" => rand_int_sequence(config.max_users / 10, config.max_users / 4, 0, 24)
"total_inc_bot" => rand_int_sequence(config.max_users / 10, config.max_users / 3, 0, 24),
"total_non_bot" => rand_int_sequence(config.max_users / 10, config.max_users / 3, 0, 24)
},
"minutes" => minutes,
"peak_user_counts" => %{
"bot" => rand_int(10, config.max_users / 3, get_in(last_day, ~w(stats peak_user_counts bot))),
"lobby" => rand_int(10, config.max_users / 3, get_in(last_day, ~w(stats peak_user_counts lobby))),
"menu" => rand_int(10, config.max_users / 3, get_in(last_day, ~w(stats peak_user_counts menu))),
"player" => rand_int(10, config.max_users / 3, get_in(last_day, ~w(stats peak_user_counts player))),
"spectator" => rand_int(10, config.max_users / 3, get_in(last_day, ~w(stats peak_user_counts spectator))),
"total" => rand_int(10, config.max_users / 3, get_in(last_day, ~w(stats peak_user_counts total))),
},
} |> add_bot_totals,
"stats" => %{
"accounts_created" => accounts_created,
"unique_users" => rand_int(config.max_users / 10, config.max_users, get_in(last_day, ~w(stats unique_users))),
"unique_players" => rand_int(config.max_users / 12, config.max_users / 1.5, get_in(last_day, ~w(stats unique_users))),
}
}
end

defp add_bot_totals(m) do
m
|> add_total_key("total_non_bot", ["bot"])
|> add_total_key("total_inc_bot", ["total_non_bot"])
end
end
2 changes: 1 addition & 1 deletion lib/angen/helpers/styling_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Angen.Helper.StylingHelper do
def icon(:up), do: "fa-level-up"
def icon(:back), do: "fa-arrow-left"

def icon(:list, _fa_type), do: "fa-bars"
def icon(:list), do: "fa-bars"
def icon(:show), do: "fa-eye"
def icon(:search), do: "fa-search"
def icon(:new), do: "fa-plus"
Expand Down
3 changes: 2 additions & 1 deletion lib/angen/helpers/timex_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ defmodule Angen.Helper.TimexHelper do
Timex.compare(a, b) == -1
end

def represent_minutes(nil), do: ""
def represent_minutes(nil), do: "0 minutes"
def represent_minutes(0), do: "0 minutes"

def represent_minutes(s) do
now = Timex.now()
Expand Down
51 changes: 35 additions & 16 deletions lib/angen/logging/tasks/persist_server_day_task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Angen.Logging.PersistServerDayTask do

@log_keep_days 30

@client_states ~w(lobby bot menu player spectator total)a
@client_states ~w(lobby bot menu player spectator total_non_bot total_inc_bot)a

# [] List means 1 hour segments
# %{} Dict means total for the day for that key
Expand Down Expand Up @@ -39,7 +39,8 @@ defmodule Angen.Logging.PersistServerDayTask do
lobby: 0,
menu: 0,
bot: 0,
total: 0
total_non_bot: 0,
total_inc_bot: 0
},

# The number of minutes users (combined) spent in that state during the segment
Expand All @@ -49,15 +50,17 @@ defmodule Angen.Logging.PersistServerDayTask do
spectator: [],
lobby: [],
menu: [],
total: []
total_non_bot: [],
total_inc_bot: []
},
peak_user_counts: %{
bot: [],
player: [],
spectator: [],
lobby: [],
menu: [],
total: []
total_non_bot: [],
total_inc_bot: []
}
}

Expand All @@ -82,7 +85,8 @@ defmodule Angen.Logging.PersistServerDayTask do
spectator: 0,
lobby: 0,
menu: 0,
total: 0
total_non_bot: 0,
total_inc_bot: 0
},

# The number of minutes users (combined) spent in that state during the segment
Expand All @@ -92,15 +96,17 @@ defmodule Angen.Logging.PersistServerDayTask do
spectator: 0,
lobby: 0,
menu: 0,
total: 0
total_non_bot: 0,
total_inc_bot: 0
},
peak_user_counts: %{
bot: 0,
player: 0,
spectator: 0,
lobby: 0,
menu: 0,
total: 0
total_non_bot: 0,
total_inc_bot: 0
}
}

Expand Down Expand Up @@ -196,7 +202,7 @@ defmodule Angen.Logging.PersistServerDayTask do
search: [
after: start_time,
before: end_time,
node: node
# node: node
],
select: [:data],
limit: :infinity
Expand All @@ -208,6 +214,7 @@ defmodule Angen.Logging.PersistServerDayTask do
defp extend_segment(segment, logs) do
extend = calculate_segment_parts(logs)


%{
# Daily totals
stats: %{
Expand All @@ -222,7 +229,9 @@ defmodule Angen.Logging.PersistServerDayTask do
spectator: segment.minutes.spectator + extend.minutes.spectator,
lobby: segment.minutes.lobby + extend.minutes.lobby,
menu: segment.minutes.menu + extend.minutes.menu,
total: segment.minutes.total + extend.minutes.total
bot: segment.minutes.bot + extend.minutes.bot,
total_non_bot: segment.minutes.total_non_bot + extend.minutes.total_non_bot,
total_inc_bot: segment.minutes.total_inc_bot + extend.minutes.total_inc_bot
},

# The number of minutes users (combined) spent in that state during the segment
Expand All @@ -232,14 +241,18 @@ defmodule Angen.Logging.PersistServerDayTask do
segment.average_user_counts.spectator ++ [extend.average_user_counts.spectator],
lobby: segment.average_user_counts.lobby ++ [extend.average_user_counts.lobby],
menu: segment.average_user_counts.menu ++ [extend.average_user_counts.menu],
total: segment.average_user_counts.total ++ [extend.average_user_counts.total]
bot: segment.average_user_counts.bot ++ [extend.average_user_counts.bot],
total_non_bot: segment.average_user_counts.total_non_bot ++ [extend.average_user_counts.total_non_bot],
total_inc_bot: segment.average_user_counts.total_inc_bot ++ [extend.average_user_counts.total_inc_bot]
},
peak_user_counts: %{
player: segment.peak_user_counts.player ++ [extend.peak_user_counts.player],
spectator: segment.peak_user_counts.spectator ++ [extend.peak_user_counts.spectator],
lobby: segment.peak_user_counts.lobby ++ [extend.peak_user_counts.lobby],
menu: segment.peak_user_counts.menu ++ [extend.peak_user_counts.menu],
total: segment.peak_user_counts.total ++ [extend.peak_user_counts.total]
bot: segment.peak_user_counts.bot ++ [extend.peak_user_counts.bot],
total_non_bot: segment.peak_user_counts.total_non_bot ++ [extend.peak_user_counts.total_non_bot],
total_inc_bot: segment.peak_user_counts.total_inc_bot ++ [extend.peak_user_counts.total_inc_bot]
}
}
end
Expand All @@ -251,7 +264,8 @@ defmodule Angen.Logging.PersistServerDayTask do
count = Enum.count(logs)

empty_user_maps = %{
total: 0,
total_non_bot: 0,
total_inc_bot: 0,
bot: 0,
player: 0,
spectator: 0,
Expand All @@ -264,7 +278,8 @@ defmodule Angen.Logging.PersistServerDayTask do
logs
|> Enum.reduce(empty_user_maps, fn log, acc ->
%{
total: acc.total + Map.get(log["client"], "total", 0),
total_non_bot: acc.bot + Map.get(log["client"], "total_non_bot", 0),
total_inc_bot: acc.bot + Map.get(log["client"], "total_inc_bot", 0),
bot: acc.bot + Map.get(log["client"], "bot", 0),
player: acc.player + Map.get(log["client"], "player", 0),
spectator: acc.spectator + Map.get(log["client"], "spectator", 0),
Expand All @@ -291,15 +306,17 @@ defmodule Angen.Logging.PersistServerDayTask do
spectator: sum_counts(logs, ~w(client spectator)) / count,
lobby: sum_counts(logs, ~w(client lobby)) / count,
menu: sum_counts(logs, ~w(client menu)) / count,
total: sum_counts(logs, ~w(client total)) / count
total_inc_bot: sum_counts(logs, ~w(client total_inc_bot)) / count,
total_non_bot: sum_counts(logs, ~w(client total_non_bot)) / count
},
peak_user_counts: %{
bot: max_counts(logs, ~w(client bot)),
player: max_counts(logs, ~w(client player)),
spectator: max_counts(logs, ~w(client spectator)),
lobby: max_counts(logs, ~w(client lobby)),
menu: max_counts(logs, ~w(client menu)),
total: max_counts(logs, ~w(client total))
total_inc_bot: max_counts(logs, ~w(client total_inc_bot)),
total_non_bot: max_counts(logs, ~w(client total_non_bot))
}
}
end
Expand All @@ -308,7 +325,9 @@ defmodule Angen.Logging.PersistServerDayTask do
end_of_day = Timex.shift(date, days: 1)

Map.put(data, :telemetry, %{
simple_clientapp: Telemetry.simple_clientapp_events_summary(after: date, before: end_of_day)
simple_clientapp: Telemetry.simple_clientapp_events_summary(after: date, before: end_of_day),

simple_lobby: Telemetry.simple_lobby_events_summary(after: date, before: end_of_day)
})
end

Expand Down
2 changes: 1 addition & 1 deletion lib/angen/logging/tasks/persist_server_minute_task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ defmodule Angen.Logging.PersistServerMinuteTask do
{key, Enum.count(values)}
end)
|> add_total_key(:total_non_bot, [:bot])
|> add_total_key(:total_bot, [:non_bot])
|> add_total_key(:total_inc_bot, [:total_non_bot])
end

@spec get_lobby_states() :: map()
Expand Down
24 changes: 23 additions & 1 deletion lib/angen/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,39 @@ defmodule Angen.Telemetry do
# SimpleClientappEvents
alias Angen.Telemetry.{SimpleClientappEvent, SimpleClientappEventLib, SimpleClientappEventQueries}

@doc section: :event_type
@doc section: :simple_clientapp_event
@spec log_simple_clientapp_event(String.t(), Teiserver.user_id()) :: :ok | {:error, String.t()}
defdelegate log_simple_clientapp_event(name, user_id), to: SimpleClientappEventLib

@doc false
@spec simple_clientapp_event_query(Angen.query_args()) :: Ecto.Query.t()
defdelegate simple_clientapp_event_query(args), to: SimpleClientappEventQueries

@doc section: :simple_clientapp_event
@spec list_simple_clientapp_events(Teiserver.query_args()) :: [SimpleClientappEvent.t()]
defdelegate list_simple_clientapp_events(args), to: SimpleClientappEventLib

@doc section: :simple_clientapp_event
@spec simple_clientapp_events_summary(list) :: map()
defdelegate simple_clientapp_events_summary(args), to: SimpleClientappEventQueries


# SimpleLobbyEvents
alias Angen.Telemetry.{SimpleLobbyEvent, SimpleLobbyEventLib, SimpleLobbyEventQueries}

@doc section: :simple_lobby_event
@spec log_simple_lobby_event(String.t(), Teiserver.match_id(), Teiserver.user_id()) :: :ok | {:error, String.t()}
defdelegate log_simple_lobby_event(name, match_id, user_id \\ nil), to: SimpleLobbyEventLib

@doc false
@spec simple_lobby_event_query(Angen.query_args()) :: Ecto.Query.t()
defdelegate simple_lobby_event_query(args), to: SimpleLobbyEventQueries

@doc section: :simple_lobby_event
@spec list_simple_lobby_events(Teiserver.query_args()) :: [SimpleLobbyEvent.t()]
defdelegate list_simple_lobby_events(args), to: SimpleLobbyEventLib

@doc section: :simple_lobby_event
@spec simple_lobby_events_summary(list) :: map()
defdelegate simple_lobby_events_summary(args), to: SimpleLobbyEventQueries
end
8 changes: 0 additions & 8 deletions lib/angen/telemetry/libs/simple_clientapp_event_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ defmodule Angen.Telemetry.SimpleClientappEventLib do
case create_simple_clientapp_event(attrs) do
{:ok, _event} -> :ok
{:error, changeset} ->
IO.puts "#{__MODULE__}:#{__ENV__.line}"
IO.inspect attrs
IO.puts ""

IO.puts "#{__MODULE__}:#{__ENV__.line}"
IO.inspect changeset
IO.puts ""

{:error,
changeset.errors
|> Enum.map_join(", ", fn {key, {message, _}} ->
Expand Down
Loading

0 comments on commit 23baf39

Please sign in to comment.