trace_viz 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +63 -41
  3. data/lib/trace_viz/builders/base_builder.rb +11 -0
  4. data/lib/trace_viz/builders/diagram/base_builder.rb +10 -0
  5. data/lib/trace_viz/builders/diagram/message_builder.rb +96 -0
  6. data/lib/trace_viz/builders/diagram/sequence_builder.rb +50 -0
  7. data/lib/trace_viz/collectors/filters/base_class_filter.rb +3 -1
  8. data/lib/trace_viz/config/validator.rb +1 -1
  9. data/lib/trace_viz/defaults/config.rb +4 -2
  10. data/lib/trace_viz/exporters/base_exporter.rb +26 -5
  11. data/lib/trace_viz/exporters/mermaid_exporter.rb +17 -0
  12. data/lib/trace_viz/exporters/registry.rb +2 -0
  13. data/lib/trace_viz/exporters/text_exporter.rb +2 -24
  14. data/lib/trace_viz/extractors/base_extractor.rb +17 -0
  15. data/lib/trace_viz/extractors/diagram/base_extractor.rb +18 -0
  16. data/lib/trace_viz/extractors/diagram/box_extractor.rb +50 -0
  17. data/lib/trace_viz/extractors/diagram/message_extractor.rb +23 -0
  18. data/lib/trace_viz/extractors/diagram/participant_extractor.rb +37 -0
  19. data/lib/trace_viz/extractors/diagram/processors/message_processor.rb +93 -0
  20. data/lib/trace_viz/formatters/base_formatter.rb +1 -0
  21. data/lib/trace_viz/formatters/base_formatter_factory.rb +23 -0
  22. data/lib/trace_viz/formatters/diagram/sequence/base_formatter.rb +14 -0
  23. data/lib/trace_viz/formatters/diagram/sequence/message_formatter.rb +37 -0
  24. data/lib/trace_viz/formatters/diagram_formatter.rb +10 -0
  25. data/lib/trace_viz/formatters/export/formatter_factory.rb +8 -13
  26. data/lib/trace_viz/formatters/export/summary_group_formatter.rb +1 -1
  27. data/lib/trace_viz/formatters/helpers/digram/action_helper.rb +17 -0
  28. data/lib/trace_viz/formatters/helpers/digram/result_helper.rb +39 -0
  29. data/lib/trace_viz/formatters/helpers/log/params_helper.rb +8 -4
  30. data/lib/trace_viz/formatters/helpers/log/result_helper.rb +3 -3
  31. data/lib/trace_viz/formatters/helpers/log/summary/params_helper.rb +8 -4
  32. data/lib/trace_viz/formatters/helpers/params_helper.rb +8 -4
  33. data/lib/trace_viz/formatters/helpers/result_helper.rb +4 -3
  34. data/lib/trace_viz/formatters/helpers/source_helper.rb +4 -3
  35. data/lib/trace_viz/formatters/helpers/summary/params_helper.rb +7 -3
  36. data/lib/trace_viz/formatters/helpers/summary/source_helper.rb +3 -3
  37. data/lib/trace_viz/formatters/log/formatter_factory.rb +8 -13
  38. data/lib/trace_viz/formatters/log/summary_group_formatter.rb +1 -1
  39. data/lib/trace_viz/helpers/config_helper.rb +4 -0
  40. data/lib/trace_viz/loggers/post_collection_logger.rb +9 -4
  41. data/lib/trace_viz/loggers/trace_logger.rb +3 -6
  42. data/lib/trace_viz/managers/diagram/participant_manager.rb +23 -0
  43. data/lib/trace_viz/models/box.rb +19 -0
  44. data/lib/trace_viz/models/diagram.rb +27 -0
  45. data/lib/trace_viz/models/message.rb +16 -0
  46. data/lib/trace_viz/models/participant.rb +18 -0
  47. data/lib/trace_viz/models.rb +8 -0
  48. data/lib/trace_viz/renderers/base_renderer.rb +6 -3
  49. data/lib/trace_viz/renderers/diagram/sequence_renderer.rb +59 -0
  50. data/lib/trace_viz/renderers/render_context.rb +2 -3
  51. data/lib/trace_viz/renderers/renderer_builder.rb +20 -0
  52. data/lib/trace_viz/renderers/renderer_factory.rb +10 -22
  53. data/lib/trace_viz/renderers/summary_renderer.rb +18 -6
  54. data/lib/trace_viz/renderers/verbose_renderer.rb +6 -6
  55. data/lib/trace_viz/shared/renderer_helper.rb +4 -25
  56. data/lib/trace_viz/syntax/mermaid/sequence_syntax.rb +99 -0
  57. data/lib/trace_viz/trace_data/base.rb +7 -0
  58. data/lib/trace_viz/trace_data/node.rb +9 -3
  59. data/lib/trace_viz/trace_data/root_node.rb +4 -0
  60. data/lib/trace_viz/trace_data/summary_node.rb +5 -9
  61. data/lib/trace_viz/trace_data/trace_point/base.rb +4 -0
  62. data/lib/trace_viz/trace_data/trace_point/method_call.rb +4 -0
  63. data/lib/trace_viz/transformers/base_transformer.rb +29 -0
  64. data/lib/trace_viz/transformers/summary_transformer.rb +70 -0
  65. data/lib/trace_viz/utils/alias_generator.rb +58 -0
  66. data/lib/trace_viz/utils/format/key_value_formatter.rb +42 -0
  67. data/lib/trace_viz/utils/format/value_truncator.rb +123 -0
  68. data/lib/trace_viz/utils.rb +8 -0
  69. data/lib/trace_viz/version.rb +1 -1
  70. metadata +38 -11
  71. data/lib/trace_viz/renderers/summary/node_processor.rb +0 -82
  72. data/lib/trace_viz/utils/format_utils/key_value_formatter.rb +0 -37
  73. data/lib/trace_viz/utils/format_utils/value_truncator.rb +0 -74
  74. data/lib/trace_viz/utils/format_utils.rb +0 -24
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8fffeaf37d8c9d9854f95159dba5ab2c8d9657a4ad63828e04164a79911ba4a3
4
- data.tar.gz: 8c868e5bd2e954611c280fcb23a68cbf059f8314a98f7aaba2d3da909197e203
3
+ metadata.gz: bb4571e8a37548c6819b56d576ec740c9052efe64c2984cd65a6a06f53e935b3
4
+ data.tar.gz: 7dfd025e87f5076957783b45512a6c8aca8b65dd98ec67bd6318681efd5d1d3c
5
5
  SHA512:
6
- metadata.gz: 25fa2eb78c78ad670bc3dd5e2819880154e4119c7260ab4127d35d7f75bcf56a23e237a0eb4cbbd893e2d3f6547a57496b432a214a2145770bdb252385d7dd94
7
- data.tar.gz: f267c37003eaa0a4a4427e6faedac0e83827e3e5c6433bc22aee68a32b5b14a07abc51cf6d71bbfaf9aff3bfb0448cd381d1e168bb34e345d95c138478623635
6
+ metadata.gz: 2bf8264a62463c20f1e585fac19835f8d739fe703c4dfaef159e8c3fde331ace651f750c17591da5c3441faf6cf33d9baff2af6048c457bdc89d3a5e76148015
7
+ data.tar.gz: 4ae53f49154cbb6b6c0a95075c62ccd698ad3154abb3a5871a2697019020a99634fa9a82ddee118d81ec29543de2ad5cd7610a6f57c6429d7700372581f2f91a
data/README.md CHANGED
@@ -9,8 +9,6 @@
9
9
 
10
10
  TraceViz is a Ruby library designed to trace and visualize events executed in a block of code. It is useful for logging, debugging, and generating diagrams to understand code execution and flow.
11
11
 
12
- > **Note:** The diagram generation feature is currently under development.
13
-
14
12
  The gem allows you to customize how much detail you want to see, such as method calls, parameters, return values, and execution times.
15
13
 
16
14
  ## Demo
@@ -143,12 +141,14 @@ TraceViz.trace(
143
141
  },
144
142
  filters: [
145
143
  :exclude_internal_call,
146
- include_classes: [Example]
144
+ include_classes: {
145
+ classes: [Example]
146
+ }
147
147
  ],
148
148
  export: {
149
149
  enabled: true,
150
150
  path: "tmp",
151
- format: :txt,
151
+ format: :txt, # Select :mermaid to export a mermaid diagram
152
152
  overwrite: false,
153
153
  }
154
154
  ) do
@@ -159,8 +159,6 @@ end
159
159
 
160
160
  </details>
161
161
 
162
- **Output**
163
-
164
162
  <details open>
165
163
  <summary>Sample Output</summary>
166
164
 
@@ -178,45 +176,69 @@ Final result: 24
178
176
 
179
177
  </details>
180
178
 
179
+ <details open>
180
+ <summary>Sample Diagram</summary>
181
+
182
+ ```mermaid
183
+ sequenceDiagram
184
+ box rgb(224, 236, 221) Example
185
+ participant E as Example
186
+ end
187
+ E ->> E: perform_task(x: 5, y: 7)
188
+ Note over E: 24
189
+ activate E
190
+ E ->> E: add_numbers(a: 5, b: 7)
191
+ Note over E: 24
192
+ activate E
193
+ E ->> E: multiply_by_factor(value: 12, factor: 2)
194
+ Note over E: 24
195
+ deactivate E
196
+ E ->> E: log_result(result: 24)
197
+ Note over E: nil
198
+ deactivate E
199
+ ```
200
+
201
+ </details>
202
+
181
203
  ### Configuration Options
182
204
 
183
205
  TraceViz provides extensive configuration options to customize tracing behavior.
184
206
 
185
- | Group | Option | Type | Default Value | Description |
186
- | ----------------- | -------------------------- | ---------------- | --------------------------- | -------------------------------------------------------------------------- |
187
- | `general` | `tab_size` | Integer | 2 | Number of spaces for indentation. |
188
- | | `mode` | Symbol | `:summary` | Display mode (`:summary` or `:verbose`). |
189
- | | `group_keys` | Array of Symbols | `[:event, :klass, :action]` | Keys to group similar outputs. |
190
- | | `show_indent` | Boolean | true | Enables visual indentation for nested calls. |
191
- | | `show_depth` | Boolean | true | Displays the depth level of the method call. |
192
- | | `max_display_depth` | Integer | 3 | Maximum depth of calls to display. |
193
- | | `show_method_name` | Boolean | true | Logs the name of the method being executed. |
194
- | `source_location` | `show` | Boolean | false | Logs the source file and line number for methods. |
195
- | | `truncate_length` | Integer | 100 | Maximum length of displayed source location information. |
196
- | `params` | `show` | Boolean | true | Logs method parameters. |
197
- | | `mode` | Symbol | `:name_and_value` | Parameter display mode (`:name`, `:value`, or `:name_and_value`). |
198
- | | `truncate_values` | Integer | 50 | Maximum length of parameter values to display. |
199
- | `result` | `show` | Boolean | true | Logs method return values. |
200
- | | `truncate_length` | Integer | 50 | Maximum length of return value logs. |
201
- | `execution` | `show_time` | Boolean | true | Logs execution time for methods. |
202
- | | `show_trace_events` | Array of Symbols | `[:call, :return]` | Specifies the trace events to log (e.g., `:call`, `:return`). |
203
- | `filters` | `:exclude_internal_call` | Symbol | N/A | Exclude internal Ruby calls. |
204
- | | `:exclude_default_classes` | Symbol | N/A | Skip logging standard library classes. |
205
- | | `:exclude_rails_framework` | Symbol | N/A | Ignore Rails framework classes and methods. |
206
- | | `include_classes` | Hash | N/A | Specify classes to include in tracing. |
207
- | | - `classes` | Array | [] | List of class names to include (e.g., `["ClassName"]`). |
208
- | | `exclude_classes` | Hash | N/A | Specify classes to exclude from tracing. |
209
- | | - `classes` | Array | [] | List of class names to exclude (e.g., `["ClassName"]`). |
210
- | | `include_gems` | Hash | N/A | Include specific gems or runtime application gems. |
211
- | | - `app_running` | Boolean | true | Include the code of the application. |
212
- | | - `app_path` | String | `Dir.pwd` | Path to the application (e.g., `Dir.pwd`). |
213
- | | - `gems` | Array | [] | List of gems to include (e.g., `["gem1", "gem2"]`). |
214
- | | `exclude_gems` | Hash | N/A | Exclude specified gems. |
215
- | | - `gems` | Array | [] | List of gems to exclude (e.g., `["excluded_gem"]`). |
216
- | `export` | `enabled` | Boolean | true | Enables or disables exporting of trace logs. |
217
- | | `path` | String | `"tmp"` | Directory for exported trace logs. |
218
- | | `format` | Symbol | `:txt` | Format for trace logs (`:txt`). `.json` and `.yaml` are under development. |
219
- | | `overwrite` | Boolean | false | Prevents overwriting of existing exported files. |
207
+ | Group | Option | Type | Default Value | Description |
208
+ | ----------------- | -------------------------- | ---------------- | --------------------------- | -------------------------------------------------------------------------------- |
209
+ | `general` | `tab_size` | Integer | 2 | Number of spaces for indentation. |
210
+ | | `mode` | Symbol | `:summary` | Display mode (`:summary` or `:verbose`). |
211
+ | | `group_keys` | Array of Symbols | `[:event, :klass, :action]` | Keys to group similar outputs. |
212
+ | | `show_indent` | Boolean | true | Enables visual indentation for nested calls. |
213
+ | | `show_depth` | Boolean | true | Displays the depth level of the method call. |
214
+ | | `max_display_depth` | Integer | 3 | Maximum depth of calls to display. |
215
+ | | `show_method_name` | Boolean | true | Logs the name of the method being executed. |
216
+ | `source_location` | `show` | Boolean | false | Logs the source file and line number for methods. |
217
+ | | `truncate_length` | Integer | 100 | Maximum length of displayed source location information. |
218
+ | `params` | `show` | Boolean | true | Logs method parameters. |
219
+ | | `mode` | Symbol | `:name_and_value` | Parameter display mode (`:name`, `:value`, or `:name_and_value`). |
220
+ | | `truncate_values` | Integer | 50 | Maximum length of parameter values to display. |
221
+ | `result` | `show` | Boolean | true | Logs method return values. |
222
+ | | `truncate_length` | Integer | 50 | Maximum length of return value logs. |
223
+ | `execution` | `show_time` | Boolean | true | Logs execution time for methods. |
224
+ | | `show_trace_events` | Array of Symbols | `[:call, :return]` | Specifies the trace events to log (e.g., `:call`, `:return`). |
225
+ | `filters` | `:exclude_internal_call` | Symbol | N/A | Exclude internal Ruby calls. |
226
+ | | `:exclude_default_classes` | Symbol | N/A | Skip logging standard library classes. |
227
+ | | `:exclude_rails_framework` | Symbol | N/A | Ignore Rails framework classes and methods. |
228
+ | | `include_classes` | Hash | N/A | Specify classes to include in tracing. |
229
+ | | - `classes` | Array | [] | List of class names to include (e.g., `["ClassName"]`). |
230
+ | | `exclude_classes` | Hash | N/A | Specify classes to exclude from tracing. |
231
+ | | - `classes` | Array | [] | List of class names to exclude (e.g., `["ClassName"]`). |
232
+ | | `include_gems` | Hash | N/A | Include specific gems or runtime application gems. |
233
+ | | - `app_running` | Boolean | true | Include the code of the application. |
234
+ | | - `app_path` | String | `Dir.pwd` | Path to the application (e.g., `Dir.pwd`). |
235
+ | | - `gems` | Array | [] | List of gems to include (e.g., `["gem1", "gem2"]`). |
236
+ | | `exclude_gems` | Hash | N/A | Exclude specified gems. |
237
+ | | - `gems` | Array | [] | List of gems to exclude (e.g., `["excluded_gem"]`). |
238
+ | `export` | `enabled` | Boolean | true | Enables or disables exporting of trace logs. |
239
+ | | `path` | String | `"tmp"` | Directory for exported trace logs. |
240
+ | | `format` | Symbol | `:txt` | Format for trace logs. Use :txt for plain text or :mermaid for mermaid diagrams. |
241
+ | | `overwrite` | Boolean | false | Prevents overwriting of existing exported files. |
220
242
 
221
243
  ### Notes
222
244
 
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TraceViz
4
+ module Builders
5
+ class BaseBuilder
6
+ def build
7
+ raise NotImplementedError
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TraceViz
4
+ module Builders
5
+ module Diagram
6
+ class BaseBuilder
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "trace_viz/managers/diagram/participant_manager"
4
+ require "trace_viz/models"
5
+ require_relative "base_builder"
6
+
7
+ module TraceViz
8
+ module Builders
9
+ module Diagram
10
+ class MessageBuilder < BaseBuilder
11
+ def initialize(formatter, participants)
12
+ super()
13
+ @formatter = formatter
14
+ @participants_manager = Managers::Diagram::ParticipantsManager.new(participants)
15
+ end
16
+
17
+ def build_call_message(from_trace, to_trace)
18
+ return if same_class?(from_trace, to_trace)
19
+
20
+ build(
21
+ :call,
22
+ from: participant_for(from_trace),
23
+ to: participant_for(to_trace),
24
+ content: formatter.format_call,
25
+ )
26
+ end
27
+
28
+ def build_return_message(from_trace, to_trace)
29
+ return if same_class?(from_trace, to_trace)
30
+
31
+ build(
32
+ :return,
33
+ from: participant_for(from_trace),
34
+ to: participant_for(to_trace),
35
+ content: formatter.format_return,
36
+ )
37
+ end
38
+
39
+ def build_loop_start_message(trace)
40
+ build(:loop_start, content: "#{trace.count} calls")
41
+ end
42
+
43
+ def build_loop_end_message
44
+ build(:loop_end)
45
+ end
46
+
47
+ def build_activate_message(trace)
48
+ build(:activate, to: participant_for(trace))
49
+ end
50
+
51
+ def build_deactivate_message(trace)
52
+ build(:deactivate, to: participant_for(trace))
53
+ end
54
+
55
+ def build_internal_message(trace)
56
+ build(
57
+ :call,
58
+ from: participant_for(trace),
59
+ to: participant_for(trace),
60
+ content: formatter.format_internal_message(trace),
61
+ )
62
+ end
63
+
64
+ def build_note(trace)
65
+ build(
66
+ :note,
67
+ from: participant_for(trace),
68
+ to: participant_for(trace),
69
+ content: formatter.format_result(trace),
70
+ )
71
+ end
72
+
73
+ private
74
+
75
+ attr_reader :formatter, :participants_manager
76
+
77
+ def build(type, from: nil, to: nil, content: "")
78
+ Models::Message.new(
79
+ type: type,
80
+ from: from,
81
+ to: to,
82
+ content: content,
83
+ )
84
+ end
85
+
86
+ def same_class?(from_trace, to_trace)
87
+ from_trace.klass == to_trace.klass
88
+ end
89
+
90
+ def participant_for(trace)
91
+ participants_manager.find(trace.klass)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "trace_viz/models/diagram"
4
+ require "trace_viz/extractors/diagram/box_extractor"
5
+ require "trace_viz/extractors/diagram/message_extractor"
6
+ require_relative "base_builder"
7
+
8
+ module TraceViz
9
+ module Builders
10
+ module Diagram
11
+ class SequenceBuilder < BaseBuilder
12
+ def initialize(collector)
13
+ super()
14
+ @collector = collector
15
+ end
16
+
17
+ def build
18
+ Models::Diagram.new.tap do |diagram|
19
+ add_boxes_to(diagram)
20
+ add_messages_to(diagram)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :collector
27
+
28
+ def add_boxes_to(diagram)
29
+ boxes.each { |box| diagram.add_box(box) }
30
+ end
31
+
32
+ def add_messages_to(diagram)
33
+ messages.each { |message| diagram.add_message(message) }
34
+ end
35
+
36
+ def boxes
37
+ @boxes ||= Extractors::Diagram::BoxExtractor.new(collector).extract
38
+ end
39
+
40
+ def participants
41
+ @participants ||= boxes.flat_map(&:participants)
42
+ end
43
+
44
+ def messages
45
+ @messages ||= Extractors::Diagram::MessageExtractor.new(collector, participants).extract
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -17,7 +17,9 @@ module TraceViz
17
17
 
18
18
  def normalize_class_name(klass)
19
19
  # Normalize class/module names to handle dynamic/nested representations
20
- klass.to_s.gsub(/^#<Class:/, "").gsub(/>$/, "").strip
20
+ klass.to_s
21
+ .gsub(/^#<Class/, "<Class")
22
+ .strip
21
23
  end
22
24
 
23
25
  def matches_hierarchy?(klass_name, target_name)
@@ -38,7 +38,7 @@ module TraceViz
38
38
  end
39
39
 
40
40
  def validate_result(value)
41
- if value[:truncate_length] && !value[:truncate_length].is_a?(Integer)
41
+ if value[:truncate_value] && !value[:truncate_value].is_a?(Integer)
42
42
  raise ArgumentError, "Truncate values must be a positive integer."
43
43
  end
44
44
  end
@@ -21,10 +21,12 @@ module TraceViz
21
21
  show: true,
22
22
  mode: :name_and_value,
23
23
  truncate_values: 10,
24
+ truncate_length: -1,
24
25
  },
25
26
  result: {
26
27
  show: true,
27
- truncate_length: 50,
28
+ truncate_value: 50,
29
+ truncate_length: 30,
28
30
  },
29
31
  execution: {
30
32
  show_time: true,
@@ -77,7 +79,7 @@ module TraceViz
77
79
  end
78
80
 
79
81
  def valid_export_formats
80
- [:txt, :json, :yaml].freeze
82
+ [:txt, :json, :yaml, :mermaid].freeze
81
83
  end
82
84
 
83
85
  def valid_export_format?(format)
@@ -3,6 +3,8 @@
3
3
  require "fileutils"
4
4
  require "trace_viz/helpers"
5
5
  require "trace_viz/shared"
6
+ require "trace_viz/renderers/renderer_builder"
7
+ require "trace_viz/formatters/export/formatter_factory"
6
8
 
7
9
  module TraceViz
8
10
  module Exporters
@@ -15,6 +17,12 @@ module TraceViz
15
17
  @logger = config.logger
16
18
 
17
19
  @collector = collector
20
+
21
+ @renderer = Renderers::RendererBuilder.build(
22
+ collector,
23
+ key: renderer_mode,
24
+ formatter_factory: Formatters::Export::FormatterFactory.new,
25
+ )
18
26
  end
19
27
 
20
28
  def export
@@ -34,10 +42,18 @@ module TraceViz
34
42
 
35
43
  private
36
44
 
37
- attr_reader :export_config, :logger, :collector
45
+ attr_reader :export_config, :logger, :collector, :renderer
46
+
47
+ def renderer_mode
48
+ fetch_general_config(:mode)
49
+ end
38
50
 
39
51
  def content
40
- raise NotImplementedError
52
+ data.join("\n")
53
+ end
54
+
55
+ def data
56
+ process_lines(renderer.to_lines) { |line| line[:line] }
41
57
  end
42
58
 
43
59
  def export_enabled?
@@ -67,10 +83,15 @@ module TraceViz
67
83
  end
68
84
 
69
85
  def file_path
70
- format = export_config[:format]
71
- path = export_config[:path]
86
+ "#{export_directory}/trace_output#{file_extension}"
87
+ end
72
88
 
73
- "#{path}/trace_output.#{format}"
89
+ def export_directory
90
+ export_config[:path]
91
+ end
92
+
93
+ def file_extension
94
+ raise NotImplementedError
74
95
  end
75
96
 
76
97
  def write_file(data)
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TraceViz
4
+ module Exporters
5
+ class MermaidExporter < BaseExporter
6
+ private
7
+
8
+ def renderer_mode
9
+ :sequence_diagram
10
+ end
11
+
12
+ def file_extension
13
+ ".mmd"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "text_exporter"
4
+ require_relative "mermaid_exporter"
4
5
 
5
6
  module TraceViz
6
7
  module Exporters
7
8
  class Registry
8
9
  EXPORTS = {
9
10
  txt: TextExporter,
11
+ mermaid: MermaidExporter,
10
12
  }
11
13
 
12
14
  class << self
@@ -1,36 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "base_exporter"
4
- require "trace_viz/renderers/renderer_factory"
5
- require "trace_viz/renderers/render_context"
6
- require "trace_viz/formatters/export/formatter_factory"
7
4
 
8
5
  module TraceViz
9
6
  module Exporters
10
7
  class TextExporter < BaseExporter
11
- def initialize(collector)
12
- super(collector)
13
-
14
- @renderer = build_renderer(collector, Formatters::Export::FormatterFactory)
15
- end
16
-
17
8
  private
18
9
 
19
- attr_reader :renderer
20
-
21
- def content
22
- data.join("\n")
23
- end
24
-
25
- def data
26
- process_lines(renderer.to_lines) { |line| process_line(line) }
27
- end
28
-
29
- def process_line(line)
30
- [
31
- line[:line],
32
- *process_lines(line[:nested_lines]) { |nested| process_line(nested) },
33
- ].compact
10
+ def file_extension
11
+ ".txt"
34
12
  end
35
13
  end
36
14
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TraceViz
4
+ module Extractors
5
+ class BaseExtractor
6
+ attr_reader :collector
7
+
8
+ def initialize(collector)
9
+ @collector = collector
10
+ end
11
+
12
+ def extract
13
+ raise NotImplementedError
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "trace_viz/transformers/summary_transformer"
4
+ require_relative "../base_extractor"
5
+
6
+ module TraceViz
7
+ module Extractors
8
+ module Diagram
9
+ class BaseExtractor < BaseExtractor
10
+ private
11
+
12
+ def data
13
+ @data ||= Transformers::SummaryTransformer.new(collector).transform
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base_extractor"
4
+ require_relative "participant_extractor"
5
+ require "trace_viz/models/box"
6
+
7
+ module TraceViz
8
+ module Extractors
9
+ module Diagram
10
+ class BoxExtractor < BaseExtractor
11
+ def initialize(collector)
12
+ super(collector)
13
+
14
+ @participants = ParticipantExtractor.new(collector).extract
15
+ end
16
+
17
+ def extract
18
+ grouped_participants = group_by_namespace(@participants)
19
+
20
+ grouped_participants.map do |namespace, participants|
21
+ box = Models::Box.new(
22
+ color: random_rgb,
23
+ description: namespace,
24
+ )
25
+
26
+ participants.each do |participant|
27
+ box.add_participant(participant)
28
+ end
29
+
30
+ box
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def group_by_namespace(participants)
37
+ participants.group_by { |participant| extract_namespace(participant.name) }
38
+ end
39
+
40
+ def extract_namespace(klass_name)
41
+ klass_name.split("::").first
42
+ end
43
+
44
+ def random_rgb
45
+ "rgb(#{rand(200..255)}, #{rand(200..255)}, #{rand(200..255)})"
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "processors/message_processor"
4
+ require_relative "base_extractor"
5
+
6
+ module TraceViz
7
+ module Extractors
8
+ module Diagram
9
+ class MessageExtractor < BaseExtractor
10
+ def initialize(collector, participants)
11
+ super(collector)
12
+
13
+ @node_processor = Processors::MessageProcessor.new(participants)
14
+ end
15
+
16
+ def extract
17
+ root = data
18
+ root.children.flat_map { |child| @node_processor.process_node(child) }
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "trace_viz/utils/alias_generator"
4
+ require "trace_viz/models/participant"
5
+ require_relative "base_extractor"
6
+
7
+ module TraceViz
8
+ module Extractors
9
+ module Diagram
10
+ class ParticipantExtractor < BaseExtractor
11
+ def extract
12
+ unique_names = data.map(&:klass).uniq
13
+
14
+ assigned_aliases = {}
15
+
16
+ unique_names.map do |raw_name|
17
+ alias_name = Utils::AliasGenerator.generate(
18
+ name: raw_name,
19
+ assigned_aliases: assigned_aliases,
20
+ )
21
+
22
+ Models::Participant.new(
23
+ name: raw_name.to_s,
24
+ alias_name: alias_name,
25
+ )
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def data
32
+ collector.collection
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end