Class: ActionAgent::Api::MetricsController

Inherits:
BaseController show all
Defined in:
app/controllers/action_agent/api/metrics_controller.rb

Overview

Read API for telemetry metrics, backing the dashboard Metrics view.

Exposes the same aggregates as the gem dashboard's metrics page (ActionAgent::TracesController#metrics / #calculate_metrics / #agent_statistics): trace counts, token totals, average duration, error rate, active agents and per-agent statistics — account-scoped, plus previous-period deltas for trend indicators.

Constant Summary collapse

DEFAULT_WINDOW_HOURS =
24
MAX_WINDOW_HOURS =
24 * 30
AGENT_SORTS =

How to rank the per-agent table. Cost is applied after the grouped query because pricing happens in Ruby (rates vary per model), so all four are ordered in one place rather than half in SQL.

{
  "popular" => "Most requests",
  "longest" => "Longest average",
  "cost" => "Highest cost",
  "tokens" => "Most tokens",
  "errors" => "Most errors"
}.freeze
DEFAULT_AGENT_SORT =
"popular"

Instance Method Summary collapse

Methods inherited from ActionAgent::ApplicationController

allow_unauthenticated_access

Instance Method Details

#showObject

GET /api/metrics



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/action_agent/api/metrics_controller.rb', line 31

def show
  hours = params.fetch(:hours, DEFAULT_WINDOW_HOURS).to_i.clamp(1, MAX_WINDOW_HOURS)
  now = Time.current

  current = traces_scope.for_date_range(hours.hours.ago(now), now)
  previous = traces_scope.for_date_range((hours * 2).hours.ago(now), hours.hours.ago(now))

  costs = cost_statistics(current)
  priced = agent_statistics(current).map { |row| row.merge(cost: costs[:by_agent][row[:name]] || 0.0) }

  render json: {
    summary: summary_for(current, previous).merge(total_cost: costs[:total]),
    hourly_requests: hourly_requests(current, hours, now),
    by_agent: sort_agents(priced, params[:sort]),
    window_hours: hours,
    sorts: AGENT_SORTS,
    sort: agent_sort(params[:sort])
  }
end