From 34c98de86438cd33353bb3b1823679dc0611baf3 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Fri, 30 May 2025 11:58:28 -0700 Subject: [PATCH] FIX: Exporting overall sentiment fails (#1388) ## :mag: Overview When exporting an Overall Sentiment report in the admin panel, the export fails with: ```ruby Job exception: no implicit conversion of Symbol into Integer ``` This was happening because we are passing a single _Hash_ to `report.data` however, exports expect `report.data` to be an _Array of Hashes_. This update fixes this issue by wrapping the data in an array. --- lib/sentiment/sentiment_dashboard_report.rb | 20 ++++++++++--------- .../lib/modules/sentiment/entry_point_spec.rb | 16 ++++++++++++++- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/sentiment/sentiment_dashboard_report.rb b/lib/sentiment/sentiment_dashboard_report.rb index 19d04eb0..124e14b8 100644 --- a/lib/sentiment/sentiment_dashboard_report.rb +++ b/lib/sentiment/sentiment_dashboard_report.rb @@ -42,15 +42,17 @@ module DiscourseAi return report if grouped_sentiments.empty? - 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, - } + 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 diff --git a/spec/lib/modules/sentiment/entry_point_spec.rb b/spec/lib/modules/sentiment/entry_point_spec.rb index 871ce01e..3a212063 100644 --- a/spec/lib/modules/sentiment/entry_point_spec.rb +++ b/spec/lib/modules/sentiment/entry_point_spec.rb @@ -55,9 +55,23 @@ RSpec.describe DiscourseAi::Sentiment::EntryPoint do sentiment_classification(pm, positive_classification) report = Report.find("overall_sentiment") - overall_sentiment = report.data[:data][0][:y].to_i + overall_sentiment = report.data[0][:data][0][:y].to_i expect(overall_sentiment).to eq(0) end + + it "exports the report without any errors" do + sentiment_classification(post_1, positive_classification) + sentiment_classification(post_2, negative_classification) + sentiment_classification(pm, positive_classification) + + exporter = Jobs::ExportCsvFile.new + exporter.entity = "report" + exporter.extra = HashWithIndifferentAccess.new(name: "overall_sentiment") + exported_csv = [] + exporter.report_export { |entry| exported_csv << entry } + expect(exported_csv[0]).to eq(["Day", "Overall sentiment (Positive - Negative)"]) + expect(exported_csv[1]).to eq([post_1.created_at.to_date.to_s, "0"]) + end end describe "post_emotion report" do