Class: LlmCostTracker::CallsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/llm_cost_tracker/calls_controller.rb

Constant Summary collapse

CSV_EXPORT_LIMIT =
10_000
CSV_EXPORT_BATCH_SIZE =
500
CSV_FORMULA_PREFIXES =
["=", "+", "-", "@", "\t", "\r"].freeze
DEFAULT_TIEBREAKER =
{ tracked_at: :desc, id: :desc }.freeze
SORT_OPTIONS =
%w[tracked_at provider model input output cost latency].freeze
NULLS_LAST_GUARD =
{
  total_cost: Arel.sql("CASE WHEN total_cost IS NULL THEN 1 ELSE 0 END ASC"),
  latency_ms: Arel.sql("CASE WHEN latency_ms IS NULL THEN 1 ELSE 0 END ASC")
}.freeze

Instance Method Summary collapse

Instance Method Details

#indexObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/llm_cost_tracker/calls_controller.rb', line 18

def index
  @sort = params[:sort].to_s
  @dir = params[:dir].to_s
  scope = Dashboard::Filter.call(params: params)
  scope = scope.unknown_pricing if params[:cost_status].to_s == "incomplete"
  ordered_scope = scope.order(*calls_order(@sort, @dir))

  respond_to do |format|
    format.html do
      @page = Dashboard::Pagination.call(params)
      @calls_count, @calls_total_cost = scope.pick(Arel.sql("COUNT(*), COALESCE(SUM(total_cost), 0)"))
      @calls = ordered_scope.includes(:tag_records).limit(@page.limit).offset(@page.offset).to_a
    end
    format.csv do
      response.headers["Cache-Control"] = "no-store"
      send_data render_csv(ordered_scope),
                type: "text/csv",
                disposition: %(attachment; filename="llm_calls_#{Time.now.utc.strftime('%Y%m%d_%H%M%S')}.csv")
    end
  end
end

#showObject



40
41
42
# File 'app/controllers/llm_cost_tracker/calls_controller.rb', line 40

def show
  @call = LlmCostTracker::Call.includes(:line_items, :tag_records).find(params[:id])
end