mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-07-10 08:03:28 +00:00
- Added a new admin interface to track AI usage metrics, including tokens, features, and models. - Introduced a new route `/admin/plugins/discourse-ai/ai-usage` and supporting API endpoint in `AiUsageController`. - Implemented `AiUsageSerializer` for structuring AI usage data. - Integrated CSS stylings for charts and tables under `stylesheets/modules/llms/common/usage.scss`. - Enhanced backend with `AiApiAuditLog` model changes: added `cached_tokens` column (implemented with OpenAI for now) with relevant DB migration and indexing. - Created `Report` module for efficient aggregation and filtering of AI usage metrics. - Updated AI Bot title generation logic to log correctly to user vs bot - Extended test coverage for the new tracking features, ensuring data consistency and access controls.
28 lines
722 B
Ruby
28 lines
722 B
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseAi
|
|
module Admin
|
|
class AiUsageController < ::Admin::AdminController
|
|
requires_plugin "discourse-ai"
|
|
|
|
def show
|
|
render json: AiUsageSerializer.new(create_report, root: false)
|
|
end
|
|
|
|
private
|
|
|
|
def create_report
|
|
report =
|
|
DiscourseAi::Completions::Report.new(
|
|
start_date: params[:start_date]&.to_date || 30.days.ago,
|
|
end_date: params[:end_date]&.to_date || Time.current,
|
|
)
|
|
|
|
report = report.filter_by_feature(params[:feature]) if params[:feature].present?
|
|
report = report.filter_by_model(params[:model]) if params[:model].present?
|
|
report
|
|
end
|
|
end
|
|
end
|
|
end
|