Class: Lyra::EventFlow
- Inherits:
-
Object
- Object
- Lyra::EventFlow
- Defined in:
- lib/lyra/event_flow.rb
Overview
Visualizes event flows and chains
Instance Attribute Summary collapse
-
#subject_id ⇒ Object
readonly
Returns the value of attribute subject_id.
-
#subject_type ⇒ Object
readonly
Returns the value of attribute subject_type.
-
#time_range ⇒ Object
readonly
Returns the value of attribute time_range.
Instance Method Summary collapse
-
#build_flows(grouped_events) ⇒ Object
Build event flows showing cause and effect.
-
#build_timeline(events) ⇒ Object
Build timeline view showing CRUD operations and their events.
-
#crud_to_event_mapping(model_class, operation, model_id = nil) ⇒ Object
Show how a single CRUD operation maps to events.
-
#data_lineage(field_name, model_class = nil) ⇒ Object
Visualize data lineage for GDPR compliance.
-
#flow_data ⇒ Object
Get complete event flow for visualization.
-
#initialize(subject_id: nil, subject_type: nil, time_range: nil) ⇒ EventFlow
constructor
A new instance of EventFlow.
-
#privacy_impact_analysis ⇒ Object
Analyze privacy impact of event chains.
-
#reconstruct_state_chain(model_class, model_id) ⇒ Object
Reconstruct state from event chain.
Constructor Details
#initialize(subject_id: nil, subject_type: nil, time_range: nil) ⇒ EventFlow
Returns a new instance of EventFlow.
6 7 8 9 10 |
# File 'lib/lyra/event_flow.rb', line 6 def initialize(subject_id: nil, subject_type: nil, time_range: nil) @subject_id = subject_id @subject_type = subject_type @time_range = time_range || (30.days.ago..Time.current) end |
Instance Attribute Details
#subject_id ⇒ Object (readonly)
Returns the value of attribute subject_id.
4 5 6 |
# File 'lib/lyra/event_flow.rb', line 4 def subject_id @subject_id end |
#subject_type ⇒ Object (readonly)
Returns the value of attribute subject_type.
4 5 6 |
# File 'lib/lyra/event_flow.rb', line 4 def subject_type @subject_type end |
#time_range ⇒ Object (readonly)
Returns the value of attribute time_range.
4 5 6 |
# File 'lib/lyra/event_flow.rb', line 4 def time_range @time_range end |
Instance Method Details
#build_flows(grouped_events) ⇒ Object
Build event flows showing cause and effect
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lyra/event_flow.rb', line 45 def build_flows(grouped_events) grouped_events.map do |correlation_id, events| { correlation_id: correlation_id, started_at: (events.min_by { |e| (e) }), completed_at: (events.max_by { |e| (e) }), duration: calculate_duration(events), user_action: events.first.[:user_action], user_id: events.first.[:user_id], events_chain: build_event_chain(events), crud_operations: extract_crud_operations(events), privacy_impact: calculate_flow_privacy_impact(events) } end end |
#build_timeline(events) ⇒ Object
Build timeline view showing CRUD operations and their events
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lyra/event_flow.rb', line 26 def build_timeline(events) events.sort_by { |e| (e) }.map do |event| { event_id: event.event_id, timestamp: (event), operation: event_operation(event), model_class: event_model_class(event), model_id: event_model_id(event), correlation_id: event.[:correlation_id], action_id: event.[:action_id], user_action: event.[:user_action], changes: event_changes(event), pii_fields: detect_pii(event), grouped_with: find_grouped_events(event, events) } end end |
#crud_to_event_mapping(model_class, operation, model_id = nil) ⇒ Object
Show how a single CRUD operation maps to events
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lyra/event_flow.rb', line 62 def crud_to_event_mapping(model_class, operation, model_id = nil) events = load_events.select do |event| matches = event_model_class(event) == model_class && event_operation(event) == operation matches &&= event_model_id(event) == model_id if model_id matches end { crud_operation: { model: model_class, operation: operation, record_id: model_id }, generated_events: events.map { |e| event_summary(e) }, count: events.count, timeline: events.sort_by { |e| (e) }.map do |e| { timestamp: (e), event_type: e.class.name, event_id: e.event_id, data: e.data } end } end |
#data_lineage(field_name, model_class = nil) ⇒ Object
Visualize data lineage for GDPR compliance
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 165 166 167 |
# File 'lib/lyra/event_flow.rb', line 131 def data_lineage(field_name, model_class = nil) events = load_events if model_class events = events.select { |e| event_model_class(e) == model_class } end lineage = [] events.each do |event| attrs = event_attributes(event) changes = event_changes(event) if attrs.key?(field_name) || attrs.key?(field_name.to_s) || changes.key?(field_name) || changes.key?(field_name.to_s) lineage << { timestamp: (event), event_id: event.event_id, model: event_model_class(event), record_id: event_model_id(event), operation: event_operation(event), old_value: changes.dig(field_name, 0) || changes.dig(field_name.to_s, 0), new_value: changes.dig(field_name, 1) || changes.dig(field_name.to_s, 1) || attrs[field_name] || attrs[field_name.to_s], source: event.[:source], user_id: event.[:user_id], action: event.[:user_action] } end end { field: field_name, model_class: model_class, total_modifications: lineage.count, first_seen: lineage.min_by { |l| l[:timestamp] }&.dig(:timestamp), last_modified: lineage.max_by { |l| l[:timestamp] }&.dig(:timestamp), lineage: lineage.sort_by { |l| l[:timestamp] } } end |
#flow_data ⇒ Object
Get complete event flow for visualization
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/lyra/event_flow.rb', line 13 def flow_data events = load_events grouped = group_by_correlation(events) { timeline: build_timeline(events), flows: build_flows(grouped), statistics: calculate_statistics(events), privacy_impact: analyze_privacy_impact(events) } end |
#privacy_impact_analysis ⇒ Object
Analyze privacy impact of event chains
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/lyra/event_flow.rb', line 170 def privacy_impact_analysis events = load_events pii_inventory = extract_pii_from_events(events) { total_events: events.count, events_with_pii: events.count { |e| has_pii?(e) }, pii_categories: pii_inventory.keys, pii_fields_count: pii_inventory.values.sum(&:count), sensitive_operations: identify_sensitive_operations(events), data_flows: trace_data_flows(events), risk_assessment: assess_privacy_risk(events, pii_inventory) } end |
#reconstruct_state_chain(model_class, model_id) ⇒ Object
Reconstruct state from event chain
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 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/lyra/event_flow.rb', line 89 def reconstruct_state_chain(model_class, model_id) stream_name = "#{model_class}$#{model_id}" events = Lyra.config.event_store.read.stream(stream_name).to_a chain = [] state = {} events.sort_by { |e| (e) }.each do |event| previous_state = state.dup case event_operation(event) when :created state = event_attributes(event).dup when :updated event_changes(event).each { |k, (old, new)| state[k] = new } when :destroyed state[:_deleted] = true state[:_deleted_at] = (event) end chain << { event_id: event.event_id, timestamp: (event), operation: event_operation(event), previous_state: previous_state, changes: event_changes(event), new_state: state.dup, pii_changed: identify_pii_changes(previous_state, state), user_action: event.[:user_action] } end { model: { class: model_class, id: model_id }, initial_state: {}, final_state: state, events_count: events.count, state_evolution: chain } end |