Module: Observ::DashboardHelper
- Included in:
- ApplicationHelper
- Defined in:
- app/helpers/observ/dashboard_helper.rb
Instance Method Summary collapse
- #format_currency(amount) ⇒ Object
- #format_duration_ms(ms) ⇒ Object
- #format_duration_s(seconds) ⇒ Object
- #format_json_with_newlines(data) ⇒ Object
- #format_number(number) ⇒ Object
- #format_tokens(count) ⇒ Object
- #observ_json_preview(json_data, max_length = 100) ⇒ Object
- #observ_model_badge(model) ⇒ Object
- #observ_percentage(value) ⇒ Object
- #observ_relative_time(time) ⇒ Object
- #observ_session_status(session) ⇒ Object
- #observ_status_badge(status) ⇒ Object
- #observ_timestamp(time) ⇒ Object
- #observ_trace_status(trace) ⇒ Object
- #observ_trend_badge(percentage) ⇒ Object
- #render_input_output(content, compact: false) ⇒ Object
- #render_json_viewer(data, compact: false) ⇒ Object
- #truncate_id(id, length = 8) ⇒ Object
Instance Method Details
#format_currency(amount) ⇒ Object
3 4 5 6 |
# File 'app/helpers/observ/dashboard_helper.rb', line 3 def format_currency(amount) return "$0.00" if amount.nil? || amount.zero? "$#{sprintf('%.4f', amount)}" end |
#format_duration_ms(ms) ⇒ Object
8 9 10 11 12 |
# File 'app/helpers/observ/dashboard_helper.rb', line 8 def format_duration_ms(ms) return "0ms" if ms.nil? || ms.zero? return "#{ms.round(0)}ms" if ms < 1000 "#{(ms / 1000.0).round(1)}s" end |
#format_duration_s(seconds) ⇒ Object
14 15 16 17 18 19 20 |
# File 'app/helpers/observ/dashboard_helper.rb', line 14 def format_duration_s(seconds) return "0s" if seconds.nil? || seconds.zero? return "#{seconds.round(1)}s" if seconds < 60 minutes = (seconds / 60).floor remaining_seconds = (seconds % 60).round(0) "#{minutes}m #{remaining_seconds}s" end |
#format_json_with_newlines(data) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/helpers/observ/dashboard_helper.rb', line 100 def format_json_with_newlines(data) return "" if data.nil? # Convert to JSON with pretty formatting json_string = JSON.pretty_generate(data) # This regex finds string values in JSON and unescapes newlines within them # It preserves the JSON structure while making newlines visible json_string.gsub(/: "((?:[^"\\]|\\.)*)"/m) do |match| content = $1 # Unescape the newlines in the string content unescaped = content.gsub('\\n', "\n") .gsub('\\t', "\t") .gsub('\\r', "\r") ': "' + unescaped + '"' end end |
#format_number(number) ⇒ Object
29 30 31 32 |
# File 'app/helpers/observ/dashboard_helper.rb', line 29 def format_number(number) return "0" if number.nil? || number.zero? number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse end |
#format_tokens(count) ⇒ Object
22 23 24 25 26 27 |
# File 'app/helpers/observ/dashboard_helper.rb', line 22 def format_tokens(count) return "0" if count.nil? || count.zero? return count.to_s if count < 1000 return "#{(count / 1000.0).round(1)}K" if count < 1_000_000 "#{(count / 1_000_000.0).round(1)}M" end |
#observ_json_preview(json_data, max_length = 100) ⇒ Object
93 94 95 96 97 98 |
# File 'app/helpers/observ/dashboard_helper.rb', line 93 def observ_json_preview(json_data, max_length = 100) return "" if json_data.nil? text = json_data.is_a?(String) ? json_data : json_data.to_json truncate(text, length: max_length) end |
#observ_model_badge(model) ⇒ Object
87 88 89 90 91 |
# File 'app/helpers/observ/dashboard_helper.rb', line 87 def observ_model_badge(model) return content_tag(:span, "Unknown", class: "observ-model-badge") if model.blank? content_tag(:span, model, class: "observ-model-badge") end |
#observ_percentage(value) ⇒ Object
65 66 67 |
# File 'app/helpers/observ/dashboard_helper.rb', line 65 def observ_percentage(value) "#{value.round(1)}%" end |
#observ_relative_time(time) ⇒ Object
74 75 76 77 |
# File 'app/helpers/observ/dashboard_helper.rb', line 74 def observ_relative_time(time) return "N/A" if time.nil? distance_of_time_in_words_to_now(time) + " ago" end |
#observ_session_status(session) ⇒ Object
79 80 81 |
# File 'app/helpers/observ/dashboard_helper.rb', line 79 def observ_session_status(session) session.end_time.present? ? "completed" : "active" end |
#observ_status_badge(status) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/helpers/observ/dashboard_helper.rb', line 34 def observ_status_badge(status) css_class = case status&.downcase when "completed", "success" "observ-badge--success" when "active", "running", "in_progress" "observ-badge--info" when "failed", "error" "observ-badge--danger" else "observ-badge--warning" end content_tag(:span, status&.titleize || "Unknown", class: "observ-badge #{css_class}") end |
#observ_timestamp(time) ⇒ Object
69 70 71 72 |
# File 'app/helpers/observ/dashboard_helper.rb', line 69 def (time) return "N/A" if time.nil? time.strftime("%Y-%m-%d %H:%M:%S") end |
#observ_trace_status(trace) ⇒ Object
83 84 85 |
# File 'app/helpers/observ/dashboard_helper.rb', line 83 def observ_trace_status(trace) trace.end_time.present? ? "completed" : "active" end |
#observ_trend_badge(percentage) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'app/helpers/observ/dashboard_helper.rb', line 49 def observ_trend_badge(percentage) return content_tag(:span, "0%", class: "observ-trend observ-trend--neutral") if percentage.zero? css_class = percentage.positive? ? "observ-trend--positive" : "observ-trend--negative" icon = percentage.positive? ? "↑" : "↓" content_tag(:span, class: "observ-trend #{css_class}") do "#{icon} #{percentage.abs}%" end end |
#render_input_output(content, compact: false) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'app/helpers/observ/dashboard_helper.rb', line 134 def render_input_output(content, compact: false) return "" if content.nil? # Try to parse as JSON begin parsed_data = JSON.parse(content) # If it's a Hash or Array, use the JSON viewer if parsed_data.is_a?(Hash) || parsed_data.is_a?(Array) return render_json_viewer(parsed_data, compact: compact) end rescue JSON::ParserError # Not valid JSON, fall through to plain text display end # Display as plain text css_classes = ["observ-code-block"] css_classes << "observ-code-block--compact" if compact content_tag(:pre, content, class: css_classes.join(" ")) end |
#render_json_viewer(data, compact: false) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/helpers/observ/dashboard_helper.rb', line 118 def render_json_viewer(data, compact: false) return "" if data.nil? css_classes = ["observ-json-viewer"] css_classes << "observ-json-viewer--compact" if compact content_tag(:div, "", class: css_classes.join(" "), data: { controller: "observ--json-viewer", observ__json_viewer_data_value: data.to_json } ) end |
#truncate_id(id, length = 8) ⇒ Object
60 61 62 63 |
# File 'app/helpers/observ/dashboard_helper.rb', line 60 def truncate_id(id, length = 8) return "" if id.nil? id.to_s[0...length] end |