Class: SolidEvents::ApiController

Inherits:
ApplicationController show all
Defined in:
app/controllers/solid_events/api_controller.rb

Instance Method Summary collapse

Instance Method Details

#acknowledge_incidentObject



78
79
80
81
82
# File 'app/controllers/solid_events/api_controller.rb', line 78

def acknowledge_incident
  incident = SolidEvents::Incident.find(params[:id])
  incident.acknowledge!
  render json: serialize_incident(incident)
end

#assign_incidentObject



84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/solid_events/api_controller.rb', line 84

def assign_incident
  incident = SolidEvents::Incident.find(params[:id])
  incident.assign!(
    owner: params[:owner].presence,
    team: params[:team].presence,
    assigned_by: params[:assigned_by].presence,
    assignment_note: params[:assignment_note].presence
  )
  render json: serialize_incident(incident)
end

#causal_edgesObject



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'app/controllers/solid_events/api_controller.rb', line 343

def causal_edges
  return render json: {data: [], next_cursor: nil} unless causal_edges_table_available?

  scope = SolidEvents::CausalEdge.order(id: :desc)
  if params[:trace_id].present?
    trace_id = params[:trace_id].to_i
    scope = scope.where("from_trace_id = ? OR to_trace_id = ?", trace_id, trace_id)
  end
  scope = scope.where(edge_type: params[:edge_type].to_s) if params[:edge_type].present?
  scope = apply_cursor(scope)
  rows = scope.limit(limit_param)

  render json: {
    data: rows.map { |row| serialize_causal_edge(row) },
    next_cursor: rows.last&.id
  }
end

#cohort_metricsObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'app/controllers/solid_events/api_controller.rb', line 239

def cohort_metrics
  return render json: {error: "cohort_key is required"}, status: :unprocessable_entity if params[:cohort_key].blank?

  metric = metric_param
  cohort_key = params[:cohort_key].to_s
  requested_values = params[:cohort_values].to_s.split(",").map(&:strip).reject(&:blank?)

  rows = summary_scope_for_metrics.limit(10_000).pluck(:status, :duration_ms, :payload)
  grouped = Hash.new { |hash, key| hash[key] = {count: 0, error_count: 0, latency_sum: 0.0} }

  rows.each do |status, duration_ms, payload|
    context = payload.to_h["context"].to_h
    cohort_value = context[cohort_key]
    next if cohort_value.blank?

    cohort_value = cohort_value.to_s
    next if requested_values.any? && !requested_values.include?(cohort_value)

    grouped[cohort_value][:count] += 1
    grouped[cohort_value][:error_count] += 1 if status == "error"
    grouped[cohort_value][:latency_sum] += duration_ms.to_f
  end

  groups = grouped.map do |cohort_value, stats|
    value = if metric == "latency_avg"
      stats[:count].positive? ? (stats[:latency_sum] / stats[:count]).round(2) : 0.0
    else
      stats[:count].positive? ? ((stats[:error_count].to_f / stats[:count]) * 100.0).round(2) : 0.0
    end

    {
      cohort_key: cohort_key,
      cohort_value: cohort_value,
      metric: metric,
      value: value,
      sample_count: stats[:count],
      error_count: stats[:error_count]
    }
  end.sort_by { |row| [-row[:sample_count], row[:cohort_value]] }

  render json: {
    window: metric_window_param,
    cohort_key: cohort_key,
    metric: metric,
    groups: groups.first(limit_param)
  }
end

#compare_metricsObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/controllers/solid_events/api_controller.rb', line 200

def compare_metrics
  dimension = metric_dimension_param
  metric = metric_param
  windows = metric_compare_windows

  scoped = summary_scope_base_for_metrics
  current_stats = grouped_metric_stats(scope: scoped.where(started_at: windows[:current_start]..windows[:current_end]), dimension: dimension)
  baseline_stats = grouped_metric_stats(scope: scoped.where(started_at: windows[:baseline_start]..windows[:baseline_end]), dimension: dimension)
  values = (current_stats.keys + baseline_stats.keys).uniq

  groups = values.map do |value|
    current = current_stats.fetch(value, default_metric_stats)
    baseline = baseline_stats.fetch(value, default_metric_stats)
    current_value = metric_value_for(metric, current)
    baseline_value = metric_value_for(metric, baseline)
    delta = (current_value - baseline_value).round(2)

    {
      dimension: dimension,
      value: value,
      metric: metric,
      current: current_value,
      baseline: baseline_value,
      delta: delta,
      delta_pct: percent_delta(current_value, baseline_value),
      current_sample_count: current[:total_count],
      baseline_sample_count: baseline[:total_count]
    }
  end.sort_by { |row| [-row[:current_sample_count].to_i, row[:value].to_s] }

  render json: {
    dimension: dimension,
    metric: metric,
    current_window: windows[:current_window],
    baseline_window: windows[:baseline_window],
    groups: groups.first(limit_param)
  }
end

#error_ratesObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/solid_events/api_controller.rb', line 150

def error_rates
  dimension = metric_dimension_param
  groups = summary_scope_for_metrics
    .group(dimension)
    .order(Arel.sql("COUNT(*) DESC"))
    .limit(limit_param)
    .count

  data = groups.map do |value, total|
    scoped = summary_scope_for_metrics.where(dimension => value)
    error_count = scoped.where(status: "error").count
    {
      dimension: dimension,
      value: value,
      total_count: total,
      error_count: error_count,
      error_rate_pct: total.positive? ? ((error_count.to_f / total) * 100.0).round(2) : 0.0
    }
  end

  render json: {window: metric_window_param, dimension: dimension, groups: data}
end

#export_incidentsObject



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'app/controllers/solid_events/api_controller.rb', line 385

def export_incidents
  return render json: {error: "only json export is supported"}, status: :unprocessable_entity unless export_json?

  scope = SolidEvents::Incident.order(id: :desc)
  scope = scope.where(status: params[:status]) if params[:status].present?
  scope = scope.where(kind: params[:kind]) if params[:kind].present?
  scope = scope.where(severity: params[:severity]) if params[:severity].present?
  scope = scope.where("detected_at >= ?", window_start_for_metrics) if params[:window].present?
  scope = apply_cursor(scope)
  incidents = scope.limit(limit_param)

  render json: {
    exported_at: Time.current.iso8601,
    format: "json",
    filters: export_filters_payload,
    data: incidents.map { |incident| serialize_incident(incident) }
  }
end

#export_tracesObject



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'app/controllers/solid_events/api_controller.rb', line 361

def export_traces
  return render json: {error: "only json export is supported"}, status: :unprocessable_entity unless export_json?

  scope = SolidEvents::Trace.order(id: :desc)
  scope = scope.where(status: params[:status]) if params[:status].in?(%w[ok error])
  scope = scope.where("started_at >= ?", window_start_for_metrics) if params[:window].present?
  scope = scope.left_outer_joins(:summary).where(solid_events_summaries: {error_fingerprint: params[:error_fingerprint]}) if params[:error_fingerprint].present?
  if params[:entity_type].present? || params[:entity_id].present?
    scope = scope.left_outer_joins(:summary)
    scope = scope.where("solid_events_summaries.entity_type ILIKE ?", "%#{params[:entity_type]}%") if params[:entity_type].present?
    scope = scope.where(solid_events_summaries: {entity_id: params[:entity_id].to_i}) if params[:entity_id].present?
  end
  scope = apply_feature_slice_filter(scope)
  scope = apply_cursor(scope)

  traces = scope.includes(:summary).limit(limit_param)
  render json: {
    exported_at: Time.current.iso8601,
    format: "json",
    filters: export_filters_payload,
    data: traces.map(&:canonical_event)
  }
end

#incident_contextObject



34
35
36
37
# File 'app/controllers/solid_events/api_controller.rb', line 34

def incident_context
  incident = SolidEvents::Incident.find(params[:id])
  render json: context_payload_for(incident)
end

#incident_eventsObject



39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/solid_events/api_controller.rb', line 39

def incident_events
  incident = SolidEvents::Incident.find(params[:id])
  events = incident.incident_events.recent.limit(limit_param)
  events = events.where(action: params[:event_action].to_s) if params[:event_action].present?
  events = apply_cursor(events)
  render json: {
    incident: serialize_incident(incident),
    data: events.map { |event| serialize_incident_event(event) },
    next_cursor: events.last&.id
  }
end

#incident_evidencesObject



51
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
# File 'app/controllers/solid_events/api_controller.rb', line 51

def incident_evidences
  incident = SolidEvents::Incident.find(params[:id])
  traces = incident_related_traces(incident).includes(:summary).limit(2_000)
  summaries = traces.map(&:summary).compact

  by_source = summaries.group_by(&:source).transform_values(&:size).sort_by { |_, count| -count }.first(10).to_h
  by_status = summaries.group_by(&:status).transform_values(&:size)
  by_entity = summaries
    .map { |summary| [summary.entity_type, summary.entity_id] }
    .reject { |type, id| type.blank? || id.blank? }
    .tally
    .sort_by { |(_, count)| -count }
    .first(10)
    .map { |(type, id), count| {entity_type: type, entity_id: id, count: count} }

  render json: {
    incident: serialize_incident(incident),
    evidences: {
      by_source: by_source,
      by_status: by_status,
      by_entity: by_entity,
      duration_ms: duration_slice_for(summaries),
      error_rate_pct: error_rate_for(summaries)
    }
  }
end

#incident_tracesObject



25
26
27
28
29
30
31
32
# File 'app/controllers/solid_events/api_controller.rb', line 25

def incident_traces
  incident = SolidEvents::Incident.find(params[:id])
  traces = incident_related_traces(incident).limit(limit_param)
  render json: {
    incident: serialize_incident(incident),
    traces: traces.map(&:canonical_event)
  }
end

#incidentsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/solid_events/api_controller.rb', line 7

def incidents
  incidents = if incident_table_available?
    scope = SolidEvents::Incident.order(id: :desc)
    scope = scope.where(status: params[:status]) if params[:status].present?
    scope = scope.where(kind: params[:kind]) if params[:kind].present?
    scope = scope.where(severity: params[:severity]) if params[:severity].present?
    scope = apply_cursor(scope)
    scope.limit(limit_param)
  else
    []
  end

  render json: {
    data: incidents.map { |incident| serialize_incident(incident) },
    next_cursor: incidents.last&.id
  }
end

#journeysObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'app/controllers/solid_events/api_controller.rb', line 287

def journeys
  scope = summary_scope_for_metrics
  scope = scope.where(request_id: params[:request_id].to_s) if params[:request_id].present?
  if params[:entity_type].present?
    scope = scope.where(entity_type: params[:entity_type].to_s)
  end
  if params[:entity_id].present?
    scope = scope.where(entity_id: params[:entity_id].to_i)
  end
  scope = scope.where(status: "error") if errors_only_param?

  traces_per_journey = [[params[:traces_per_journey].to_i, 1].max, 50].min
  traces_per_journey = 20 if params[:traces_per_journey].blank?
  rows = scope.includes(:trace).order(started_at: :desc).limit(2_000).to_a
  grouped = rows.group_by { |summary| journey_key_for(summary) }.reject { |key, _| key.blank? }

  journeys = grouped.map do |key, summaries|
    ordered = summaries.sort_by(&:started_at)
    traces = ordered.last(traces_per_journey).map { |summary| summary.trace.canonical_event }
    {
      journey_key: key,
      request_id: ordered.last.request_id,
      entity_type: ordered.last.entity_type,
      entity_id: ordered.last.entity_id,
      trace_count: summaries.size,
      error_count: summaries.count { |summary| summary.status == "error" },
      started_at: ordered.first.started_at,
      finished_at: ordered.last.finished_at || ordered.last.started_at,
      traces: traces
    }
  end

  sorted = journeys.sort_by { |journey| journey[:finished_at] || Time.at(0) }.reverse.first(limit_param)
  render json: {window: metric_window_param, errors_only: errors_only_param?, journeys: sorted}
end

#latencyObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/solid_events/api_controller.rb', line 173

def latency
  dimension = metric_dimension_param
  groups = summary_scope_for_metrics
    .where.not(duration_ms: nil)
    .group(dimension)
    .order(Arel.sql("COUNT(*) DESC"))
    .limit(limit_param)
    .pluck(
      dimension,
      Arel.sql("COUNT(*)"),
      Arel.sql("AVG(duration_ms)"),
      Arel.sql("MAX(duration_ms)")
    )

  data = groups.map do |value, total_count, avg_duration, max_duration|
    {
      dimension: dimension,
      value: value,
      sample_count: total_count.to_i,
      avg_duration_ms: avg_duration.to_f.round(2),
      max_duration_ms: max_duration.to_f.round(2)
    }
  end

  render json: {window: metric_window_param, dimension: dimension, groups: data}
end

#materialized_journeysObject



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'app/controllers/solid_events/api_controller.rb', line 323

def materialized_journeys
  return render json: {data: [], next_cursor: nil} unless journey_table_available?

  scope = SolidEvents::Journey.order(id: :desc)
  scope = scope.where(request_id: params[:request_id].to_s) if params[:request_id].present?
  if params[:entity_type].present?
    scope = scope.where(entity_type: params[:entity_type].to_s)
  end
  if params[:entity_id].present?
    scope = scope.where(entity_id: params[:entity_id].to_i)
  end
  scope = apply_cursor(scope)
  rows = scope.limit(limit_param)

  render json: {
    data: rows.map { |row| serialize_materialized_journey(row) },
    next_cursor: rows.last&.id
  }
end

#mute_incidentObject



95
96
97
98
99
100
101
# File 'app/controllers/solid_events/api_controller.rb', line 95

def mute_incident
  incident = SolidEvents::Incident.find(params[:id])
  minutes = params[:minutes].to_i
  minutes = 60 if minutes <= 0
  incident.mute_for!(minutes.minutes)
  render json: serialize_incident(incident)
end

#reopen_incidentObject



113
114
115
116
117
# File 'app/controllers/solid_events/api_controller.rb', line 113

def reopen_incident
  incident = SolidEvents::Incident.find(params[:id])
  incident.reopen!
  render json: serialize_incident(incident)
end

#resolve_incidentObject



103
104
105
106
107
108
109
110
111
# File 'app/controllers/solid_events/api_controller.rb', line 103

def resolve_incident
  incident = SolidEvents::Incident.find(params[:id])
  if params[:resolved_by].present? || params[:resolution_note].present?
    incident.resolve_with!(resolved_by: params[:resolved_by].presence || "system", resolution_note: params[:resolution_note].presence)
  else
    incident.resolve!
  end
  render json: serialize_incident(incident)
end

#traceObject



119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/solid_events/api_controller.rb', line 119

def trace
  trace = SolidEvents::Trace.includes(:summary, :events, :record_links, :error_links).find(params[:id])

  render json: {
    trace: trace.canonical_event,
    summary: trace.summary&.attributes,
    record_links: trace.record_links.map { |link| {record_type: link.record_type, record_id: link.record_id} },
    error_links: trace.error_links.map { |link| {solid_error_id: link.solid_error_id} }
  }
end

#tracesObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/solid_events/api_controller.rb', line 130

def traces
  scope = SolidEvents::Trace.order(id: :desc)
  if params[:error_fingerprint].present?
    scope = scope.left_outer_joins(:summary).where(solid_events_summaries: {error_fingerprint: params[:error_fingerprint]})
  end
  if params[:entity_type].present? || params[:entity_id].present?
    scope = scope.left_outer_joins(:summary)
    scope = scope.where("solid_events_summaries.entity_type ILIKE ?", "%#{params[:entity_type]}%") if params[:entity_type].present?
    scope = scope.where(solid_events_summaries: {entity_id: params[:entity_id].to_i}) if params[:entity_id].present?
  end
  scope = apply_feature_slice_filter(scope)

  scope = apply_cursor(scope)
  traces = scope.includes(:summary).limit(limit_param)
  render json: {
    data: traces.map { |trace| trace.canonical_event },
    next_cursor: traces.last&.id
  }
end