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

Commit 3066dff

Browse files
committed
DEV: add pagination for post requests
1 parent 448ac67 commit 3066dff

File tree

3 files changed

+64
-20
lines changed

3 files changed

+64
-20
lines changed

app/controllers/discourse_ai/sentiment/sentiment_controller.rb

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ class SentimentController < ::Admin::StaffController
66
include Constants
77
requires_plugin ::DiscourseAi::PLUGIN_NAME
88

9+
# DEFAULT_POSTS_LIMIT = 50
10+
# MAX_POSTS_LIMIT = 100
11+
DEFAULT_POSTS_LIMIT = 3
12+
MAX_POSTS_LIMIT = 3
13+
914
def posts
1015
group_by = params.required(:group_by)&.to_sym
1116
group_value = params.required(:group_value).presence
@@ -15,6 +20,9 @@ def posts
1520

1621
raise Discourse::InvalidParameters if %i[category tag].exclude?(group_by)
1722

23+
limit = fetch_limit_from_params(default: DEFAULT_POSTS_LIMIT, max: MAX_POSTS_LIMIT)
24+
offset = params[:offset].to_i || 0
25+
1826
case group_by
1927
when :category
2028
grouping_clause = "c.name"
@@ -56,22 +64,31 @@ def posts
5664
((:start_date IS NULL OR p.created_at > :start_date) AND (:end_date IS NULL OR p.created_at < :end_date))
5765
AND p.deleted_at IS NULL
5866
ORDER BY p.created_at DESC
67+
LIMIT :limit OFFSET :offset
5968
SQL
6069
group_value: group_value,
6170
start_date: start_date,
6271
end_date: end_date,
6372
threshold: threshold,
73+
limit: limit + 1,
74+
offset: offset,
6475
)
6576

77+
has_more = posts.length > limit
78+
posts.pop if has_more
79+
6680
render_json_dump(
67-
serialize_data(
68-
posts,
69-
AiSentimentPostSerializer,
70-
scope: guardian,
71-
add_raw: true,
72-
add_excerpt: true,
73-
add_title: true,
74-
),
81+
posts:
82+
serialize_data(
83+
posts,
84+
AiSentimentPostSerializer,
85+
scope: guardian,
86+
add_raw: true,
87+
add_excerpt: true,
88+
add_title: true,
89+
),
90+
has_more: has_more,
91+
next_offset: has_more ? offset + limit : nil,
7592
)
7693
end
7794
end

assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import DoughnutChart from "discourse/plugins/discourse-ai/discourse/components/d
1515
export default class AdminReportSentimentAnalysis extends Component {
1616
@tracked selectedChart = null;
1717
@tracked posts = null;
18+
@tracked hasMorePosts = false;
19+
@tracked nextOffset = 0;
1820

1921
get colors() {
2022
return ["#2ecc71", "#95a5a6", "#e74c3c"];
@@ -50,20 +52,45 @@ export default class AdminReportSentimentAnalysis extends Component {
5052
});
5153
}
5254

55+
async postRequest() {
56+
return await ajax("/discourse-ai/sentiment/posts", {
57+
data: {
58+
group_by: this.currentGroupFilter,
59+
group_value: this.selectedChart?.title,
60+
start_date: this.args.model.start_date,
61+
end_date: this.args.model.end_date,
62+
offset: this.nextOffset,
63+
},
64+
});
65+
}
66+
5367
@action
5468
async showDetails(data) {
5569
this.selectedChart = data;
70+
5671
try {
57-
const posts = await ajax(`/discourse-ai/sentiment/posts`, {
58-
data: {
59-
group_by: this.currentGroupFilter,
60-
group_value: data.title,
61-
start_date: this.args.model.start_date,
62-
end_date: this.args.model.end_date,
63-
},
64-
});
65-
66-
this.posts = posts.map((post) => Post.create(post));
72+
const response = await this.postRequest();
73+
74+
this.posts = response.posts.map((post) => Post.create(post));
75+
this.hasMorePosts = response.has_more;
76+
this.nextOffset = response.next_offset;
77+
} catch (e) {
78+
popupAjaxError(e);
79+
}
80+
}
81+
82+
@action
83+
async fetchMorePosts() {
84+
if (!this.hasMorePosts || this.selectedChart === null) {
85+
return [];
86+
}
87+
88+
try {
89+
const response = await this.postRequest();
90+
91+
this.hasMorePosts = response.has_more;
92+
this.nextOffset = response.next_offset;
93+
return response.posts.map((post) => Post.create(post));
6794
} catch (e) {
6895
popupAjaxError(e);
6996
}
@@ -139,7 +166,7 @@ export default class AdminReportSentimentAnalysis extends Component {
139166

140167
<ul class="admin-report-sentiment-analysis-details__scores">
141168
<li>
142-
{{dIcon "face-smile" style="color: #2ecc71"}}
169+
{{dIcon "face-smile"}}
143170
{{i18n
144171
"discourse_ai.sentiments.sentiment_analysis.score_types.positive"
145172
}}:
@@ -164,6 +191,7 @@ export default class AdminReportSentimentAnalysis extends Component {
164191
@idPath="post_id"
165192
@titlePath="topic_title"
166193
@usernamePath="username"
194+
@fetchMorePosts={{this.fetchMorePosts}}
167195
class="admin-report-sentiment-analysis-details__post-list"
168196
>
169197
<:abovePostItemExcerpt as |post|>

lib/sentiment/sentiment_analysis_report.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def self.fetch_data(report, opts)
6868
grouping = (report.filters.dig(:group_by) || GROUP_BY_FILTER_DEFAULT).to_sym
6969
sorting = (report.filters.dig(:sort_by) || SORT_BY_FILTER_DEFAULT).to_sym
7070
category_filter = report.filters.dig(:category)
71-
pp "========================== category_filter ===================================== #{category_filter} include subcategories?: #{opts[:include_subcategories]}"
7271
tag_filter = report.filters.dig(:tag)
7372

7473
sentiment_count_sql = Proc.new { |sentiment| <<~SQL }

0 commit comments

Comments
 (0)