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.gjsindex 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 {@actiononPeriodSelect(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 {@actiononDateChange() {+ 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 {@actiononRefreshDateRange() {+ 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.rbindex 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 dolet(: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 doenable_current_pluginsign_in(admin)end- context "when viewing usage data with total rows" do+ context "when viewing usage data" dobefore 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.visitexpect(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"))- endendend- 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)endend- 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" doai_usage_page.visitmodel_selector = ai_usage_page.model_selectormodel_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 dropdownmodel_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)endendenddiff --git a/plugins/discourse-ai/spec/system/page_objects/pages/ai_usage.rb b/plugins/discourse-ai/spec/system/page_objects/pages/ai_usage.rbindex 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 PageObjectsdef feature_selectorPageObjects::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+ endendendend