Class: Lyra::FlowController

Inherits:
ApplicationController show all
Defined in:
app/controllers/lyra/flow_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#petri_flow_available?, #user_tracking_configured?

Instance Method Details

#correlation ⇒ Object

GET /lyra/flow/correlation/:correlation_id



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/lyra/flow_controller.rb', line 116

def correlation
  correlation_id = params[:correlation_id]

  events = Lyra.config.event_store.read.to_a.select do |event|
    event.[:correlation_id] == correlation_id
  end

  flow = EventFlow.new
  timeline = flow.build_timeline(events)

  render json: {
    correlation_id: correlation_id,
    events_count: events.count,
    started_at: events.min_by(&:timestamp)&.timestamp,
    completed_at: events.max_by(&:timestamp)&.timestamp,
    events: timeline,
    privacy_impact: pam_dsl_available? ? events.count { |e| Privacy::PIIDetector.detect(event_attributes(e)).any? } : nil
  }
end

#crud_mapping ⇒ Object

GET /lyra/flow/crud_mapping



52
53
54
55
56
57
58
59
60
61
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
87
88
# File 'app/controllers/lyra/flow_controller.rb', line 52

def crud_mapping
  model_class = params[:model_class]
  operation = params[:operation]&.to_sym
  model_id = params[:model_id]

  if model_class && operation
    # Specific mapping lookup
    flow = EventFlow.new
    mapping = flow.crud_to_event_mapping(model_class, operation, model_id)
    respond_to do |format|
      format.html { render json: mapping }
      format.json { render json: mapping }
    end
  else
    # Show summary of all CRUD operations
    events = Lyra.config.event_store.read.to_a
    @models_summary = events.group_by { |e| event_model_class(e) }.transform_values do |model_events|
      {
        total: model_events.count,
        operations: model_events.group_by { |e| event_operation(e) }.transform_values(&:count)
      }
    end
    @total_events = events.count
    @operations_summary = events.group_by { |e| event_operation(e) }.transform_values(&:count)

    respond_to do |format|
      format.html
      format.json do
        render json: {
          total_events: @total_events,
          operations: @operations_summary,
          models: @models_summary
        }
      end
    end
  end
end

#event_chain ⇒ Object

GET /lyra/flow/event_chain/:model_class/:model_id



41
42
43
44
45
46
47
48
49
# File 'app/controllers/lyra/flow_controller.rb', line 41

def event_chain
  model_class = params[:model_class].constantize
  model_id = params[:model_id]

  flow = EventFlow.new
  chain = flow.reconstruct_state_chain(model_class.name, model_id)

  render json: chain
end

#timeline ⇒ Object

GET /lyra/flow/timeline



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/lyra/flow_controller.rb', line 4

def timeline
  subject_id = params[:subject_id]
  subject_type = params[:subject_type]

  flow = EventFlow.new(
    subject_id: subject_id,
    subject_type: subject_type
  )

  @flow_data = flow.flow_data
  @timeline = @flow_data[:timeline] || []
  @flows = @flow_data[:flows] || []
  @statistics = @flow_data[:statistics] || {}
  @privacy_impact = @flow_data[:privacy_impact] || {}

  # Get available filter options
  @available_models = @timeline.map { |e| e[:model_class] }.compact.uniq.sort
  @available_operations = @timeline.map { |e| e[:operation] }.compact.uniq.sort

  # Apply filters
  @filter_model = params[:model]
  @filter_operation = params[:operation]

  if @filter_model.present?
    @timeline = @timeline.select { |e| e[:model_class] == @filter_model }
  end
  if @filter_operation.present?
    @timeline = @timeline.select { |e| e[:operation].to_s == @filter_operation }
  end

  respond_to do |format|
    format.html
    format.json { render json: @flow_data }
  end
end

#user_actions ⇒ Object

GET /lyra/flow/user_actions/:user_id



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
# File 'app/controllers/lyra/flow_controller.rb', line 137

def 
  user_id = params[:user_id]

  events = Lyra.config.event_store.read.to_a.select do |event|
    event.[:user_id] == user_id.to_i
  end

  actions = events.group_by { |e| e.[:action_id] }

  render json: {
    user_id: user_id,
    total_actions: actions.count,
    total_events: events.count,
    actions: actions.map do |action_id, action_events|
      {
        action_id: action_id,
        user_action: action_events.first.[:user_action],
        timestamp: event_timestamp(action_events.min_by { |e| event_timestamp(e) }),
        events_count: action_events.count,
        models_affected: action_events.map { |e| event_model_class(e) }.uniq,
        pii_affected: pam_dsl_available? ? action_events.any? { |e| Privacy::PIIDetector.detect(event_attributes(e)).any? } : nil
      }
    end
  }
end

#visualization ⇒ Object

GET /lyra/flow/visualization/:model_class/:model_id



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/lyra/flow_controller.rb', line 91

def visualization
  model_class = params[:model_class].constantize
  model_id = params[:model_id]
  format = params[:format] || 'json'

  stream_name = "#{model_class.name}$#{model_id}"
  events = Lyra.config.event_store.read.stream(stream_name).to_a

  timeline = Visualization::Timeline.new(events)

  case format
  when 'html'
    render html: timeline.to_html.html_safe
  when 'mermaid'
    render plain: timeline.to_mermaid
  when 'ascii'
    render plain: timeline.to_ascii
  when 'd3'
    render json: JSON.parse(timeline.to_d3_json)
  else
    render json: timeline.to_data
  end
end