Module: LlmCostTracker::ApplicationHelper

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

Instance Method Summary collapse

Instance Method Details

#bar_width(value, max) ⇒ Object



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

def bar_width(value, max)
  max = max.to_f
  return "0%" unless max.positive?

  "#{[(value.to_f / max) * 100.0, 100.0].min.round(2)}%"
end

#coverage_percent(numerator, denominator) ⇒ Object



7
8
9
10
11
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 7

def coverage_percent(numerator, denominator)
  return 0.0 unless denominator.to_i.positive?

  (numerator.to_f / denominator) * 100.0
end

#current_query(overrides = {}) ⇒ Object



90
91
92
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 90

def current_query(overrides = {})
  request.query_parameters.symbolize_keys.merge(overrides)
end

#delta_badge(delta_percent, mode: :cost) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 48

def delta_badge(delta_percent, mode: :cost)
  return { text: "vs. prior: n/a", css_class: "lct-delta lct-delta-neutral" } if delta_percent.nil?

  rounded = delta_percent.round(1)
  return { text: "= vs. prior", css_class: "lct-delta lct-delta-neutral" } if rounded.zero?

  sign = rounded.positive? ? "+" : ""
  text = "#{sign}#{format('%.1f', rounded)}% vs. prior"
  css_class = if mode == :neutral
                "lct-delta lct-delta-neutral"
              elsif rounded.positive?
                "lct-delta lct-delta-up"
              else
                "lct-delta lct-delta-down"
              end

  { text: text, css_class: css_class }
end

#format_date(value) ⇒ Object



36
37
38
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 36

def format_date(value)
  value.respond_to?(:strftime) ? value.strftime("%Y-%m-%d %H:%M") : value.to_s
end

#format_tokens(value) ⇒ Object



32
33
34
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 32

def format_tokens(value)
  number(value)
end

#money(value) ⇒ Object



13
14
15
16
17
18
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 13

def money(value)
  value = value.to_f
  precision = value.abs < 0.01 && value != 0.0 ? 6 : 2

  "$#{format("%.#{precision}f", value)}"
end

#number(value) ⇒ Object



28
29
30
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 28

def number(value)
  number_with_delimiter(value.to_i)
end

#optional_money(value) ⇒ Object



20
21
22
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 20

def optional_money(value)
  value.nil? ? "n/a" : money(value)
end

#optional_number(value) ⇒ Object



24
25
26
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 24

def optional_number(value)
  value.nil? ? "n/a" : number(value)
end

#percent(value) ⇒ Object



44
45
46
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 44

def percent(value)
  "#{format('%.1f', value.to_f)}%"
end

#pricing_status(call) ⇒ Object



40
41
42
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 40

def pricing_status(call)
  call.total_cost.nil? ? "Unknown pricing" : "Estimated"
end

#safe_json(value) ⇒ Object



74
75
76
77
78
79
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 74

def safe_json(value)
  parsed = value.is_a?(String) ? JSON.parse(value) : value
  JSON.pretty_generate(parsed || {})
rescue JSON::ParserError, TypeError
  value.to_s
end

#tags_summary(tags, limit: 3) ⇒ Object



81
82
83
84
85
86
87
88
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 81

def tags_summary(tags, limit: 3)
  tags = normalized_tags(tags)
  return "(untagged)" if tags.empty?

  summary = tags.first(limit).map { |key, value| "#{key}=#{tag_value_summary(value)}" }
  summary << "+#{tags.size - limit}" if tags.size > limit
  summary.join(", ")
end