Class: Lyra::Visualization::EventGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/lyra/visualization/event_graph.rb

Overview

Force-directed event graph visualization for D3.js

Instance Method Summary collapse

Constructor Details

#initialize(events) ⇒ EventGraph

Returns a new instance of EventGraph.



5
6
7
# File 'lib/lyra/visualization/event_graph.rb', line 5

def initialize(events)
  @events = events.sort_by { |e| event_timestamp(e) }
end

Instance Method Details

#to_d3_jsonObject

Generate graph data for D3.js force-directed layout



10
11
12
13
14
15
16
# File 'lib/lyra/visualization/event_graph.rb', line 10

def to_d3_json
  {
    nodes: build_nodes,
    links: build_links,
    metadata: 
  }.to_json
end

#to_dataObject

Generate data hash (for API responses)



19
20
21
22
23
24
25
# File 'lib/lyra/visualization/event_graph.rb', line 19

def to_data
  {
    nodes: build_nodes,
    links: build_links,
    metadata: 
  }
end

#to_mermaidObject

Generate Mermaid flowchart



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lyra/visualization/event_graph.rb', line 28

def to_mermaid
  lines = ["flowchart TD"]

  @events.each do |event|
    node_id = sanitize_id(event_id(event))
    operation = event_operation(event).to_s.upcase
    model_info = "#{event_model_class(event)}##{event_model_id(event)}"
    changed_fields = format_changed_fields(event)

    label = if changed_fields.present?
      "#{operation}\\n#{model_info}\\n#{changed_fields}"
    else
      "#{operation}\\n#{model_info}"
    end

    lines << "    #{node_id}[\"#{label}\"]"
  end

  lines << ""

  build_links.each do |link|
    source_id = sanitize_id(link[:source])
    target_id = sanitize_id(link[:target])
    link_style = link[:type] == 'correlation' ? '-->' : '-.->'
    lines << "    #{source_id} #{link_style} #{target_id}"
  end

  lines.join("\n")
end