Class: RubynCode::Observability::UsageReporter
- Inherits:
-
Object
- Object
- RubynCode::Observability::UsageReporter
- Defined in:
- lib/rubyn_code/observability/usage_reporter.rb
Overview
Generates human-readable cost and usage summaries from recorded cost data.
Constant Summary collapse
- TABLE_NAME =
BudgetEnforcer::TABLE_NAME
Instance Method Summary collapse
-
#daily_summary ⇒ String
Returns a formatted summary of today’s total cost across all sessions.
-
#initialize(db, formatter:) ⇒ UsageReporter
constructor
A new instance of UsageReporter.
-
#model_breakdown(session_id) ⇒ String
Returns a cost breakdown by model for a given session.
-
#session_summary(session_id) ⇒ String
Returns a formatted summary of cost and usage for a given session.
Constructor Details
#initialize(db, formatter:) ⇒ UsageReporter
Returns a new instance of UsageReporter.
13 14 15 16 |
# File 'lib/rubyn_code/observability/usage_reporter.rb', line 13 def initialize(db, formatter:) @db = db @formatter = formatter end |
Instance Method Details
#daily_summary ⇒ String
Returns a formatted summary of today’s total cost across all sessions.
37 38 39 40 41 42 43 44 |
# File 'lib/rubyn_code/observability/usage_reporter.rb', line 37 def daily_summary today = Time.now.utc.strftime('%Y-%m-%d') rows = query_daily_rows(today) return 'No usage data for today.' if rows.empty? build_daily_summary_lines(today, rows).join("\n") end |
#model_breakdown(session_id) ⇒ String
Returns a cost breakdown by model for a given session.
50 51 52 53 54 55 56 57 58 |
# File 'lib/rubyn_code/observability/usage_reporter.rb', line 50 def model_breakdown(session_id) rows = query_model_breakdown_rows(session_id) return "No usage data for session #{session_id}." if rows.empty? lines = [header('Cost by Model')] rows.each { |row| append_model_row(lines, row) } lines.join("\n") end |
#session_summary(session_id) ⇒ String
Returns a formatted summary of cost and usage for a given session.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rubyn_code/observability/usage_reporter.rb', line 22 def session_summary(session_id) rows = @db.query( "SELECT model, input_tokens, output_tokens, cost_usd FROM #{TABLE_NAME} WHERE session_id = ?", [session_id] ).to_a return "No usage data for session #{session_id}." if rows.empty? totals = compute_session_totals(rows) build_session_summary_lines(session_id, rows.size, totals).join("\n") end |