A platform for community discussion. Free, open, simple.
FIX: preserve model/feature dropdown options when filtering on AI usage page
zogstripa1cc09d22dea
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjsindex b8e3e2acce4..62cdd957797 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();+ this.fetchData({ clearCache: true });}@action- async fetchData() {+ async fetchData({ clearCache = false } = {}) {const response = await ajax("/admin/plugins/discourse-ai/ai-usage-report.json",{@@ -60,8 +60,11 @@ export default class AiUsage extends Component {);this.data = response;this.loadingData = false;- this._cachedFeatures = null;- this._cachedModels = null;++ if (clearCache) {+ this._cachedFeatures = null;+ this._cachedModels = null;+ }}@action@@ -400,8 +403,6 @@ export default class AiUsage extends Component {}get availableFeatures() {- // when you switch we don't want the list to change- // only when you switch durationsthis._cachedFeatures =this._cachedFeatures ||(this.data?.features || []).map((f) => ({@@ -456,7 +457,7 @@ export default class AiUsage extends Component {this.selectedPeriod = period;this.isCustomDateActive = false;this.setPeriodDates(period);- this.fetchData();+ this.fetchData({ clearCache: true });}@action@@ -471,7 +472,7 @@ export default class AiUsage extends Component {onDateChange() {this.isCustomDateActive = true;this.selectedPeriod = null;- this.fetchData();+ this.fetchData({ clearCache: true });}@action@@ -484,7 +485,7 @@ export default class AiUsage extends Component {onRefreshDateRange() {this.startDate = this._startDate;this.endDate = this._endDate;- this.fetchData();+ this.fetchData({ clearCache: true });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 4a8fffee1b1..540f506cc71 100644--- a/plugins/discourse-ai/spec/system/ai_usage_spec.rb+++ b/plugins/discourse-ai/spec/system/ai_usage_spec.rb@@ -4,6 +4,8 @@ RSpec.describe "AI Usage Admin Page", type: :system dofab!(:admin)fab!(:llm_model)+ let(:ai_usage_page) { PageObjects::Pages::AiUsage.new }+before doenable_current_pluginsign_in(admin)@@ -71,4 +73,44 @@ RSpec.describe "AI Usage Admin Page", type: :system doexpect(page).to have_css(".ai-usage__summary")endend++ context "when filtering by model" do+ fab!(:other_model) { Fabricate(:llm_model, display_name: "Other Model", name: "other-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,+ )+ end++ it "keeps all models in dropdown after selecting a filter" 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_selected_name(llm_model.display_name)++ # Verify other models are still available in the dropdown+ model_selector.expand+ expect(model_selector).to have_option_name(other_model.display_name)+ end+ endenddiff --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 1fd84d204f7..60f7ea7349d 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@@ -67,6 +67,14 @@ module PageObjectsdef has_users_total_row?page.has_css?(".ai-usage__users-table .ai-usage__total-row")end++ def model_selector+ PageObjects::Components::SelectKit.new(".ai-usage__model-selector")+ end++ def feature_selector+ PageObjects::Components::SelectKit.new(".ai-usage__feature-selector")+ endendendend