Module: Pgbus::ApplicationHelper
- Defined in:
- app/helpers/pgbus/application_helper.rb
Constant Summary collapse
- LOCALE_NAMES =
{ da: "Dansk", de: "Deutsch", en: "English", es: "Espa\u00f1ol", fi: "Suomi", fr: "Fran\u00e7ais", it: "Italiano", ja: "\u65E5\u672C\u8A9E", nb: "Norsk", nl: "Nederlands", pt: "Portugu\u00eas", sv: "Svenska" }.freeze
Instance Method Summary collapse
- #pgbus_duration(seconds) ⇒ Object
- #pgbus_json_preview(json_string, max_length: 120) ⇒ Object
- #pgbus_locale_flag(code) ⇒ Object
- #pgbus_locale_name(code) ⇒ Object
- #pgbus_mobile_nav_link(label, path) ⇒ Object
- #pgbus_ms_duration(millis) ⇒ Object
- #pgbus_nav_link(label, path) ⇒ Object
- #pgbus_number(n) ⇒ Object
- #pgbus_parse_message(message) ⇒ Object
- #pgbus_paused_badge(paused) ⇒ Object
- #pgbus_queue_badge(name) ⇒ Object
- #pgbus_recurring_health_badge(task) ⇒ Object
- #pgbus_refresh_interval ⇒ Object
- #pgbus_status_badge(healthy) ⇒ Object
- #pgbus_time_ago(time) ⇒ Object
- #pgbus_time_ago_future(time) ⇒ Object
- #pgbus_time_range_label(minutes) ⇒ Object
Instance Method Details
#pgbus_duration(seconds) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/helpers/pgbus/application_helper.rb', line 52 def pgbus_duration(seconds) return "—" unless seconds seconds = seconds.to_i if seconds < 60 "#{seconds}s" elsif seconds < 3600 "#{seconds / 60}m #{seconds % 60}s" elsif seconds < 86_400 "#{seconds / 3600}h #{(seconds % 3600) / 60}m" else "#{seconds / 86_400}d #{(seconds % 86_400) / 3600}h" end end |
#pgbus_json_preview(json_string, max_length: 120) ⇒ Object
99 100 101 102 103 104 |
# File 'app/helpers/pgbus/application_helper.rb', line 99 def pgbus_json_preview(json_string, max_length: 120) return "—" unless json_string text = json_string.is_a?(String) ? json_string : JSON.generate(json_string) text.length > max_length ? "#{text[0...max_length]}..." : text end |
#pgbus_locale_flag(code) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'app/helpers/pgbus/application_helper.rb', line 190 def pgbus_locale_flag(code) case code.to_sym when :da then "\u{1F1E9}\u{1F1F0}" when :de then "\u{1F1E9}\u{1F1EA}" when :en then "\u{1F1EC}\u{1F1E7}" when :es then "\u{1F1EA}\u{1F1F8}" when :fi then "\u{1F1EB}\u{1F1EE}" when :fr then "\u{1F1EB}\u{1F1F7}" when :it then "\u{1F1EE}\u{1F1F9}" when :ja then "\u{1F1EF}\u{1F1F5}" when :nb then "\u{1F1F3}\u{1F1F4}" when :nl then "\u{1F1F3}\u{1F1F1}" when :pt then "\u{1F1F5}\u{1F1F9}" when :sv then "\u{1F1F8}\u{1F1EA}" else "\u{1F310}" end end |
#pgbus_locale_name(code) ⇒ Object
186 187 188 |
# File 'app/helpers/pgbus/application_helper.rb', line 186 def pgbus_locale_name(code) LOCALE_NAMES[code.to_sym] || code.to_s.upcase end |
#pgbus_mobile_nav_link(label, path) ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'app/helpers/pgbus/application_helper.rb', line 161 def pgbus_mobile_nav_link(label, path) active = request.path == path || (path != pgbus.root_path && request.path.start_with?(path)) css = if active "block rounded-md px-3 py-2 text-base font-medium text-white bg-gray-800" else "block rounded-md px-3 py-2 text-base font-medium text-gray-300 hover:text-white hover:bg-gray-700" end link_to label, path, class: css end |
#pgbus_ms_duration(millis) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/helpers/pgbus/application_helper.rb', line 67 def pgbus_ms_duration(millis) return "—" unless millis millis = millis.to_i if millis < 1000 "#{millis}ms" elsif millis < 60_000 "#{(millis / 1000.0).round(1)}s" else "#{(millis / 60_000.0).round(1)}m" end end |
#pgbus_nav_link(label, path) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'app/helpers/pgbus/application_helper.rb', line 151 def pgbus_nav_link(label, path) active = request.path == path || (path != pgbus.root_path && request.path.start_with?(path)) css = if active "rounded-md px-3 py-2 text-sm font-medium text-white bg-gray-800" else "rounded-md px-3 py-2 text-sm font-medium text-gray-300 hover:text-white hover:bg-gray-700" end link_to label, path, class: css end |
#pgbus_number(n) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'app/helpers/pgbus/application_helper.rb', line 21 def pgbus_number(n) return "0" unless n n = n.to_i case n when 0..999 then n.to_s when 1_000..999_999 then "#{(n / 1_000.0).round(1)}K" else "#{(n / 1_000_000.0).round(1)}M" end end |
#pgbus_parse_message(message) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/helpers/pgbus/application_helper.rb', line 87 def () return {} unless case when Hash then when String then JSON.parse() else {} end rescue JSON::ParserError {} end |
#pgbus_paused_badge(paused) ⇒ Object
80 81 82 83 84 85 |
# File 'app/helpers/pgbus/application_helper.rb', line 80 def pgbus_paused_badge(paused) return unless paused tag.span(I18n.t("pgbus.helpers.paused_badge"), class: "inline-flex items-center rounded-full bg-yellow-100 px-2 py-0.5 text-xs font-medium text-yellow-800") end |
#pgbus_queue_badge(name) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'app/helpers/pgbus/application_helper.rb', line 42 def pgbus_queue_badge(name) if name.to_s.end_with?("_dlq") tag.span(I18n.t("pgbus.helpers.queue_badge.dlq"), class: "inline-flex items-center rounded-full bg-red-100 px-2 py-0.5 text-xs font-medium text-red-700") else tag.span(I18n.t("pgbus.helpers.queue_badge.queue"), class: "inline-flex items-center rounded-full bg-blue-100 px-2 py-0.5 text-xs font-medium text-blue-700") end end |
#pgbus_recurring_health_badge(task) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'app/helpers/pgbus/application_helper.rb', line 129 def pgbus_recurring_health_badge(task) if task[:last_run_at].nil? tag.span(I18n.t("pgbus.helpers.recurring_health.pending"), class: "inline-flex items-center rounded-full bg-yellow-100 px-2.5 py-0.5 text-xs font-medium text-yellow-800") else tag.span(I18n.t("pgbus.helpers.recurring_health.active"), class: "inline-flex items-center rounded-full bg-green-100 px-2.5 py-0.5 text-xs font-medium text-green-800") end end |
#pgbus_refresh_interval ⇒ Object
106 107 108 |
# File 'app/helpers/pgbus/application_helper.rb', line 106 def pgbus_refresh_interval Pgbus.configuration.web_refresh_interval end |
#pgbus_status_badge(healthy) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'app/helpers/pgbus/application_helper.rb', line 32 def pgbus_status_badge(healthy) if healthy tag.span(I18n.t("pgbus.helpers.status_badge.healthy"), class: "inline-flex items-center rounded-full bg-green-100 px-2.5 py-0.5 text-xs font-medium text-green-800") else tag.span(I18n.t("pgbus.helpers.status_badge.stale"), class: "inline-flex items-center rounded-full bg-red-100 px-2.5 py-0.5 text-xs font-medium text-red-800") end end |
#pgbus_time_ago(time) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/helpers/pgbus/application_helper.rb', line 7 def pgbus_time_ago(time) return "—" unless time time = Time.parse(time) if time.is_a?(String) seconds = (Time.now - time).to_i case seconds when 0..59 then "#{seconds}s ago" when 60..3599 then "#{seconds / 60}m ago" when 3600..86_399 then "#{seconds / 3600}h ago" else "#{seconds / 86_400}d ago" end end |
#pgbus_time_ago_future(time) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/helpers/pgbus/application_helper.rb', line 110 def pgbus_time_ago_future(time) return "—" unless time time = Time.parse(time) if time.is_a?(String) seconds = (time - Time.now).to_i if seconds <= 0 "now" elsif seconds < 60 "in #{seconds}s" elsif seconds < 3600 "in #{seconds / 60}m" elsif seconds < 86_400 "in #{seconds / 3600}h" else "in #{seconds / 86_400}d" end end |
#pgbus_time_range_label(minutes) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/helpers/pgbus/application_helper.rb', line 139 def pgbus_time_range_label(minutes) minutes = [minutes.to_i, 1].max if minutes > 1440 && (minutes % 1440).zero? pgbus_pluralize_unit(minutes / 1440, "day") elsif minutes >= 60 && (minutes % 60).zero? pgbus_pluralize_unit(minutes / 60, "hour") else pgbus_pluralize_unit(minutes, "minute") end end |