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

Commit d2cf823

Browse files
committed
DEV: Sentiment analysis report follow-up updates (#1145)
* DEV: make include subcategories checkbox operational * DEV: add pagination for post requests * WIP: selected chart UX improvements * DEV: Functional sentiment filters * DEV: Reset filters after going back * DEV: Add category colors, improve UX * DEV: Update spec
1 parent f849633 commit d2cf823

File tree

7 files changed

+312
-108
lines changed

7 files changed

+312
-108
lines changed

app/controllers/discourse_ai/sentiment/sentiment_controller.rb

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ 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+
912
def posts
1013
group_by = params.required(:group_by)&.to_sym
1114
group_value = params.required(:group_value).presence
@@ -15,10 +18,13 @@ def posts
1518

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

21+
limit = fetch_limit_from_params(default: DEFAULT_POSTS_LIMIT, max: MAX_POSTS_LIMIT)
22+
offset = params[:offset].to_i || 0
23+
1824
case group_by
1925
when :category
2026
grouping_clause = "c.name"
21-
grouping_join = "INNER JOIN categories c ON c.id = t.category_id"
27+
grouping_join = "" # categories already joined
2228
when :tag
2329
grouping_clause = "tags.name"
2430
grouping_join =
@@ -38,6 +44,11 @@ def posts
3844
u.username,
3945
u.name,
4046
u.uploaded_avatar_id,
47+
c.id AS category_id,
48+
c.name AS category_name,
49+
c.color AS category_color,
50+
c.slug AS category_slug,
51+
c.description AS category_description,
4152
(CASE
4253
WHEN (cr.classification::jsonb->'positive')::float > :threshold THEN 'positive'
4354
WHEN (cr.classification::jsonb->'negative')::float > :threshold THEN 'negative'
@@ -47,6 +58,7 @@ def posts
4758
INNER JOIN topics t ON t.id = p.topic_id
4859
INNER JOIN classification_results cr ON cr.target_id = p.id AND cr.target_type = 'Post'
4960
LEFT JOIN users u ON u.id = p.user_id
61+
LEFT JOIN categories c ON c.id = t.category_id
5062
#{grouping_join}
5163
WHERE
5264
#{grouping_clause} = :group_value AND
@@ -56,22 +68,31 @@ def posts
5668
((:start_date IS NULL OR p.created_at > :start_date) AND (:end_date IS NULL OR p.created_at < :end_date))
5769
AND p.deleted_at IS NULL
5870
ORDER BY p.created_at DESC
71+
LIMIT :limit OFFSET :offset
5972
SQL
6073
group_value: group_value,
6174
start_date: start_date,
6275
end_date: end_date,
6376
threshold: threshold,
77+
limit: limit + 1,
78+
offset: offset,
6479
)
6580

81+
has_more = posts.length > limit
82+
posts.pop if has_more
83+
6684
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-
),
85+
posts:
86+
serialize_data(
87+
posts,
88+
AiSentimentPostSerializer,
89+
scope: guardian,
90+
add_raw: true,
91+
add_excerpt: true,
92+
add_title: true,
93+
),
94+
has_more: has_more,
95+
next_offset: has_more ? offset + limit : nil,
7596
)
7697
end
7798
end

app/serializers/ai_sentiment_post_serializer.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class AiSentimentPostSerializer < ApplicationSerializer
1010
:avatar_template,
1111
:excerpt,
1212
:sentiment,
13-
:truncated
13+
:truncated,
14+
:category
1415

1516
def avatar_template
1617
User.avatar_template(object.username, object.uploaded_avatar_id)
@@ -23,4 +24,14 @@ def excerpt
2324
def truncated
2425
true
2526
end
27+
28+
def category
29+
{
30+
id: object.category_id,
31+
name: object.category_name,
32+
color: object.category_color,
33+
slug: object.category_slug,
34+
description: object.category_description,
35+
}
36+
end
2637
end

0 commit comments

Comments
 (0)