Skip to content

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

FIX: Update model/feature dropdowns when changing time period in AI usage (#36975)

Régis Hanol03e0e08b3588
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 62cdd957797..56a058b7959 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs
@@ -40,11 +40,11 @@ export default class AiUsage extends Component {
constructor() {
super(...arguments);
- this.fetchData({ clearCache: true });
+ this.fetchData();
}
@action
- async fetchData({ clearCache = false } = {}) {
+ async fetchData() {
const response = await ajax(
"/admin/plugins/discourse-ai/ai-usage-report.json",
{
@@ -58,13 +58,9 @@ export default class AiUsage extends Component {
},
}
);
+
this.data = response;
this.loadingData = false;
-
- if (clearCache) {
- this._cachedFeatures = null;
- this._cachedModels = null;
- }
}
@action
@@ -403,25 +399,35 @@ export default class AiUsage extends Component {
}
get availableFeatures() {
- this._cachedFeatures =
- this._cachedFeatures ||
- (this.data?.features || []).map((f) => ({
+ if (this._cachedFeatures) {
+ return this._cachedFeatures;
+ }
+
+ const features = this.data?.features;
+ if (features?.length) {
+ this._cachedFeatures = features.map((f) => ({
id: f.feature_name,
name: f.feature_name,
}));
+ }
- return this._cachedFeatures;
+ return this._cachedFeatures || [];
}
get availableModels() {
- this._cachedModels =
- this._cachedModels ||
- (this.data?.models || []).map((m) => ({
+ if (this._cachedModels) {
+ return this._cachedModels;
+ }
+
+ const models = this.data?.models;
+ if (models?.length) {
+ this._cachedModels = models.map((m) => ({
id: m.id,
name: m.llm,
}));
+ }
- return this._cachedModels;
+ return this._cachedModels || [];
}
get periodOptions() {
@@ -454,10 +460,12 @@ export default class AiUsage extends Component {
@action
onPeriodSelect(period) {
+ this._cachedFeatures = null;
+ this._cachedModels = null;
this.selectedPeriod = period;
this.isCustomDateActive = false;
this.setPeriodDates(period);
- this.fetchData({ clearCache: true });
+ this.fetchData();
}
@action
@@ -470,9 +478,11 @@ export default class AiUsage extends Component {
@action
onDateChange() {
+ this._cachedFeatures = null;
+ this._cachedModels = null;
this.isCustomDateActive = true;
this.selectedPeriod = null;
- this.fetchData({ clearCache: true });
+ this.fetchData();
}
@action
@@ -483,9 +493,11 @@ export default class AiUsage extends Component {
@action
onRefreshDateRange() {
+ this._cachedFeatures = null;
+ this._cachedModels = null;
this.startDate = this._startDate;
this.endDate = this._endDate;
- this.fetchData({ clearCache: true });
+ this.fetchData();
this._startDate = null;
this._endDate = null;
}
diff --git a/plugins/discourse-ai/spec/system/ai_usage_spec.rb b/plugins/discourse-ai/spec/system/ai_usage_spec.rb
index 540f506cc71..5907d959d3b 100644
--- a/plugins/discourse-ai/spec/system/ai_usage_spec.rb
+++ b/plugins/discourse-ai/spec/system/ai_usage_spec.rb
@@ -6,111 +6,84 @@ RSpec.describe "AI Usage Admin Page", type: :system do
let(:ai_usage_page) { PageObjects::Pages::AiUsage.new }
+ def create_usage(model, feature: "summarize", created_at: 1.day.ago)
+ AiApiRequestStat.create!(
+ provider_id: 1,
+ feature_name: feature,
+ language_model: model.name,
+ llm_id: model.id,
+ request_tokens: 100,
+ response_tokens: 50,
+ created_at:,
+ )
+ end
+
before do
enable_current_plugin
sign_in(admin)
end
- context "when viewing usage data with total rows" do
+ context "when viewing usage data" 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,
- )
+ create_usage(llm_model, feature: "summarize")
+ create_usage(llm_model, feature: "translate", created_at: 2.days.ago)
end
- it "displays total rows in the features and models tables" do
- visit "/admin/plugins/discourse-ai/ai-usage"
+ it "displays total rows in tables" do
+ ai_usage_page.visit
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"
- expect(page).to have_css(".ai-usage")
-
- # Click custom date button to show date picker
- find(".ai-usage__period-buttons .btn-default:last-child").click
- expect(page).to have_css(".ai-usage__custom-date-pickers")
+ context "when filtering by model" do
+ fab!(:other_model) { Fabricate(:llm_model, display_name: "Other Model", name: "other-model") }
- # Set dates
- date_inputs = all(".ai-usage__custom-date-pickers input[type='date']")
- date_inputs[0].set("2025-07-01")
- date_inputs[1].set("2025-07-31")
+ before do
+ create_usage(llm_model)
+ create_usage(other_model, feature: "translate")
+ end
- # Verify dates are set correctly (preview functionality)
- expect(date_inputs[0].value).to eq("2025-07-01")
- expect(date_inputs[1].value).to eq("2025-07-31")
+ it "keeps all models in dropdown after selecting a filter" do
+ ai_usage_page.visit
- # Click refresh - this used to cause visual glitches and date reversion
- find(".ai-usage__custom-date-pickers .btn", text: I18n.t("js.refresh")).click
+ model_selector = ai_usage_page.model_selector
+ model_selector.expand
+ model_selector.select_row_by_name(llm_model.display_name)
- # Wait for any potential async operations
- sleep(1)
+ expect(model_selector).to have_selected_name(llm_model.display_name)
- expect(page).to have_css(".ai-usage__summary")
+ model_selector.expand
+ expect(model_selector).to have_option_name(other_model.display_name)
end
end
- context "when filtering by model" do
- fab!(:other_model) { Fabricate(:llm_model, display_name: "Other Model", name: "other-model") }
+ context "when changing time period" do
+ fab!(:recent_model) do
+ Fabricate(:llm_model, display_name: "Recent Model", name: "recent-model")
+ end
+ fab!(:old_model) { Fabricate(:llm_model, display_name: "Old Model", name: "old-model") }
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: other_model.name,
- llm_id: other_model.id,
- request_tokens: 200,
- response_tokens: 100,
- created_at: 1.day.ago,
- )
+ create_usage(recent_model, created_at: 6.hours.ago)
+ create_usage(old_model, feature: "translate", created_at: 5.days.ago)
end
- it "keeps all models in dropdown after selecting a filter" do
+ it "updates model dropdown to reflect the selected period" do
ai_usage_page.visit
model_selector = ai_usage_page.model_selector
model_selector.expand
- model_selector.select_row_by_name(llm_model.display_name)
+ expect(model_selector).to have_option_name(recent_model.display_name)
+ expect(model_selector).to have_option_name(old_model.display_name)
+ model_selector.collapse
- expect(model_selector).to have_selected_name(llm_model.display_name)
+ ai_usage_page.select_period(:day)
- # Verify other models are still available in the dropdown
model_selector.expand
- expect(model_selector).to have_option_name(other_model.display_name)
+ expect(model_selector).to have_option_name(recent_model.display_name)
+ expect(model_selector).to have_no_option_name(old_model.display_name)
end
end
end
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 60f7ea7349d..e5c14e4b154 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
@@ -75,6 +75,22 @@ module PageObjects
def feature_selector
PageObjects::Components::SelectKit.new(".ai-usage__feature-selector")
end
+
+ def select_period(period)
+ label =
+ case period
+ when :day
+ I18n.t("js.discourse_ai.usage.periods.last_day")
+ when :week
+ I18n.t("js.discourse_ai.usage.periods.last_week")
+ when :month
+ I18n.t("js.discourse_ai.usage.periods.last_month")
+ end
+
+ find(".ai-usage__period-buttons .btn", text: label).click
+
+ self
+ end
end
end
end