Skip to content

Commit

Permalink
Generalize enriching function
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Sep 19, 2024
1 parent a56f2fa commit 03f6d8c
Showing 1 changed file with 11 additions and 46 deletions.
57 changes: 11 additions & 46 deletions lib/trento/activity_logging/parser/metadata_enricher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,18 @@ defmodule Trento.ActivityLog.Logger.Parser.MetadataEnricher do
defp enrich_metadata(activity, metadata),
do:
{activity, metadata}
|> maybe_enrich_with_hostname()
|> maybe_enrich_with_cluster_name()
|> maybe_enrich_with_database_sid()
|> maybe_enrich_with_sap_system_sid()
|> maybe_enrich(:host, Hosts, :hostname)
|> maybe_enrich(:cluster, Clusters, :name)
|> maybe_enrich(:database, Databases, :sid)
|> maybe_enrich(:sap_system, SapSystems, :sid)
|> elem(1)

defp maybe_enrich_with_hostname({activity, metadata}) do
defp maybe_enrich({activity, metadata}, entity_reference, context_module, enriching_field) do
enriched_metadata =
with {:ok, host_id} <- detect_enrichment(:host, {activity, metadata}),
{:ok, %{hostname: hostname}} <- Hosts.by_id(host_id) do
Map.put(metadata, :hostname, hostname)
else
_ -> metadata
end

{activity, enriched_metadata}
end

defp maybe_enrich_with_cluster_name({activity, metadata}) do
enriched_metadata =
with {:ok, cluster_id} <- detect_enrichment(:cluster, {activity, metadata}),
{:ok, %{name: cluster_name}} <- Clusters.by_id(cluster_id) do
Map.put(metadata, :name, cluster_name)
else
_ -> metadata
end

{activity, enriched_metadata}
end

defp maybe_enrich_with_database_sid({activity, metadata}) do
enriched_metadata =
with {:ok, database_id} <- detect_enrichment(:database, {activity, metadata}),
{:ok, %{sid: sid}} <- Databases.by_id(database_id) do
Map.put(metadata, :sid, sid)
else
_ -> metadata
end

{activity, enriched_metadata}
end

defp maybe_enrich_with_sap_system_sid({activity, metadata}) do
enriched_metadata =
with {:ok, sap_system_id} <- detect_enrichment(:sap_system, {activity, metadata}),
{:ok, %{sid: sid}} <- SapSystems.by_id(sap_system_id) do
Map.put(metadata, :sid, sid)
with {:ok, entity_id} <- detect_enrichment(entity_reference, {activity, metadata}),
{:ok, entity} <- context_module.by_id(entity_id),
{:ok, enriching_value} <- Map.fetch(entity, enriching_field) do
Map.put(metadata, enriching_field, enriching_value)
else
_ -> metadata
end
Expand All @@ -78,7 +43,7 @@ defmodule Trento.ActivityLog.Logger.Parser.MetadataEnricher do
end

defp detect_enrichment(
_target_resource,
_target_entity,
{activity,
%{
resource_id: resource_id,
Expand Down Expand Up @@ -115,6 +80,6 @@ defmodule Trento.ActivityLog.Logger.Parser.MetadataEnricher do
defp detect_enrichment(:database, {_, %{database_id: id}}), do: {:ok, id}
defp detect_enrichment(:sap_system, {_, %{sap_system_id: id}}), do: {:ok, id}

defp detect_enrichment(_target_resource, {_activity, _metadata}),
defp detect_enrichment(_target_entity, {_activity, _metadata}),
do: {:error, :no_enrichment_needed}
end

0 comments on commit 03f6d8c

Please sign in to comment.