Skip to content

A platform for community discussion. Free, open, simple.

DEV: add totals to AI usage (#36544)

Keegan Georgec39380181e11
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs
index 3d19812c935..61694a58294 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs
@@ -275,6 +275,61 @@ export default class AiUsage extends Component {
return (this.data?.models || []).filter((m) => !m.credit_allocation);
}
+ get featuresTotals() {
+ const features = this.data?.features || [];
+ return {
+ usage_count: features.reduce((sum, f) => sum + (f.usage_count || 0), 0),
+ total_tokens: features.reduce((sum, f) => sum + (f.total_tokens || 0), 0),
+ total_spending: features.reduce(
+ (sum, f) =>
+ sum +
+ (f.input_spending || 0) +
+ (f.cache_read_spending || 0) +
+ (f.cache_write_spending || 0) +
+ (f.output_spending || 0),
+ 0
+ ),
+ };
+ }
+
+ get modelsWithoutCreditsTotals() {
+ const models = this.modelsWithoutCredits || [];
+ return {
+ usage_count: models.reduce((sum, m) => sum + (m.usage_count || 0), 0),
+ total_tokens: models.reduce((sum, m) => sum + (m.total_tokens || 0), 0),
+ total_spending: models.reduce(
+ (sum, m) =>
+ sum +
+ (m.input_spending || 0) +
+ (m.cache_read_spending || 0) +
+ (m.cache_write_spending || 0) +
+ (m.output_spending || 0),
+ 0
+ ),
+ };
+ }
+
+ get usersTotals() {
+ const users = this.data?.users || [];
+ return {
+ usage_count: users.reduce((sum, u) => sum + (u.usage_count || 0), 0),
+ total_tokens: users.reduce((sum, u) => sum + (u.total_tokens || 0), 0),
+ total_spending: users.reduce(
+ (sum, u) =>
+ sum +
+ (u.input_spending || 0) +
+ (u.cache_read_spending || 0) +
+ (u.cache_write_spending || 0) +
+ (u.output_spending || 0),
+ 0
+ ),
+ };
+ }
+
+ formatSpending(value) {
+ return `$${(value || 0).toFixed(2)}`;
+ }
+
get availableFeatures() {
// when you switch we don't want the list to change
// only when you switch durations
@@ -534,6 +589,18 @@ export default class AiUsage extends Component {
</td>
</tr>
{{/each}}
+ <tr class="ai-usage__total-row">
+ <td>{{i18n "discourse_ai.usage.total"}}</td>
+ <td title={{this.featuresTotals.usage_count}}>{{number
+ this.featuresTotals.usage_count
+ }}</td>
+ <td title={{this.featuresTotals.total_tokens}}>{{number
+ this.featuresTotals.total_tokens
+ }}</td>
+ <td>{{this.formatSpending
+ this.featuresTotals.total_spending
+ }}</td>
+ </tr>
</tbody>
</table>
{{/if}}
@@ -617,6 +684,22 @@ export default class AiUsage extends Component {
</td>
</tr>
{{/each}}
+ <tr class="ai-usage__total-row">
+ <td>{{i18n "discourse_ai.usage.total"}}</td>
+ <td
+ title={{this.modelsWithoutCreditsTotals.usage_count}}
+ >{{number
+ this.modelsWithoutCreditsTotals.usage_count
+ }}</td>
+ <td
+ title={{this.modelsWithoutCreditsTotals.total_tokens}}
+ >{{number
+ this.modelsWithoutCreditsTotals.total_tokens
+ }}</td>
+ <td>{{this.formatSpending
+ this.modelsWithoutCreditsTotals.total_spending
+ }}</td>
+ </tr>
</tbody>
</table>
{{/if}}
@@ -687,6 +770,20 @@ export default class AiUsage extends Component {
</td>
</tr>
{{/each}}
+ {{#unless (gt this.data.users.length 24)}}
+ <tr class="ai-usage__total-row">
+ <td>{{i18n "discourse_ai.usage.total"}}</td>
+ <td title={{this.usersTotals.usage_count}}>{{number
+ this.usersTotals.usage_count
+ }}</td>
+ <td title={{this.usersTotals.total_tokens}}>{{number
+ this.usersTotals.total_tokens
+ }}</td>
+ <td>{{this.formatSpending
+ this.usersTotals.total_spending
+ }}</td>
+ </tr>
+ {{/unless}}
</tbody>
</table>
@@ -739,6 +836,18 @@ export default class AiUsage extends Component {
</td>
</tr>
{{/each}}
+ <tr class="ai-usage__total-row">
+ <td>{{i18n "discourse_ai.usage.total"}}</td>
+ <td title={{this.usersTotals.usage_count}}>{{number
+ this.usersTotals.usage_count
+ }}</td>
+ <td title={{this.usersTotals.total_tokens}}>{{number
+ this.usersTotals.total_tokens
+ }}</td>
+ <td>{{this.formatSpending
+ this.usersTotals.total_spending
+ }}</td>
+ </tr>
</tbody>
</table>
{{/if}}
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/llms/common/usage.scss b/plugins/discourse-ai/assets/stylesheets/modules/llms/common/usage.scss
index dd95d6cdf8b..dd24eb53554 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/llms/common/usage.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/llms/common/usage.scss
@@ -176,6 +176,20 @@
width: 100%;
}
+ &__total-row {
+ background: var(--primary-very-low);
+
+ td {
+ font-weight: 700;
+ padding: var(--space-2);
+ border-bottom: 1px solid var(--primary-low);
+ }
+
+ &:hover {
+ background: var(--primary-very-low);
+ }
+ }
+
&__models-credit-bar {
padding: var(--space-2);
border-bottom: 1px solid var(--primary-low);
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index b7665fef318..fe0ea58a1d7 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -443,6 +443,7 @@ en:
no_users: "No user usage data found"
no_models: "No model usage data found"
no_features: "No feature usage data found"
+ total: "Total"
subheader_description: "Tokens are the basic units that LLMs use to understand and generate text, usage data may affect costs"
stat_tooltips:
total_requests: "All requests made to LLMs through Discourse"
diff --git a/plugins/discourse-ai/spec/system/ai_usage_spec.rb b/plugins/discourse-ai/spec/system/ai_usage_spec.rb
index ae53f40a072..4a8fffee1b1 100644
--- a/plugins/discourse-ai/spec/system/ai_usage_spec.rb
+++ b/plugins/discourse-ai/spec/system/ai_usage_spec.rb
@@ -2,12 +2,48 @@
RSpec.describe "AI Usage Admin Page", type: :system do
fab!(:admin)
+ fab!(:llm_model)
before do
enable_current_plugin
sign_in(admin)
end
+ context "when viewing usage data with total rows" do
+ before do
+ AiApiRequestStat.create!(
+ provider_id: 1,
+ feature_name: "summarize",
+ language_model: llm_model.name,
+ llm_id: llm_model.id,
+ request_tokens: 100,
+ response_tokens: 50,
+ created_at: 1.day.ago,
+ )
+
+ AiApiRequestStat.create!(
+ provider_id: 1,
+ feature_name: "translate",
+ language_model: llm_model.name,
+ llm_id: llm_model.id,
+ request_tokens: 200,
+ response_tokens: 100,
+ created_at: 2.days.ago,
+ )
+ end
+
+ it "displays total rows in the features and models tables" do
+ visit "/admin/plugins/discourse-ai/ai-usage"
+
+ expect(page).to have_css(".ai-usage__features-table .ai-usage__total-row")
+ expect(page).to have_css(".ai-usage__models-table .ai-usage__total-row")
+
+ within ".ai-usage__features-table .ai-usage__total-row" do
+ expect(page).to have_content(I18n.t("js.discourse_ai.usage.total"))
+ end
+ end
+ end
+
context "when using custom date range functionality" do
it "allows selecting custom date range without JavaScript errors" do
visit "/admin/plugins/discourse-ai/ai-usage"
diff --git a/plugins/discourse-ai/spec/system/page_objects/pages/ai_usage.rb b/plugins/discourse-ai/spec/system/page_objects/pages/ai_usage.rb
index c7f3f6b110e..1fd84d204f7 100644
--- a/plugins/discourse-ai/spec/system/page_objects/pages/ai_usage.rb
+++ b/plugins/discourse-ai/spec/system/page_objects/pages/ai_usage.rb
@@ -55,6 +55,18 @@ module PageObjects
def has_summary_data?
page.has_css?(".ai-usage__summary .d-stat-tiles")
end
+
+ def has_features_total_row?
+ page.has_css?(".ai-usage__features-table .ai-usage__total-row")
+ end
+
+ def has_models_total_row?
+ page.has_css?(".ai-usage__models-table .ai-usage__total-row")
+ end
+
+ def has_users_total_row?
+ page.has_css?(".ai-usage__users-table .ai-usage__total-row")
+ end
end
end
end