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

Commit 8e00e03

Browse files
authored
FEATURE: Make emotion /filter ordering match the dashboard table (#939)
* FEATURE: Make emotion /filter ordering match the dashboard table This change makes the /filter endpoint use the same criteria we use in the dashboard table for emotion, so it is not confusing for users. It means that only posts made in the period with the emotion shall be shown in the /filter, and the order is simply a count of posts that match the emotion in the period. It also uses a trick to extract the filter period, and apply it to the CTE clause that calculates post emotion count on the period, making it a bit more efficient. Downside is that /filter filters are evaluated from left to right, so it will only get the speed-up if the emotion order is last. As we do this on the dashboard table, it should cover most uses of the ordering, kicking the need for materialized views down the road. * Remove zero score in filter * add table tooltip * lint
1 parent d56ed53 commit 8e00e03

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

assets/javascripts/discourse/templates/admin-dashboard-sentiment.hbs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@
2727
<ul class="breadcrumb">
2828
<li class="item report">
2929
<LinkTo @route="adminReports" class="report-url">
30-
{{i18n "admin.dashboard.emotion"}}
30+
{{i18n "admin.dashboard.emotion.title"}}
3131
</LinkTo>
32+
<DTooltip @interactive="true">
33+
<:trigger>
34+
{{d-icon "circle-question"}}
35+
</:trigger>
36+
<:content>
37+
<span>{{i18n "admin.dashboard.emotion.description"}}</span>
38+
</:content>
39+
</DTooltip>
3240
</li>
3341
</ul>
3442
</div>

config/locales/client.en.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ en:
1212
categories:
1313
discourse_ai: "Discourse AI"
1414
dashboard:
15-
emotion: "Emotion"
15+
emotion:
16+
title: "Emotion"
17+
description: "The table lists a count of posts classified with a determined emotion. Classified with the model 'SamLowe/roberta-base-go_emotions'."
1618
js:
1719
discourse_automation:
1820
scriptables:

config/locales/server.en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ en:
112112
reports:
113113
overall_sentiment:
114114
title: "Overall sentiment"
115-
description: 'The chart compares the number of posts classified as either positive or negative. These are calculated when positive or negative scores > the set threshold score. This means neutral posts are not shown. Private messages (PMs) are also excluded. Classified with "cardiffnlp/twitter-roberta-base-sentiment-latest"'
115+
description: 'The chart compares the number of posts classified as either positive or negative. These are calculated when positive or negative scores > the set threshold score. This means neutral posts are not shown. Personal messages (PMs) are also excluded. Classified with "cardiffnlp/twitter-roberta-base-sentiment-latest"'
116116
xaxis: "Positive(%)"
117117
yaxis: "Date"
118118
emotion_admiration:

lib/sentiment/emotion_filter_order.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@ class EmotionFilterOrder
66
def self.register!(plugin)
77
Emotions::LIST.each do |emotion|
88
filter_order_emotion = ->(scope, order_direction) do
9+
scope_period =
10+
scope
11+
.arel
12+
&.constraints
13+
&.flat_map(&:children)
14+
&.find do |node|
15+
node.is_a?(Arel::Nodes::Grouping) &&
16+
node.expr.to_s.match?(/topics\.bumped_at\s*>=/)
17+
end
18+
&.expr
19+
&.split(">=")
20+
&.last if scope.arel.constraints.present? &&
21+
scope.arel.constraints.any? { |c| c.is_a?(Arel::Nodes::Grouping) }
22+
23+
# Fallback in case we can't find the scope period
24+
scope_period ||= "CURRENT_DATE - INTERVAL '1 year'"
25+
926
emotion_clause = <<~SQL
10-
SUM(
11-
CASE
12-
WHEN (classification_results.classification::jsonb->'#{emotion}')::float > 0.1
13-
THEN 1
14-
ELSE 0
15-
END
16-
)::float / COUNT(posts.id)
27+
COUNT(*) FILTER (WHERE (classification_results.classification::jsonb->'#{emotion}')::float > 0.1)
1728
SQL
1829

1930
# TODO: This is slow, we will need to materialize this in the future
@@ -35,10 +46,11 @@ def self.register!(plugin)
3546
AND topics.deleted_at IS NULL
3647
AND posts.deleted_at IS NULL
3748
AND posts.post_type = 1
49+
AND posts.created_at >= #{scope_period}
3850
GROUP BY
3951
1
4052
HAVING
41-
#{emotion_clause} > 0.05
53+
#{emotion_clause} > 0
4254
SQL
4355

4456
scope

0 commit comments

Comments
 (0)