Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 4fca0b8

Browse files
FEATURE: Load usage data after page load
Use ConditionalLoadingSpinner to hide load of usage data, this prevents us hanging on page load with a white screen.
1 parent 5345104 commit 4fca0b8

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

app/controllers/discourse_ai/admin/ai_usage_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class AiUsageController < ::Admin::AdminController
66
requires_plugin "discourse-ai"
77

88
def show
9+
end
10+
11+
def report
912
render json: AiUsageSerializer.new(create_report, root: false)
1013
end
1114

assets/javascripts/discourse/components/ai-usage.gjs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { action } from "@ember/object";
55
import { LinkTo } from "@ember/routing";
66
import { service } from "@ember/service";
77
import { eq } from "truth-helpers";
8+
import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";
89
import DButton from "discourse/components/d-button";
910
import DateTimeInputRange from "discourse/components/date-time-input-range";
1011
import avatar from "discourse/helpers/avatar";
@@ -24,18 +25,30 @@ export default class AiUsage extends Component {
2425
@tracked selectedModel;
2526
@tracked selectedPeriod = "month";
2627
@tracked isCustomDateActive = false;
28+
@tracked loadingData = true;
29+
30+
constructor() {
31+
super(...arguments);
32+
this.fetchData();
33+
}
2734

2835
@action
2936
async fetchData() {
30-
const response = await ajax("/admin/plugins/discourse-ai/ai-usage.json", {
31-
data: {
32-
start_date: moment(this.startDate).format("YYYY-MM-DD"),
33-
end_date: moment(this.endDate).format("YYYY-MM-DD"),
34-
feature: this.selectedFeature,
35-
model: this.selectedModel,
36-
},
37-
});
37+
const response = await ajax(
38+
"/admin/plugins/discourse-ai/ai-usage-report.json",
39+
{
40+
data: {
41+
start_date: moment(this.startDate).format("YYYY-MM-DD"),
42+
end_date: moment(this.endDate).format("YYYY-MM-DD"),
43+
feature: this.selectedFeature,
44+
model: this.selectedModel,
45+
},
46+
}
47+
);
3848
this.data = response;
49+
this.loadingData = false;
50+
this._cachedFeatures = null;
51+
this._cachedModels = null;
3952
}
4053

4154
@action
@@ -312,7 +325,7 @@ export default class AiUsage extends Component {
312325
/>
313326
</div>
314327

315-
{{#if this.data}}
328+
<ConditionalLoadingSpinner @condition={{this.loadingData}}>
316329
<AdminConfigAreaCard
317330
@heading="discourse_ai.usage.summary"
318331
class="ai-usage__summary"
@@ -492,7 +505,7 @@ export default class AiUsage extends Component {
492505
</:content>
493506
</AdminConfigAreaCard>
494507
</div>
495-
{{/if}}
508+
</ConditionalLoadingSpinner>
496509
</div>
497510
</div>
498511
</template>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
to: "discourse_ai/admin/rag_document_fragments#indexing_status_check"
8080

8181
get "/ai-usage", to: "discourse_ai/admin/ai_usage#show"
82+
get "/ai-usage-report", to: "discourse_ai/admin/ai_usage#report"
8283

8384
resources :ai_llms,
8485
only: %i[index create show update destroy],

0 commit comments

Comments
 (0)