Skip to content

Commit

Permalink
Merge pull request #14 from patrick204nqh/develop
Browse files Browse the repository at this point in the history
[Update] Refactor and enhance Sequence Diagram processing and export functionality
  • Loading branch information
patrick204nqh authored Jan 13, 2025
2 parents f99f925 + afecf09 commit 427cc9c
Show file tree
Hide file tree
Showing 39 changed files with 731 additions and 89 deletions.
11 changes: 11 additions & 0 deletions lib/trace_viz/builders/base_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module TraceViz
module Builders
class BaseBuilder
def build
raise NotImplementedError
end
end
end
end
10 changes: 10 additions & 0 deletions lib/trace_viz/builders/diagram/base_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module TraceViz
module Builders
module Diagram
class BaseBuilder
end
end
end
end
74 changes: 74 additions & 0 deletions lib/trace_viz/builders/diagram/message_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

require "trace_viz/managers/diagram/participant_manager"
require "trace_viz/models/message"
require_relative "base_builder"

module TraceViz
module Builders
module Diagram
class MessageBuilder < BaseBuilder
def initialize(formatter, participants)
super()
@formatter = formatter
@participants_manager = Managers::Diagram::ParticipantsManager.new(participants)
end

def build(type, from, to, content)
Models::Message.new(type: type, from: from, to: to, content: content)
end

def build_call_message(from_trace, to_trace)
return if from_trace.klass == to_trace.klass

build(
:call,
participants_manager.find(from_trace.klass),
participants_manager.find(to_trace.klass),
formatter.format_call,
)
end

def build_return_message(from_trace, to_trace)
return if from_trace.klass == to_trace.klass

build(
:return,
participants_manager.find(from_trace.klass),
participants_manager.find(to_trace.klass),
formatter.format_return,
)
end

def build_loop_start_message(trace)
build(:loop_start, nil, nil, "#{trace.count} calls")
end

def build_loop_end_message
build(:loop_end, nil, nil, "")
end

def build_activate_message(trace)
build(:activate, nil, participants_manager.find(trace.klass), "")
end

def build_deactivate_message(trace)
build(:deactivate, nil, participants_manager.find(trace.klass), "")
end

def build_internal_message(trace)
build(
:call,
participants_manager.find(trace.klass),
participants_manager.find(trace.klass),
trace.action,
)
end

private

attr_reader :formatter, :participants_manager
end
end
end
end
35 changes: 35 additions & 0 deletions lib/trace_viz/builders/diagram/sequence_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "trace_viz/models/diagram"
require "trace_viz/extractors/diagram/participant_extractor"
require "trace_viz/extractors/diagram/message_extractor"
require_relative "base_builder"

module TraceViz
module Builders
module Diagram
class SequenceBuilder < BaseBuilder
def initialize(collector)
super()
@collector = collector
end

def build
diagram = Models::Diagram.new

participants = Extractors::Diagram::ParticipantExtractor.new(collector).extract
messages = Extractors::Diagram::MessageExtractor.new(collector, participants).extract

participants.each { |p| diagram.add_participant(p) }
messages.each { |m| diagram.add_message(m) }

diagram
end

private

attr_reader :collector
end
end
end
end
2 changes: 1 addition & 1 deletion lib/trace_viz/defaults/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def valid_param_mode?(mode)
end

def valid_export_formats
[:txt, :json, :yaml].freeze
[:txt, :json, :yaml, :mermaid].freeze
end

def valid_export_format?(format)
Expand Down
31 changes: 26 additions & 5 deletions lib/trace_viz/exporters/base_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "fileutils"
require "trace_viz/helpers"
require "trace_viz/shared"
require "trace_viz/renderers/renderer_builder"
require "trace_viz/formatters/export/formatter_factory"

module TraceViz
module Exporters
Expand All @@ -15,6 +17,12 @@ def initialize(collector)
@logger = config.logger

@collector = collector

@renderer = Renderers::RendererBuilder.build(
collector,
key: renderer_mode,
formatter_factory: Formatters::Export::FormatterFactory.new,
)
end

def export
Expand All @@ -34,10 +42,18 @@ def export

private

attr_reader :export_config, :logger, :collector
attr_reader :export_config, :logger, :collector, :renderer

def renderer_mode
fetch_general_config(:mode)
end

def content
raise NotImplementedError
data.join("\n")
end

def data
process_lines(renderer.to_lines) { |line| line[:line] }
end

def export_enabled?
Expand Down Expand Up @@ -67,10 +83,15 @@ def handle_empty_content
end

def file_path
format = export_config[:format]
path = export_config[:path]
"#{export_directory}/trace_output#{file_extension}"
end

"#{path}/trace_output.#{format}"
def export_directory
export_config[:path]
end

def file_extension
raise NotImplementedError
end

def write_file(data)
Expand Down
17 changes: 17 additions & 0 deletions lib/trace_viz/exporters/mermaid_exporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module TraceViz
module Exporters
class MermaidExporter < BaseExporter
private

def renderer_mode
:sequence_diagram
end

def file_extension
".mmd"
end
end
end
end
2 changes: 2 additions & 0 deletions lib/trace_viz/exporters/registry.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true

require_relative "text_exporter"
require_relative "mermaid_exporter"

module TraceViz
module Exporters
class Registry
EXPORTS = {
txt: TextExporter,
mermaid: MermaidExporter,
}

class << self
Expand Down
24 changes: 2 additions & 22 deletions lib/trace_viz/exporters/text_exporter.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
# frozen_string_literal: true

require_relative "base_exporter"
require "trace_viz/renderers/renderer_factory"
require "trace_viz/renderers/render_context"
require "trace_viz/formatters/export/formatter_factory"

module TraceViz
module Exporters
class TextExporter < BaseExporter
def initialize(collector)
super(collector)

@formatter_factory = Formatters::Export::FormatterFactory.new
@renderer = build_renderer(
collector,
mode: fetch_general_config(:mode),
formatter_factory: @formatter_factory,
)
end

private

attr_reader :renderer

def content
data.join("\n")
end

def data
process_lines(renderer.to_lines) { |line| line[:line] }
def file_extension
".txt"
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/trace_viz/extractors/base_extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module TraceViz
module Extractors
class BaseExtractor
attr_reader :collector

def initialize(collector)
@collector = collector
end

def extract
raise NotImplementedError
end
end
end
end
30 changes: 30 additions & 0 deletions lib/trace_viz/extractors/diagram/message_extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "trace_viz/transformers/summary_transformer"
require "trace_viz/extractors/diagram/processors/sequence_node_processor"
require_relative "../base_extractor"

module TraceViz
module Extractors
module Diagram
class MessageExtractor < BaseExtractor
def initialize(collector, participants)
super(collector)

@node_processor = Processors::SequenceNodeProcessor.new(participants)
end

def extract
root = data
root.children.flat_map { |child| @node_processor.process_node(child) }
end

private

def data
@data ||= Transformers::SummaryTransformer.new(collector).transform
end
end
end
end
end
37 changes: 37 additions & 0 deletions lib/trace_viz/extractors/diagram/participant_extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "trace_viz/utils/alias_generator"
require "trace_viz/models/participant"
require_relative "../base_extractor"

module TraceViz
module Extractors
module Diagram
class ParticipantExtractor < BaseExtractor
def extract
unique_names = data.map(&:klass).uniq

assigned_aliases = {}

unique_names.map do |raw_name|
alias_name = Utils::AliasGenerator.generate(
name: raw_name,
assigned_aliases: assigned_aliases,
)

Models::Participant.new(
name: raw_name.to_s,
alias_name: alias_name,
)
end
end

private

def data
collector.collection
end
end
end
end
end
Loading

0 comments on commit 427cc9c

Please sign in to comment.