Skip to content

Commit

Permalink
UX: Make sentiment trends more readable (#1018)
Browse files Browse the repository at this point in the history
Instead of a stacked chart showing a separate series for positive and negative, this PR introduces a simplification to the overall sentiment dashboard. It comprises the sentiment into a single series of the difference between `positive - negative` instead. This should allow for the data to be more easy to scan and look for trends
  • Loading branch information
keegangeorge authored Dec 11, 2024
1 parent 5fc7a73 commit a4440c5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
4 changes: 1 addition & 3 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ en:

sentiment:
reports:
overall_sentiment:
positive: "Positive"
negative: "Negative"
overall_sentiment: "Overall sentiment (Positive - Negative)"
post_emotion:
sadness: "Sadness 😢"
surprise: "Surprise 😱"
Expand Down
29 changes: 12 additions & 17 deletions lib/sentiment/sentiment_dashboard_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ def self.register!(plugin)
sentiment_count_sql = Proc.new { |sentiment| <<~SQL }
COUNT(
CASE WHEN (cr.classification::jsonb->'#{sentiment}')::float > :threshold THEN 1 ELSE NULL END
) AS #{sentiment}_count
)
SQL

grouped_sentiments =
DB.query(
<<~SQL,
SELECT
DATE_TRUNC('day', p.created_at)::DATE AS posted_at,
#{sentiment_count_sql.call("positive")},
-#{sentiment_count_sql.call("negative")}
#{sentiment_count_sql.call("positive")} - #{sentiment_count_sql.call("negative")} AS sentiment_count
FROM
classification_results AS cr
INNER JOIN posts p ON p.id = cr.target_id AND cr.target_type = 'Post'
Expand All @@ -32,28 +31,24 @@ def self.register!(plugin)
cr.model_used = 'cardiffnlp/twitter-roberta-base-sentiment-latest' AND
(p.created_at > :report_start AND p.created_at < :report_end)
GROUP BY DATE_TRUNC('day', p.created_at)
ORDER BY 1 ASC
SQL
report_start: report.start_date,
report_end: report.end_date,
threshold: threshold,
)

data_points = %w[positive negative]

return report if grouped_sentiments.empty?

report.data =
data_points.map do |point|
{
req: "sentiment_#{point}",
color: point == "positive" ? report.colors[:lime] : report.colors[:purple],
label: I18n.t("discourse_ai.sentiment.reports.overall_sentiment.#{point}"),
data:
grouped_sentiments.map do |gs|
{ x: gs.posted_at, y: gs.public_send("#{point}_count") }
end,
}
end
report.data = {
req: "overall_sentiment",
color: report.colors[:lime],
label: I18n.t("discourse_ai.sentiment.reports.overall_sentiment"),
data:
grouped_sentiments.map do |gs|
{ x: gs.posted_at, y: gs.public_send("sentiment_count") }
end,
}
end
end
end
Expand Down
7 changes: 2 additions & 5 deletions spec/lib/modules/sentiment/entry_point_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ def sentiment_classification(post, classification)
sentiment_classification(pm, positive_classification)

report = Report.find("overall_sentiment")
positive_data_point = report.data[0][:data].first[:y].to_i
negative_data_point = report.data[1][:data].first[:y].to_i

expect(positive_data_point).to eq(1)
expect(negative_data_point).to eq(-1)
overall_sentiment = report.data[:data][0][:y].to_i
expect(overall_sentiment).to eq(0)
end
end

Expand Down

0 comments on commit a4440c5

Please sign in to comment.