Module: SolidObserver::ApplicationHelper

Defined in:
app/helpers/solid_observer/application_helper.rb

Constant Summary collapse

STATUS_COLORS =
{
  "completed" => "success",
  "ready" => "success",
  "failed" => "danger",
  "retry_stopped" => "danger",
  "scheduled" => "warning",
  "claimed" => "warning",
  "enqueued" => "info",
  "discarded" => "info"
}.freeze
DURATION_SEMANTICS =
{
  "job_enqueued" => "Time spent in the ActiveJob enqueue call (Rails internal; typically sub-millisecond to single-digit ms)",
  "job_completed" => "Time spent performing the job",
  "job_failed" => "Time spent performing the job before the exception was raised",
  "job_discarded" => "Time before discard decision was made"
}.freeze
STABILITY_STATES =
{
  stable: {label: "Stable", tone: "success"},
  degraded: {label: "Degraded", tone: "warning"},
  critical: {label: "Critical", tone: "danger"}
}.freeze

Instance Method Summary collapse

Instance Method Details

#duration_with_semantic(value, event_type) ⇒ Object



47
48
49
50
51
# File 'app/helpers/solid_observer/application_helper.rb', line 47

def duration_with_semantic(value, event_type)
  return (:span, "", class: "so-text-muted") unless value

  (:abbr, format_duration(value), title: DURATION_SEMANTICS.fetch(event_type.to_s))
end

#execution_status(execution) ⇒ Object



27
28
29
# File 'app/helpers/solid_observer/application_helper.rb', line 27

def execution_status(execution)
  ExecutionPresenter.new(execution).status
end

#format_duration(seconds) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'app/helpers/solid_observer/application_helper.rb', line 31

def format_duration(seconds)
  return "0ms" if seconds.to_f.zero?

  if seconds < 1
    "#{(seconds * 1000).round}ms"
  else
    "#{"%.1f" % seconds}s"
  end
end

#latest_failure_phrase(timestamp) ⇒ Object



91
92
93
# File 'app/helpers/solid_observer/application_helper.rb', line 91

def latest_failure_phrase(timestamp)
  timestamp ? "#{time_ago_in_words(timestamp)} ago" : "unknown"
end

#mode_badgeObject



53
54
55
56
57
# File 'app/helpers/solid_observer/application_helper.rb', line 53

def mode_badge
  config = SolidObserver.config
  color = config.persistence_mode? ? "info" : "warning"
  (:span, config.storage_mode.to_s.capitalize, class: "so-badge so-badge--#{color}")
end

#stability_badge(stats) ⇒ Object



75
76
77
78
79
80
81
82
# File 'app/helpers/solid_observer/application_helper.rb', line 75

def stability_badge(stats)
  meta = STABILITY_STATES.fetch(stability_state(stats))
  dot = tag.svg(tag.circle(r: 3, cx: 3, cy: 3),
    class: "so-badge__dot", viewBox: "0 0 6 6", "aria-hidden": "true")
  tag.span(class: "so-badge so-badge--pill so-badge--#{meta[:tone]}") do
    safe_join([dot, meta[:label]], " ")
  end
end

#stability_detail(stats) ⇒ Object



84
85
86
87
88
89
# File 'app/helpers/solid_observer/application_helper.rb', line 84

def stability_detail(stats)
  failures_24h = stats[:failed_last_24h].to_i
  return "No failures in the last 24h" if failures_24h.zero?

  "#{pluralize(failures_24h, "failure")} in the last 24h, latest #{latest_failure_phrase(stats[:latest_failure_at])}"
end

#stability_state(stats) ⇒ Object



68
69
70
71
72
73
# File 'app/helpers/solid_observer/application_helper.rb', line 68

def stability_state(stats)
  return :critical if stats[:failed_last_hour].to_i.positive?
  return :degraded if stats[:failed_last_24h].to_i.positive?

  :stable
end

#status_badge(status) ⇒ Object



41
42
43
44
45
# File 'app/helpers/solid_observer/application_helper.rb', line 41

def status_badge(status)
  status_str = status.to_s
  color = STATUS_COLORS.fetch(status_str, "default")
  (:span, status_str.humanize, class: "so-badge so-badge--#{color}")
end

#turbo_frame_tag(id, **options, &block) ⇒ Object



59
60
61
62
63
64
65
66
# File 'app/helpers/solid_observer/application_helper.rb', line 59

def turbo_frame_tag(id, **options, &block)
  return super if defined?(super)

  content = options.delete(:content)
  body = block_given? ? capture(&block) : content

  (:"turbo-frame", body, **options.merge(id: id).compact)
end