Module: LlmCostTracker::ApplicationHelper

Includes:
ChartHelper, DashboardFilterHelper, DashboardFilterOptionsHelper, DashboardQueryHelper, PaginationHelper
Defined in:
app/helpers/llm_cost_tracker/application_helper.rb

Constant Summary collapse

TAG_VALUE_SUMMARY_BYTES =
80
TAG_TOOLTIP_BYTES =
512

Constants included from PaginationHelper

PaginationHelper::PER_PAGE_CHOICES

Constants included from DashboardFilterOptionsHelper

DashboardFilterOptionsHelper::MAX_FILTER_OPTIONS

Constants included from DashboardFilterHelper

DashboardFilterHelper::FILTER_PARAM_KEYS, DashboardFilterHelper::STREAM_FILTER_OPTIONS

Instance Method Summary collapse

Methods included from PaginationHelper

#pagination_page_items

Methods included from ChartHelper

#spend_chart_svg

Methods included from DashboardQueryHelper

#calls_query_for_tag, #dashboard_filter_path

Methods included from DashboardFilterOptionsHelper

#model_filter_options, #provider_filter_options

Methods included from DashboardFilterHelper

#active_tag_filters, #any_filter_applied?, #dashboard_date_range_label

Instance Method Details

#bar_width(value, max) ⇒ Object



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

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

#budget_fill_modifier(percent) ⇒ Object



126
127
128
129
130
131
132
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 126

def budget_fill_modifier(percent)
  percent = percent.to_f
  return "lct-budget-fill--over" if percent >= 100.0
  return "lct-budget-fill--warn" if percent >= 80.0

  ""
end

#calls_query_for_model(provider:, model:) ⇒ Object



138
139
140
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 138

def calls_query_for_model(provider:, model:)
  current_query(provider: provider, model: model, page: nil, per: nil, format: nil)
end

#coverage_percent(numerator, denominator) ⇒ Object



16
17
18
19
20
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 16

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

  (numerator.to_f / denominator) * 100.0
end

#current_query(overrides = {}) ⇒ Object



134
135
136
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 134

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

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 57

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

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

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

  { text: text, css_class: css_class }
end

#format_date(value) ⇒ Object



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

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

#format_tokens(value) ⇒ Object



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

def format_tokens(value)
  number(value)
end

#money(value) ⇒ Object



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

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



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

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

#optional_money(value) ⇒ Object



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

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

#optional_number(value) ⇒ Object



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

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

#percent(value) ⇒ Object



53
54
55
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 53

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

#pricing_status(call) ⇒ Object



49
50
51
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 49

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

#safe_json(value) ⇒ Object



95
96
97
98
99
100
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 95

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

#stack_segments(entries) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 83

def stack_segments(entries)
  total = entries.sum { |entry| entry[:value].to_f }
  return [] unless total.positive?

  entries.filter_map do |entry|
    value = entry[:value].to_f
    next unless value.positive?

    entry.merge(percent: (value / total) * 100.0)
  end
end

#tag_chip_entries(tags, limit: 3) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 111

def tag_chip_entries(tags, limit: 3)
  normalized = normalized_tags(tags)
  return [] if normalized.empty?

  visible = normalized.first(limit).map do |key, value|
    { key: key.to_s, value: tag_value_summary(value) }
  end
  visible << { more: normalized.size - limit } if normalized.size > limit
  visible
end

#tag_chips_title(tags) ⇒ Object



122
123
124
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 122

def tag_chips_title(tags)
  truncate_text(safe_json(tags), TAG_TOOLTIP_BYTES)
end

#tags_summary(tags, limit: 3) ⇒ Object



102
103
104
105
106
107
108
109
# File 'app/helpers/llm_cost_tracker/application_helper.rb', line 102

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