Module: SagaForge::Dashboard::DashboardHelper

Includes:
ActionView::Helpers::DateHelper, ActionView::Helpers::TagHelper
Defined in:
app/helpers/saga_forge/dashboard/dashboard_helper.rb

Constant Summary collapse

STATE_COLORS =
{
  "compensating" => "amber", "compensated" => "slate",
  "cancelled" => "slate"
}.freeze

Instance Method Summary collapse

Instance Method Details

#absolute_time?Boolean

Whether the viewer prefers absolute timestamps (cookie-persisted nav toggle).

Returns:

  • (Boolean)


74
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 74

def absolute_time? = cookies[:sf_time_format] == "absolute"

#bar_width_class(pct) ⇒ Object



28
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 28

def bar_width_class(pct) = "sf-bar-#{pct.to_i.clamp(0, 100)}"

#format_bytes(n) ⇒ Object



30
31
32
33
34
35
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 30

def format_bytes(n)
  return "0 B" if n.to_i <= 0
  units = %w[B KB MB]
  e = [Math.log(n, 1024).floor, units.size - 1].min
  "#{(n.to_f / (1024**e)).round(1)} #{units[e]}"
end

#poll_intervalObject



37
38
39
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 37

def poll_interval
  cookies[:sf_poll_interval].presence&.to_i || SagaForge::Dashboard.config.polling_interval
end

#poll_label(secs) ⇒ Object



63
64
65
66
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 63

def poll_label(secs)
  return "off" if secs.zero?
  (secs % 60 == 0) ? "#{secs / 60}m" : "#{secs}s"
end

#poll_optionsObject

Auto-refresh interval options (seconds; 0 = off), from config.



61
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 61

def poll_options = SagaForge::Dashboard.config.polling_interval_options

#poll_region?Boolean

Whether the main region opts into auto-refresh. A page sets Cytoscape canvas can't survive the poll's morph region refresh).

Returns:

  • (Boolean)


71
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 71

def poll_region? = !@sf_disable_polling

#state_badge(state) ⇒ Object



16
17
18
19
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 16

def state_badge(state)
  color = STATE_COLORS[state.to_s] || (terminal_like?(state) ? "green" : "indigo")
  (:span, state, class: "sf-badge sf-badge-#{color}")
end

#status_badge(status) ⇒ Object



21
22
23
24
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 21

def status_badge(status)
  color = {"pending" => "slate", "processed" => "green", "stalled" => "amber", "failed" => "red"}[status.to_s] || "slate"
  (:span, status, class: "sf-badge sf-badge-#{color}")
end

#terminal_like?(state) ⇒ Boolean

Returns:

  • (Boolean)


26
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 26

def terminal_like?(state) = %w[completed done finished shipped notified].include?(state.to_s)

#time_tag(t) ⇒ Object

Rendered relative ("3 minutes ago") or absolute (UTC) per the viewer's sf_time_format cookie preference, with the other form available on hover. This is decided server-side at render time; there's no client-side relative-time JS; the toggle button (dashboard.js) just writes the cookie and does a Turbo reload, which re-renders every time_tag on the page with the new preference.



47
48
49
50
51
52
53
54
# File 'app/helpers/saga_forge/dashboard/dashboard_helper.rb', line 47

def time_tag(t)
  return "" unless t
  absolute = t.utc.strftime("%Y-%m-%d %H:%M:%S UTC")
  relative = "#{time_ago_in_words(t)} ago"
  shown = absolute_time? ? absolute : relative
  title = absolute_time? ? relative : absolute
  (:time, shown, datetime: t.iso8601, class: "sf-time", title: title)
end