Skip to content

Commit

Permalink
Polish activity catalog and event parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Aug 1, 2024
1 parent 74e9659 commit 4814ab6
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 263 deletions.
125 changes: 62 additions & 63 deletions lib/trento/activity_logging/activity_catalog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,19 @@ defmodule Trento.ActivityLog.ActivityCatalog do
@type domain_event_activity :: event_module :: module()
@type logged_activity :: connection_activity() | domain_event_activity()

defp load_domain_events do
case :application.get_key(:trento, :modules) do
{:ok, modules} ->
modules
|> Enum.map(&Module.split/1)
|> Enum.filter(fn
["Trento", resource_type, "Events", _]
when resource_type in [
"Hosts",
"Clusters",
"SapSystems",
"Databases"
] ->
true

_ ->
false
end)
|> Enum.map(&Module.concat/1)
|> Enum.filter(&(not function_exported?(&1, :legacy, 0)))
|> Map.new(fn event_module ->
{event_module,
{event_module
|> Module.split()
|> List.last()
|> Macro.underscore()
|> String.to_atom(), :always}}
end)

_ ->
%{}
end
end

defp load_connection_activities do
%{
{TrentoWeb.SessionController, :create} => {:login_attempt, :always},
{TrentoWeb.V1.TagsController, :add_tag} => {:resource_tagging, 201},
{TrentoWeb.V1.TagsController, :remove_tag} => {:resource_untagging, 204},
{TrentoWeb.V1.SettingsController, :update_api_key_settings} => {:api_key_generation, 200},
{TrentoWeb.V1.SettingsController, :save_suse_manager_settings} =>
{:saving_suma_settings, 201},
{TrentoWeb.V1.SettingsController, :update_suse_manager_settings} =>
{:changing_suma_settings, 200},
{TrentoWeb.V1.SettingsController, :delete_suse_manager_settings} =>
{:clearing_suma_settings, 204},
{TrentoWeb.V1.UsersController, :create} => {:user_creation, 201},
{TrentoWeb.V1.UsersController, :update} => {:user_modification, 200},
{TrentoWeb.V1.UsersController, :delete} => {:user_deletion, 204},
{TrentoWeb.V1.ProfileController, :update} => {:profile_update, 200},
{TrentoWeb.V1.ClusterController, :request_checks_execution} =>
{:cluster_checks_execution_request, 202}
}
end

@spec supported_activities() :: [activity_type()]
def supported_activities, do: Enum.map(activity_catalog(), &to_activity_type/1)

@spec connection_activities() :: [activity_type()]
def connection_activities, do: Enum.map(load_connection_activities(), &to_activity_type/1)
def connection_activities, do: Enum.map(get_connection_activities(), &to_activity_type/1)

@spec domain_event_activities() :: [activity_type()]
def domain_event_activities, do: Enum.map(load_domain_events(), &to_activity_type/1)

defp activity_catalog, do: Map.merge(load_connection_activities(), load_domain_events())
def domain_event_activities, do: Enum.map(get_domain_events_activities(), &to_activity_type/1)

@spec detect_activity_category(activity_type()) ::
:connection_activity | :domain_event_activity | :unsupported_activity
def detect_activity_category(activity) do
is_connection_activity? = activity in connection_activities()
is_domain_event_activity? = activity in domain_event_activities()

case {is_connection_activity?, is_domain_event_activity?} do
case {activity in connection_activities(), activity in domain_event_activities()} do
{true, false} ->
:connection_activity

Expand All @@ -105,6 +46,9 @@ defmodule Trento.ActivityLog.ActivityCatalog do

defp to_activity_type({_, {activity_type, _}}), do: activity_type

defp activity_catalog,
do: Map.merge(get_connection_activities(), get_domain_events_activities())

@spec get_activity_type(logged_activity()) :: activity_type() | nil
defp get_activity_type(activity) do
case Map.fetch(activity_catalog(), activity) do
Expand Down Expand Up @@ -143,4 +87,59 @@ defmodule Trento.ActivityLog.ActivityCatalog do
defp interesting_occurrence?({_, :always}, _), do: true
defp interesting_occurrence?({_, status}, %Plug.Conn{status: status}), do: true
defp interesting_occurrence?(_, _), do: false

defp get_domain_events_activities do
case :application.get_key(:trento, :modules) do
{:ok, modules} ->
modules
|> Enum.map(&Module.split/1)
|> Enum.filter(fn
["Trento", resource_type, "Events", _]
when resource_type in [
"Hosts",
"Clusters",
"SapSystems",
"Databases"
] ->
true

_ ->
false
end)
|> Enum.map(&Module.concat/1)
|> Enum.filter(&(not function_exported?(&1, :legacy, 0)))
|> Map.new(fn event_module ->
{event_module,
{event_module
|> Module.split()
|> List.last()
|> Macro.underscore()
|> String.to_atom(), :always}}
end)

_ ->
%{}
end
end

defp get_connection_activities do
%{
{TrentoWeb.SessionController, :create} => {:login_attempt, :always},
{TrentoWeb.V1.TagsController, :add_tag} => {:resource_tagging, 201},
{TrentoWeb.V1.TagsController, :remove_tag} => {:resource_untagging, 204},
{TrentoWeb.V1.SettingsController, :update_api_key_settings} => {:api_key_generation, 200},
{TrentoWeb.V1.SettingsController, :save_suse_manager_settings} =>
{:saving_suma_settings, 201},
{TrentoWeb.V1.SettingsController, :update_suse_manager_settings} =>
{:changing_suma_settings, 200},
{TrentoWeb.V1.SettingsController, :delete_suse_manager_settings} =>
{:clearing_suma_settings, 204},
{TrentoWeb.V1.UsersController, :create} => {:user_creation, 201},
{TrentoWeb.V1.UsersController, :update} => {:user_modification, 200},
{TrentoWeb.V1.UsersController, :delete} => {:user_deletion, 204},
{TrentoWeb.V1.ProfileController, :update} => {:profile_update, 200},
{TrentoWeb.V1.ClusterController, :request_checks_execution} =>
{:cluster_checks_execution_request, 202}
}
end
end
Loading

0 comments on commit 4814ab6

Please sign in to comment.