Class: Fractor::Workflow::Visualizer

Inherits:
Object
  • Object
show all
Defined in:
lib/fractor/workflow/visualizer.rb

Overview

Generates visual representations of workflows. Supports Mermaid, DOT/Graphviz, and ASCII art formats.

Instance Method Summary collapse

Constructor Details

#initialize(workflow_class) ⇒ Visualizer

Returns a new instance of Visualizer.



8
9
10
11
# File 'lib/fractor/workflow/visualizer.rb', line 8

def initialize(workflow_class)
  @workflow_class = workflow_class
  @jobs = workflow_class.jobs
end

Instance Method Details

Print ASCII diagram to stdout



167
168
169
# File 'lib/fractor/workflow/visualizer.rb', line 167

def print
  puts to_ascii
end

#to_asciiString

Generate ASCII art diagram

Returns:

  • (String)

    ASCII art representation



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fractor/workflow/visualizer.rb', line 123

def to_ascii
  lines = []
  lines << "┌─────────────────────────────────────────┐"
  lines << "│ Workflow: #{@workflow_class.workflow_name.ljust(27)}"
  lines << "└─────────────────────────────────────────┘"
  lines << ""

  # Compute execution order
  order = compute_execution_order

  order.each_with_index do |job_group, index|
    if job_group.size == 1
      # Single job
      job = @jobs[job_group.first]
      lines << "    ┌─────────────────────────┐"
      lines << "#{job_group.first.ljust(23)}"
      lines << "    │ (#{job.worker_class.name.split('::').last.ljust(21)}) │"
      lines << "    └─────────────────────────┘"
    else
      # Parallel jobs
      lines << "    ╔═════════════════════════╗"
      lines << "    ║ PARALLEL EXECUTION      ║"
      lines << "    ╚═════════════════════════╝"
      job_group.each do |job_name|
        job = @jobs[job_name]
        lines << "        ├─ #{job_name}"
        lines << "        │  (#{job.worker_class.name.split('::').last})"
      end
    end

    # Arrow to next group
    if index < order.size - 1
      lines << ""
      lines << ""
    end
  end

  lines << ""
  lines << "Legend: Regular jobs │ Parallel jobs ╔═══╗"

  lines.join("\n")
end

#to_dotString

Generate DOT/Graphviz diagram

Returns:

  • (String)

    DOT syntax



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fractor/workflow/visualizer.rb', line 70

def to_dot
  lines = ["digraph #{dot_id(@workflow_class.workflow_name)} {"]
  lines << "  rankdir=TD;"
  lines << "  node [shape=box, style=rounded];"
  lines << ""

  # Start node
  lines << '  start [label="Start", shape=ellipse];'

  # Job nodes
  @jobs.each do |name, job|
    worker = job.worker_class.name
    label = "#{name}\\n(#{worker})"

    lines << if job.terminates
               "  #{dot_id(name)} [label=\"#{label}\", " \
                        "style=\"rounded,filled\", fillcolor=lightpink];"
             else
               "  #{dot_id(name)} [label=\"#{label}\"];"
             end
  end

  # End node
  lines << '  end [label="End", shape=ellipse];'
  lines << ""

  # Edges
  @jobs.each do |name, job|
    if job.dependencies.empty?
      lines << "  start -> #{dot_id(name)};"
    else
      job.dependencies.each do |dep|
        lines << if job.condition_proc
                   "  #{dot_id(dep)} -> #{dot_id(name)} " \
                            "[label=\"conditional\", style=dashed];"
                 else
                   "  #{dot_id(dep)} -> #{dot_id(name)};"
                 end
      end
    end

    if job.terminates
      lines << "  #{dot_id(name)} -> end;"
    end
  end

  lines << "}"
  lines.join("\n")
end

#to_mermaidString

Generate Mermaid flowchart diagram

Returns:

  • (String)

    Mermaid diagram syntax



16
17
18
19
20
21
22
23
24
25
26
27
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
57
58
59
60
61
62
63
64
65
# File 'lib/fractor/workflow/visualizer.rb', line 16

def to_mermaid
  lines = ["flowchart TD"]
  lines << "    Start([Start: #{@workflow_class.workflow_name}])"

  # Add job nodes
  @jobs.each do |name, job|
    label = escape_mermaid(name)
    worker = escape_mermaid(job.worker_class.name.split("::").last)

    # Different shapes based on job type
    lines << if job.terminates
               "    #{node_id(name)}[/#{label}<br/>#{worker}/]"
             else
               "    #{node_id(name)}[#{label}<br/>#{worker}]"
             end
  end

  lines << "    End([End])"

  # Add edges
  @jobs.each do |name, job|
    if job.dependencies.empty?
      # Start job
      lines << "    Start --> #{node_id(name)}"
    else
      # Dependencies
      job.dependencies.each do |dep|
        edge_label = ""
        if job.condition_proc
          edge_label = "|conditional|"
        end
        lines << "    #{node_id(dep)} -->#{edge_label} #{node_id(name)}"
      end
    end

    # Terminating jobs
    if job.terminates
      lines << "    #{node_id(name)} --> End"
    end
  end

  # Add styling
  lines << ""
  lines << "    classDef terminating fill:#f9f,stroke:#333,stroke-width:2px"
  @jobs.each do |name, job|
    lines << "    class #{node_id(name)} terminating" if job.terminates
  end

  lines.join("\n")
end