Skip to content

Commit

Permalink
we can render a simple "event table".
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 18, 2024
1 parent 59523cd commit 38b4430
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 87 deletions.
17 changes: 13 additions & 4 deletions lib/trailblazer/workflow/discovery/present.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Trailblazer
module Workflow
module Discovery
# Rendering-specific code using {Discovery:states}.
# https://stackoverflow.com/questions/22885702/html-for-the-pause-symbol-in-audio-and-video-control
module Present
module_function

Expand All @@ -14,11 +15,9 @@ def label_for_next_task(activity, catch_event)

def readable_name_for_catch_event(activity, catch_event, lanes_cfg: {})
envelope_icon = "(✉)➔" # TODO: implement {envelope_icon} flag.
envelope_icon = ""
envelope_icon = "⏵︎"

lane_options = lane_options_for(activity, catch_event, lanes_cfg: lanes_cfg)
lane_name = lane_options[:label]
lane_label = lane_options[:icon] #if lane_icons.key?(lane_name) # TODO: handle default!
lane_label = lane_label_for(activity, catch_event, lanes_cfg: lanes_cfg)

event_label = label_for_next_task(activity, catch_event)

Expand All @@ -36,7 +35,17 @@ def lane_options_for(activity, task, lanes_cfg:)
lanes_cfg.values.find { |options| options[:activity] == activity } or raise
end

#if lane_icons.key?(lane_name) # TODO: handle default!
def lane_label_for(activity, task, lanes_cfg:, show_icon: true)
lane_options_for(activity, task, lanes_cfg: lanes_cfg)[:icon]
end

# def readable_id_label(activity, task, **options)
# id = Trailblazer::Activity::Introspect.Nodes(activity, task: task).id
# lane_label = lane_label_for(activity, task, **options)

# %(#{lane_label} #{id})
# end
end
end
end
Expand Down
88 changes: 87 additions & 1 deletion lib/trailblazer/workflow/discovery/present/event_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,94 @@ module Present
module EventTable
module_function

def call(discovered_states, render_ids: true, hide_lanes: [], lanes_cfg:, **)
rows = discovered_states.flat_map do |row|
positions_before, start_position = row[:positions_before]

lane_positions = render_lane_position_columns(positions_before, lanes_cfg: lanes_cfg)

# The resulting hash represents one row.
state_row = Hash[
"triggered event",
Present.readable_name_for_catch_event(*start_position.to_a, lanes_cfg: lanes_cfg),

# "triggered catch event",
# readable_name_for_catch_event(row[:start_position]),

*lane_positions
]

rows = [
state_row,
]

# FIXME: use developer for coloring.
# def bg_gray(str); "\e[47m#{str}\e[0m" end
if render_ids
rows << render_lane_position_id_columns(positions_before, lanes_cfg: lanes_cfg)
end

rows
end


pp rows
lane_labels = lanes_cfg.collect { |id, cfg| cfg[:label] }

lane_labels = lane_labels - hide_lanes # TODO: extract, new feature.

Hirb::Helpers::Table.render(rows, fields: [
# "event name",
"triggered event",
*lane_labels,
],
max_width: 186,
) # 186 for laptop 13"
end

# @private
def render_lane_position_columns(positions_before, **options)
lane_positions = positions_before.to_a.flat_map do |lane_position|
lane_label = Present.lane_options_for(*lane_position.to_a, **options)[:label]

# DISCUSS: extract?
if lane_position.task.to_h["resumes"].nil? # Terminus.
readable_lane_position = "◉End.#{lane_position.task.to_h[:semantic]}"
else
readable_lane_position = Present.resumes_from_suspend(*lane_position.to_a).collect do |catch_event|
Present.readable_name_for_catch_event(lane_position.activity, catch_event, **options)
end.join(",")
end

[
lane_label,
readable_lane_position
]
end
end

def render_lane_position_id_columns(positions_before, **options)
lane_position_ids = positions_before.to_a.flat_map do |lane_position|
# Present.readable_id_label(*lane_position.to_a, **options)
lane_label = lane_label_for(lane_position, **options) # FIXME: redundant in {#render_lane_position_columns}
id = Trailblazer::Activity::Introspect.Nodes(lane_position.activity, task: lane_position.task).id

[
lane_label, # column name
id
]
end

_id_row = Hash[
*lane_position_ids, # TODO: this adds the remaining IDs.
]
end

def lane_label_for(lane_position, **options)
Present.lane_options_for(*lane_position.to_a, **options)[:label]
end
end
end
end # Present
end
end
end
4 changes: 1 addition & 3 deletions lib/trailblazer/workflow/discovery/present/state_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ module StateTable
# Each row represents a configuration of suspends aka "state".
# The state knows its possible resume events.
# does the state know which state fields belong to it?
#
# TODO: move that to separate module {StateTable.call}.
def call(discovered_states, lanes_cfg:)
# raise discovery_state_table.inspect
start_position_to_catch = {}
Expand All @@ -36,7 +34,7 @@ def call(discovered_states, lanes_cfg:)
cli_rows = states.flat_map do |configuration, catch_events|
suggested_state_name = suggested_state_name_for(catch_events)

suggested_state_name = " #{suggested_state_name}".inspect
suggested_state_name = "⏸︎ #{suggested_state_name}".inspect

triggerable_events = catch_events
.collect { |event_position| Present.readable_name_for_catch_event(*event_position.to_a, lanes_cfg: lanes_cfg).inspect }
Expand Down
66 changes: 0 additions & 66 deletions lib/trailblazer/workflow/state/discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,73 +94,7 @@ def self.readable_name_for_resume_event(position, tuple: false, lane_icons: {})
"#{lane_label} [#{catch_events}]"
end

def self.render_cli_event_table(discovery_state_table, render_ids: false, hide_lanes: [])
rows = discovery_state_table.flat_map do |row|
start_lane_id, start_lane_task_id = row[:start_position][:tuple]

lane_positions = row[:lane_positions].flat_map do |lane_position|
lane_id, suspend_id = lane_position[:tuple]
comment = lane_position[:comment][1]

[
lane_id,
comment
]
end

# The resulting hash represents one row.
state_row = Hash[
"event name",
row[:event_name].inspect,

"triggered catch event",
readable_name_for_catch_event(row[:start_position]),

*lane_positions
]

rows = [
state_row,
]

# FIXME: use developer for coloring.
# def bg_gray(str); "\e[47m#{str}\e[0m" end

# TODO: optional feature, extract!
if render_ids
lane_position_ids = row[:lane_positions].flat_map do |lane_position|
tuple = lane_position[:tuple]

# [tuple[0], "\e[34m#{tuple[1]}\e[0m"] # FIXME: when entry is shortened by Hirb, the stop byte gets lost.
tuple
end

id_row = Hash[
"triggered catch event",
"\e[34m#{row[:start_position][:tuple][1]}\e[0m",

*lane_position_ids, # TODO: this adds the remaining IDs.
]

rows << id_row
end


rows
end

lane_ids = discovery_state_table[0][:lane_positions].collect { |lane_position| lane_position[:tuple][0] }

lane_ids = lane_ids - hide_lanes # TODO: extract, new feature.

Hirb::Helpers::Table.render(rows, fields: [
"event name",
"triggered catch event",
*lane_ids,
],
max_width: 186,
) # 186 for laptop 13"
end

def self.serialize_comment(event_name)
["before", event_name]
Expand Down
69 changes: 56 additions & 13 deletions test/discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,63 @@ def assert_position_after(actual_configuration, expected_ids, lanes:)
cli_state_table = Trailblazer::Workflow::Discovery::Present::StateTable.(states, lanes_cfg: lanes_cfg)
puts cli_state_table
assert_equal cli_state_table,
%(+---------------------------------+----------------------------------------+
| state name | triggerable events |
+---------------------------------+----------------------------------------+
| " Create form" | "☝ ▶Create form" |
| " Create" | "☝ ▶Create" |
| " Update form/Notify approver" | "☝ ▶Update form", "☝ ▶Notify approver" |
| " Update" | "☝ ▶Update" |
| " Delete? form/Publish" | "☝ ▶Delete? form", "☝ ▶Publish" |
| " Revise form" | "☝ ▶Revise form" |
| " Delete/Cancel" | "☝ ▶Delete", "☝ ▶Cancel" |
| " Archive" | "☝ ▶Archive" |
| " Revise" | "☝ ▶Revise" |
+---------------------------------+----------------------------------------+
%(+----------------------------------+------------------------------------------+
| state name | triggerable events |
+----------------------------------+------------------------------------------+
| "⏸︎ Create form" | "☝ ⏵︎Create form" |
| "⏸︎ Create" | "☝ ⏵︎Create" |
| "⏸︎ Update form/Notify approver" | "☝ ⏵︎Update form", "☝ ⏵︎Notify approver" |
| "⏸︎ Update" | "☝ ⏵︎Update" |
| "⏸︎ Delete? form/Publish" | "☝ ⏵︎Delete? form", "☝ ⏵︎Publish" |
| "⏸︎ Revise form" | "☝ ⏵︎Revise form" |
| "⏸︎ Delete/Cancel" | "☝ ⏵︎Delete", "☝ ⏵︎Cancel" |
| "⏸︎ Archive" | "☝ ⏵︎Archive" |
| "⏸︎ Revise" | "☝ ⏵︎Revise" |
+----------------------------------+------------------------------------------+
9 rows in set)

end

it "{Present::EventTable.call}" do
states, lanes_sorted, lanes_cfg = self.states()

cli_state_table_with_ids = Trailblazer::Workflow::Discovery::Present::EventTable.(states, render_ids: true, hide_lanes: ["approver"], lanes_cfg: lanes_cfg)
puts cli_state_table_with_ids
assert_equal cli_state_table_with_ids,
%(+---------------------+---------------------------------------------+----------------------------------------------+
| triggered event | lifecycle | UI |
+---------------------+---------------------------------------------+----------------------------------------------+
| ☝ ⏵︎Create form | ⛾ ⏵︎Create | ☝ ⏵︎Create form |
| | suspend-gw-to-catch-before-Activity_0wwfenp | suspend-gw-to-catch-before-Activity_0wc2mcq |
| ☝ ⏵︎Create | ⛾ ⏵︎Create | ☝ ⏵︎Create |
| | suspend-gw-to-catch-before-Activity_0wwfenp | suspend-Gateway_14h0q7a |
| ☝ ⏵︎Create | ⛾ ⏵︎Create | ☝ ⏵︎Create |
| | suspend-gw-to-catch-before-Activity_0wwfenp | suspend-Gateway_14h0q7a |
| ☝ ⏵︎Update form | ⛾ ⏵︎Update,⛾ ⏵︎Notify approver | ☝ ⏵︎Update form,☝ ⏵︎Notify approver |
| | suspend-Gateway_0fnbg3r | suspend-Gateway_0kknfje |
| ☝ ⏵︎Notify approver | ⛾ ⏵︎Update,⛾ ⏵︎Notify approver | ☝ ⏵︎Update form,☝ ⏵︎Notify approver |
| | suspend-Gateway_0fnbg3r | suspend-Gateway_0kknfje |
| ☝ ⏵︎Update | ⛾ ⏵︎Update,⛾ ⏵︎Notify approver | ☝ ⏵︎Update |
| | suspend-Gateway_0fnbg3r | suspend-Gateway_0nxerxv |
| ☝ ⏵︎Notify approver | ⛾ ⏵︎Update,⛾ ⏵︎Notify approver | ☝ ⏵︎Update form,☝ ⏵︎Notify approver |
| | suspend-Gateway_0fnbg3r | suspend-Gateway_0kknfje |
| ☝ ⏵︎Delete? form | ⛾ ⏵︎Publish,⛾ ⏵︎Delete,⛾ ⏵︎Update | ☝ ⏵︎Update form,☝ ⏵︎Delete? form,☝ ⏵︎Publish |
| | suspend-Gateway_1hp2ssj | suspend-Gateway_1sq41iq |
| ☝ ⏵︎Publish | ⛾ ⏵︎Publish,⛾ ⏵︎Delete,⛾ ⏵︎Update | ☝ ⏵︎Update form,☝ ⏵︎Delete? form,☝ ⏵︎Publish |
| | suspend-Gateway_1hp2ssj | suspend-Gateway_1sq41iq |
| ☝ ⏵︎Update | ⛾ ⏵︎Update,⛾ ⏵︎Notify approver | ☝ ⏵︎Update |
| | suspend-Gateway_0fnbg3r | suspend-Gateway_0nxerxv |
| ☝ ⏵︎Revise form | ⛾ ⏵︎Revise | ☝ ⏵︎Revise form |
| | suspend-Gateway_01p7uj7 | suspend-gw-to-catch-before-Activity_0zsock2 |
| ☝ ⏵︎Delete | ⛾ ⏵︎Publish,⛾ ⏵︎Delete,⛾ ⏵︎Update | ☝ ⏵︎Delete,☝ ⏵︎Cancel |
| | suspend-Gateway_1hp2ssj | suspend-Gateway_100g9dn |
| ☝ ⏵︎Cancel | ⛾ ⏵︎Publish,⛾ ⏵︎Delete,⛾ ⏵︎Update | ☝ ⏵︎Delete,☝ ⏵︎Cancel |
| | suspend-Gateway_1hp2ssj | suspend-Gateway_100g9dn |
| ☝ ⏵︎Archive | ⛾ ⏵︎Archive | ☝ ⏵︎Archive |
| | suspend-gw-to-catch-before-Activity_1hgscu3 | suspend-gw-to-catch-before-Activity_0fy41qq |
| ☝ ⏵︎Revise | ⛾ ⏵︎Revise | ☝ ⏵︎Revise |
| | suspend-Gateway_01p7uj7 | suspend-Gateway_1xs96ik |
+---------------------+---------------------------------------------+----------------------------------------------+
30 rows in set)
end
end

0 comments on commit 38b4430

Please sign in to comment.