From 5fb1177f7bb3d03dfa37e2c6971f492775edb96f Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Mon, 25 Nov 2024 11:31:51 -0300 Subject: [PATCH] FEATURE: Refinements to Emotion in dashboard (#947) * FEATURE: Refinements to Emotion in dashboard - Added descriptions to individual reports - Made reports work with data older than 60 days --- .../components/admin-report-emotion.hbs | 25 +++-------------- .../controllers/admin-dashboard-sentiment.js | 7 +++++ .../templates/admin-dashboard-sentiment.hbs | 1 + config/locales/server.en.yml | 28 +++++++++++++++++++ lib/sentiment/emotion_dashboard_report.rb | 13 +++++---- 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/assets/javascripts/discourse/components/admin-report-emotion.hbs b/assets/javascripts/discourse/components/admin-report-emotion.hbs index a546ef81..fb0119bd 100644 --- a/assets/javascripts/discourse/components/admin-report-emotion.hbs +++ b/assets/javascripts/discourse/components/admin-report-emotion.hbs @@ -6,22 +6,14 @@
- - {{number this.model.todayCount}} - + {{number this.model.todayCount}}
- - {{number this.model.yesterdayCount}} - + {{number this.model.yesterdayCount}} {{d-icon this.model.yesterdayTrendIcon}}
@@ -29,11 +21,7 @@ class="cell value sevendays-count {{this.model.sevenDaysTrend}}" title={{this.model.sevenDaysCountTitle}} > - - {{number this.model.lastSevenDaysCount}} - + {{number this.model.lastSevenDaysCount}} {{d-icon this.model.sevenDaysTrendIcon}} @@ -41,12 +29,7 @@ class="cell value thirty-days-count {{this.model.thirtyDaysTrend}}" title={{this.model.thirtyDaysCountTitle}} > - - {{number this.model.lastThirtyDaysCount}} - - + {{number this.model.lastThirtyDaysCount}} {{#if this.model.canDisplayTrendIcon}} {{d-icon this.model.thirtyDaysTrendIcon}} {{/if}} diff --git a/assets/javascripts/discourse/controllers/admin-dashboard-sentiment.js b/assets/javascripts/discourse/controllers/admin-dashboard-sentiment.js index a74ef8ed..824daf24 100644 --- a/assets/javascripts/discourse/controllers/admin-dashboard-sentiment.js +++ b/assets/javascripts/discourse/controllers/admin-dashboard-sentiment.js @@ -7,6 +7,13 @@ export default class AdminDashboardSentiment extends AdminDashboardTabController return { startDate: this.startDate, endDate: this.endDate }; } + get emotionFilters() { + return { + startDate: moment().format("YYYY-MM-DD"), + endDate: moment().subtract(2, "month").format("YYYY-MM-DD"), + }; + } + get emotions() { const emotions = [ "admiration", diff --git a/assets/javascripts/discourse/templates/admin-dashboard-sentiment.hbs b/assets/javascripts/discourse/templates/admin-dashboard-sentiment.hbs index 5d20c19f..0e2310d8 100644 --- a/assets/javascripts/discourse/templates/admin-dashboard-sentiment.hbs +++ b/assets/javascripts/discourse/templates/admin-dashboard-sentiment.hbs @@ -60,6 +60,7 @@ {{#each this.emotions as |metric|}} diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 37a2fe46..0c717649 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -117,60 +117,88 @@ en: yaxis: "Date" emotion_admiration: title: ðŸĪĐ Admiration + description: "Posts classified with the emotion admiration via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_amusement: title: 😄 Amusement + description: "Posts classified with the emotion amusement via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_anger: title: 😠 Anger + description: "Posts classified with the emotion anger via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_annoyance: title: 😒 Annoyance + description: "Posts classified with the emotion annoyance via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_approval: title: 👍 Approval + description: "Posts classified with the emotion approval via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_caring: title: ðŸĪ— Caring + description: "Posts classified with the emotion caring via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_confusion: title: 😕 Confusion + description: "Posts classified with the emotion confusion via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_curiosity: title: ðŸĪ” Curiosity + description: "Posts classified with the emotion curiosity via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_desire: title: 😍 Desire + description: "Posts classified with the emotion desire via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_disappointment: title: 😞 Disappointment + description: "Posts classified with the emotion disappointment via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_disapproval: title: 👎 Disapproval + description: "Posts classified with the emotion disapproval via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_disgust: title: ðŸĪĒ Disgust + description: "Posts classified with the emotion disgust via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_embarrassment: title: ðŸ˜ģ Embarrassment + description: "Posts classified with the emotion embarrassment via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_excitement: title: ðŸĪŠ Excitement + description: "Posts classified with the emotion excitement via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_fear: title: ðŸ˜Ļ Fear + description: "Posts classified with the emotion fear via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_gratitude: title: 🙏 Gratitude + description: "Posts classified with the emotion gratitude via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_grief: title: ðŸ˜Ē Grief + description: "Posts classified with the emotion grief via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_joy: title: 😊 Joy + description: "Posts classified with the emotion joy via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_love: title: âĪïļ Love + description: "Posts classified with the emotion love via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_nervousness: title: 😰 Nervousness + description: "Posts classified with the emotion nervousness via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_neutral: title: 😐 Neutral + description: "Posts classified with the emotion neutral via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_optimism: title: 🌟 Optimism + description: "Posts classified with the emotion optimism via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_pride: title: ðŸĶ Pride + description: "Posts classified with the emotion pride via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_realization: title: ðŸ’Ą Realization + description: "Posts classified with the emotion realization via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_relief: title: 😌 Relief + description: "Posts classified with the emotion relief via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_remorse: title: 😔 Remorse + description: "Posts classified with the emotion remorse via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_sadness: title: 😭 Sadness + description: "Posts classified with the emotion sadness via AI, using the model 'SamLowe/roberta-base-go_emotions'." emotion_surprise: title: ðŸ˜ē Surprise + description: "Posts classified with the emotion surprise via AI, using the model 'SamLowe/roberta-base-go_emotions'." discourse_ai: ai_artifact: diff --git a/lib/sentiment/emotion_dashboard_report.rb b/lib/sentiment/emotion_dashboard_report.rb index 14763c53..11c190c5 100644 --- a/lib/sentiment/emotion_dashboard_report.rb +++ b/lib/sentiment/emotion_dashboard_report.rb @@ -6,15 +6,16 @@ module DiscourseAi def self.register!(plugin) Emotions::LIST.each do |emotion| plugin.add_report("emotion_#{emotion}") do |report| - query_results = DiscourseAi::Sentiment::EmotionDashboardReport.fetch_data - report.data = query_results.pop(30).map { |row| { x: row.day, y: row.send(emotion) } } - report.prev30Days = - query_results.take(30).reduce(0) { |sum, row| sum + row.send(emotion) }.to_i + query_results = DiscourseAi::Sentiment::EmotionDashboardReport.fetch_data(report) + report.data = query_results.map { |row| { x: row.day, y: row.send(emotion) } } + if report.facets.include?(:prev_period) && query_results.length > 30 + report.prev30Days = query_results[31..60].sum { |row| row.send(emotion) } + end end end - def self.fetch_data - DB.query(<<~SQL, end: Time.now.tomorrow.midnight, start: 60.days.ago.midnight) + def self.fetch_data(report) + DB.query(<<~SQL, end: report.end_date, start: report.start_date) SELECT posts.created_at::DATE AS day, #{