@@ -5,22 +5,21 @@ module Sentiment
55 class SentimentDashboardReport
66 def self . register! ( plugin )
77 plugin . add_report ( "overall_sentiment" ) do |report |
8- report . modes = [ :chart ]
8+ report . modes = [ :stacked_chart ]
99 threshold = 0.6
1010
1111 sentiment_count_sql = Proc . new { |sentiment | <<~SQL }
1212 COUNT(
1313 CASE WHEN (cr.classification::jsonb->'#{ sentiment } ')::float > :threshold THEN 1 ELSE NULL END
14- ) AS #{ sentiment } _count
14+ )
1515 SQL
1616
1717 grouped_sentiments =
1818 DB . query (
1919 <<~SQL ,
2020 SELECT
2121 DATE_TRUNC('day', p.created_at)::DATE AS posted_at,
22- #{ sentiment_count_sql . call ( "positive" ) } ,
23- -#{ sentiment_count_sql . call ( "negative" ) }
22+ #{ sentiment_count_sql . call ( "positive" ) } - #{ sentiment_count_sql . call ( "negative" ) } AS sentiment_count
2423 FROM
2524 classification_results AS cr
2625 INNER JOIN posts p ON p.id = cr.target_id AND cr.target_type = 'Post'
@@ -32,6 +31,7 @@ def self.register!(plugin)
3231 cr.model_used = 'cardiffnlp/twitter-roberta-base-sentiment-latest' AND
3332 (p.created_at > :report_start AND p.created_at < :report_end)
3433 GROUP BY DATE_TRUNC('day', p.created_at)
34+ ORDER BY 1 ASC
3535 SQL
3636 report_start : report . start_date ,
3737 report_end : report . end_date ,
@@ -40,17 +40,15 @@ def self.register!(plugin)
4040
4141 return report if grouped_sentiments . empty?
4242
43- report . data =
44- grouped_sentiments . map do |gs |
45- {
46- color : report . colors [ :lime ] ,
47- label : I18n . t ( "discourse_ai.sentiment.reports.overall_sentiment" ) ,
48- data : {
49- x : gs . posted_at ,
50- y : gs . public_send ( "positive_count" ) - gs . public_send ( "negative_count" ) ,
51- } ,
52- }
53- end
43+ report . data = {
44+ req : "overall_sentiment" ,
45+ color : report . colors [ :lime ] ,
46+ label : I18n . t ( "discourse_ai.sentiment.reports.overall_sentiment" ) ,
47+ data :
48+ grouped_sentiments . map do |gs |
49+ { x : gs . posted_at , y : gs . public_send ( "sentiment_count" ) }
50+ end ,
51+ }
5452 end
5553 end
5654 end
0 commit comments