discourse-ai/lib/sentiment/sentiment_dashboard_report.rb
Keegan George 375dd702b2
UX: Make sentiment trends more readable in time series data (#1013)
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.
2024-12-10 07:22:41 -08:00

59 lines
1.9 KiB
Ruby

# frozen_string_literal: true
module DiscourseAi
module Sentiment
class SentimentDashboardReport
def self.register!(plugin)
plugin.add_report("overall_sentiment") do |report|
report.modes = [:chart]
threshold = 0.6
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")}
FROM
classification_results AS cr
INNER JOIN posts p ON p.id = cr.target_id AND cr.target_type = 'Post'
INNER JOIN topics t ON t.id = p.topic_id
INNER JOIN categories c ON c.id = t.category_id
WHERE
t.archetype = 'regular' AND
p.user_id > 0 AND
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)
SQL
report_start: report.start_date,
report_end: report.end_date,
threshold: threshold,
)
return report if grouped_sentiments.empty?
report.data =
grouped_sentiments.map do |gs|
{
color: report.colors[:lime],
label: I18n.t("discourse_ai.sentiment.reports.overall_sentiment"),
data: {
x: gs.posted_at,
y: gs.public_send("positive_count") - gs.public_send("negative_count"),
},
}
end
end
end
end
end
end