defmodule DaProductAppWeb.AuditComplianceLive do use DaProductAppWeb, :live_view alias DaProductApp.TerminalManagement def mount(_params, _session, socket) do if connected?(socket) do # Subscribe to audit events Phoenix.PubSub.subscribe(DaProductApp.PubSub, "audit_events") # Schedule periodic updates :timer.send_interval(30_000, self(), :update_data) end socket = socket |> assign(:current_page, "audit_compliance") |> assign(:active_tab, "audit_trail") |> assign(:audit_data, %{}) |> assign(:compliance_data, %{}) |> assign(:security_data, %{}) |> assign(:current_page, "audit") |> assign(:loading, true) |> load_initial_data() {:ok, socket} end def handle_params(params, _url, socket) do tab = params["tab"] || "audit_trail" {:noreply, assign(socket, :active_tab, tab)} end def handle_event("change_tab", %{"tab" => tab}, socket) do {:noreply, push_patch(socket, to: ~p"/audit-compliance?tab=#{tab}")} end def handle_event("filter_audit", %{"date_range" => date_range}, socket) do # Apply audit filters filters = %{date_range: date_range} audit_data = TerminalManagement.get_audit_trail(filters) socket = assign(socket, :audit_data, audit_data) {:noreply, socket} end def handle_event("export_audit", %{"format" => format}, socket) do # Export audit data case TerminalManagement.export_audit_data(format) do {:ok, export_url} -> {:noreply, socket |> put_flash(:info, "Audit export completed successfully") |> push_event("download", %{url: export_url})} {:error, reason} -> {:noreply, put_flash(socket, :error, "Export failed: #{reason}")} end end def handle_event("generate_compliance_report", %{"framework" => framework}, socket) do case TerminalManagement.generate_compliance_report(framework) do {:ok, report} -> {:noreply, socket |> put_flash(:info, "Compliance report generated: #{report.id}") |> update(:compliance_data, fn data -> Map.put(data, :latest_report, report) end)} {:error, reason} -> {:noreply, put_flash(socket, :error, "Report generation failed: #{reason}")} end end def handle_event("run_security_assessment", _params, socket) do case TerminalManagement.perform_security_assessment() do {:ok, assessment} -> security_data = TerminalManagement.get_security_dashboard() {:noreply, socket |> assign(:security_data, security_data) |> put_flash(:info, "Security assessment completed: #{assessment.id}")} {:error, reason} -> {:noreply, put_flash(socket, :error, "Assessment failed: #{reason}")} end end def handle_event("acknowledge_violation", %{"violation_id" => violation_id}, socket) do # Acknowledge compliance violation {:noreply, put_flash(socket, :info, "Violation #{violation_id} acknowledged")} end def handle_event("resolve_incident", %{"incident_id" => incident_id}, socket) do # Resolve security incident {:noreply, put_flash(socket, :info, "Security incident #{incident_id} resolved")} end def handle_info(:update_data, socket) do socket = load_initial_data(socket) {:noreply, socket} end def handle_info({:audit_event, audit_entry}, socket) do # Handle real-time audit events socket = update(socket, :audit_data, fn data -> recent_entries = [audit_entry | (data[:recent_entries] || [])] |> Enum.take(50) # Keep only recent 50 entries Map.put(data, :recent_entries, recent_entries) end) {:noreply, socket} end defp load_initial_data(socket) do audit_data = TerminalManagement.get_audit_trail() compliance_data = TerminalManagement.get_compliance_dashboard() security_data = TerminalManagement.get_security_dashboard() socket |> assign(:audit_data, audit_data) |> assign(:compliance_data, compliance_data) |> assign(:security_data, security_data) |> assign(:loading, false) end def render(assigns) do ~H"""
Monitor audit trails, compliance status, and security metrics
| Timestamp | Event Type | User | Resource | Details |
|---|---|---|---|---|
| <%= Calendar.strftime(entry.timestamp, "%Y-%m-%d %H:%M") %> | <%= entry.event_type %> | <%= entry.user_id %> | <%= "#{entry.resource_type}/#{entry.resource_id}" %> | <%= inspect(entry.details) %> |
No active compliance violations
| Framework | Requirement | Severity | Status | Actions |
|---|---|---|---|---|
| <%= violation.framework %> | <%= violation.requirement %> | "bg-red-100 text-red-800" "medium" -> "bg-yellow-100 text-yellow-800" _ -> "bg-green-100 text-green-800" end ]}> <%= violation.severity %> | <%= String.replace(violation.status, "_", " ") %> |
Latest report generated: <%= @compliance_data[:latest_report].id %> for <%= @compliance_data[:latest_report].framework %>
No recent security events
| Timestamp | Event Type | Severity | Source | Details |
|---|---|---|---|---|
| <%= Calendar.strftime(event.timestamp, "%Y-%m-%d %H:%M") %> | <%= event.type %> | "bg-red-100 text-red-800" "high" -> "bg-orange-100 text-orange-800" "medium" -> "bg-yellow-100 text-yellow-800" _ -> "bg-green-100 text-green-800" end ]}> <%= event.severity %> | <%= event.source_ip %> | <%= event.details %> |
Comprehensive audit trail reports
Regulatory compliance assessments
Security monitoring and incidents
| Report Type | Generated | Status | Size | Actions |
|---|---|---|---|---|
| Compliance Assessment | 2024-01-15 10:30 | Completed | 2.4 MB | |
| Security Scan | 2024-01-14 15:45 | Completed | 1.8 MB |