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

Commit 715c4b2

Browse files
committed
fix tests
1 parent 65097b7 commit 715c4b2

File tree

3 files changed

+87
-56
lines changed

3 files changed

+87
-56
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
@filters={{this.filters}}
2323
@showHeader={{true}}
2424
/>
25-
26-
2725
<div class="admin-report activity-metrics">
2826
<div class="header">
2927
<ul class="breadcrumb">
@@ -51,7 +49,6 @@
5149
"admin.dashboard.reports.last_30_days"
5250
}}</div>
5351
</div>
54-
5552
{{#each this.emotions as |metric|}}
5653
<AdminReport
5754
@showHeader={{false}}
@@ -62,7 +59,6 @@
6259
</div>
6360
</div>
6461
</div>
65-
6662
</div>
6763
</div>
6864
</div>

lib/sentiment/emotion_dashboard_report.rb

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,32 @@ def self.register!(plugin)
1414
end
1515

1616
def self.fetch_data
17-
Discourse
18-
.cache
19-
.fetch("emotion_dashboard", expires_in: 5.minutes) do
20-
DB.query(<<~SQL, start: Time.now.midnight, end: 60.days.ago.midnight)
21-
SELECT
22-
posts.created_at::DATE + 540 AS day,
23-
#{
24-
DiscourseAi::Sentiment::Emotions::LIST
25-
.map do |emotion|
26-
"COUNT(*) FILTER (WHERE (classification_results.classification::jsonb->'#{emotion}')::float > 0.1) AS #{emotion}"
27-
end
28-
.join(",\n ")
29-
}
30-
FROM
31-
classification_results
32-
INNER JOIN
33-
posts ON posts.id = classification_results.target_id AND
34-
posts.deleted_at IS NULL AND
35-
posts.created_at BETWEEN :start AND :end
36-
INNER JOIN
37-
topics ON topics.id = posts.topic_id AND
38-
topics.archetype = 'regular' AND
39-
topics.deleted_at IS NULL
40-
WHERE
41-
classification_results.target_type = 'Post' AND
42-
classification_results.model_used = 'SamLowe/roberta-base-go_emotions'
43-
GROUP BY 1
44-
ORDER BY 1 ASC
45-
SQL
46-
end
17+
DB.query(<<~SQL, end: Time.now.tomorrow.midnight, start: 60.days.ago.midnight)
18+
SELECT
19+
posts.created_at::DATE AS day,
20+
#{
21+
DiscourseAi::Sentiment::Emotions::LIST
22+
.map do |emotion|
23+
"COUNT(*) FILTER (WHERE (classification_results.classification::jsonb->'#{emotion}')::float > 0.1) AS #{emotion}"
24+
end
25+
.join(",\n ")
26+
}
27+
FROM
28+
classification_results
29+
INNER JOIN
30+
posts ON posts.id = classification_results.target_id AND
31+
posts.deleted_at IS NULL AND
32+
posts.created_at BETWEEN :start AND :end
33+
INNER JOIN
34+
topics ON topics.id = posts.topic_id AND
35+
topics.archetype = 'regular' AND
36+
topics.deleted_at IS NULL
37+
WHERE
38+
classification_results.target_type = 'Post' AND
39+
classification_results.model_used = 'SamLowe/roberta-base-go_emotions'
40+
GROUP BY 1
41+
ORDER BY 1 ASC
42+
SQL
4743
end
4844
end
4945
end

spec/lib/modules/sentiment/entry_point_spec.rb

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,69 @@ def sentiment_classification(post, classification)
8888
describe "post_emotion report" do
8989
let(:emotion_1) do
9090
{
91-
sadness: 0.49,
92-
surprise: 0.23,
93-
neutral: 0.6,
94-
fear: 0.34,
95-
anger: 0.87,
96-
joy: 0.22,
97-
disgust: 0.70,
91+
love: 0.9444406,
92+
admiration: 0.013724019,
93+
surprise: 0.010188869,
94+
excitement: 0.007888741,
95+
curiosity: 0.006301749,
96+
joy: 0.004060776,
97+
confusion: 0.0028238264,
98+
approval: 0.0018160914,
99+
realization: 0.001174849,
100+
neutral: 0.0008561869,
101+
amusement: 0.00075853954,
102+
disapproval: 0.0006987994,
103+
disappointment: 0.0006166883,
104+
anger: 0.0006000542,
105+
annoyance: 0.0005615011,
106+
desire: 0.00046368592,
107+
fear: 0.00045117878,
108+
sadness: 0.00041727215,
109+
gratitude: 0.00041727215,
110+
optimism: 0.00037112957,
111+
disgust: 0.00035552034,
112+
nervousness: 0.00022954118,
113+
embarrassment: 0.0002049572,
114+
caring: 0.00017737568,
115+
remorse: 0.00011407586,
116+
grief: 0.0001006716,
117+
pride: 0.00009681493,
118+
relief: 0.00008919009,
98119
}
99120
end
100121
let(:emotion_2) do
101122
{
102-
sadness: 0.19,
103-
surprise: 0.63,
104-
neutral: 0.45,
105-
fear: 0.44,
106-
anger: 0.27,
107-
joy: 0.62,
108-
disgust: 0.30,
123+
love: 0.8444406,
124+
admiration: 0.113724019,
125+
surprise: 0.010188869,
126+
excitement: 0.007888741,
127+
curiosity: 0.006301749,
128+
joy: 0.004060776,
129+
confusion: 0.0028238264,
130+
approval: 0.0018160914,
131+
realization: 0.001174849,
132+
neutral: 0.0008561869,
133+
amusement: 0.00075853954,
134+
disapproval: 0.0006987994,
135+
disappointment: 0.0006166883,
136+
anger: 0.0006000542,
137+
annoyance: 0.0005615011,
138+
desire: 0.00046368592,
139+
fear: 0.00045117878,
140+
sadness: 0.00041727215,
141+
gratitude: 0.00041727215,
142+
optimism: 0.00037112957,
143+
disgust: 0.00035552034,
144+
nervousness: 0.00022954118,
145+
embarrassment: 0.0002049572,
146+
caring: 0.00017737568,
147+
remorse: 0.00011407586,
148+
grief: 0.0001006716,
149+
pride: 0.00009681493,
150+
relief: 0.00008919009,
109151
}
110152
end
111-
let(:model_used) { "j-hartmann/emotion-english-distilroberta-base" }
153+
let(:model_used) { "SamLowe/roberta-base-go_emotions" }
112154

113155
def emotion_classification(post, classification)
114156
Fabricate(
@@ -125,22 +167,19 @@ def strip_emoji_and_downcase(str)
125167
end
126168

127169
it "calculate averages using only public posts" do
128-
threshold = 0.30
170+
threshold = 0.10
129171

130172
emotion_classification(post_1, emotion_1)
131173
emotion_classification(post_2, emotion_2)
132174
emotion_classification(pm, emotion_2)
133175

134-
report = Report.find("post_emotion")
176+
report = Report.find("emotion_love")
135177

136178
data_point = report.data
137179

138180
data_point.each do |point|
139-
emotion = strip_emoji_and_downcase(point[:label])
140-
expected =
141-
(emotion_1[emotion.to_sym] > threshold ? 1 : 0) +
142-
(emotion_2[emotion.to_sym] > threshold ? 1 : 0)
143-
expect(point[:data][0][:y]).to eq(expected)
181+
expected = (emotion_1[:love] > threshold ? 1 : 0) + (emotion_2[:love] > threshold ? 1 : 0)
182+
expect(point[:y]).to eq(expected)
144183
end
145184
end
146185
end

0 commit comments

Comments
 (0)