Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 34c98de

Browse files
authored
FIX: Exporting overall sentiment fails (#1388)
## 🔍 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.
1 parent 38f7e9c commit 34c98de

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

lib/sentiment/sentiment_dashboard_report.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ def self.register!(plugin)
4242

4343
return report if grouped_sentiments.empty?
4444

45-
report.data = {
46-
req: "overall_sentiment",
47-
color: report.colors[:lime],
48-
label: I18n.t("discourse_ai.sentiment.reports.overall_sentiment"),
49-
data:
50-
grouped_sentiments.map do |gs|
51-
{ x: gs.posted_at, y: gs.public_send("sentiment_count") }
52-
end,
53-
}
45+
report.data = [
46+
{
47+
req: "overall_sentiment",
48+
color: report.colors[:lime],
49+
label: I18n.t("discourse_ai.sentiment.reports.overall_sentiment"),
50+
data:
51+
grouped_sentiments.map do |gs|
52+
{ x: gs.posted_at, y: gs.public_send("sentiment_count") }
53+
end,
54+
},
55+
]
5456
end
5557
end
5658
end

spec/lib/modules/sentiment/entry_point_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,23 @@ def sentiment_classification(post, classification)
5555
sentiment_classification(pm, positive_classification)
5656

5757
report = Report.find("overall_sentiment")
58-
overall_sentiment = report.data[:data][0][:y].to_i
58+
overall_sentiment = report.data[0][:data][0][:y].to_i
5959
expect(overall_sentiment).to eq(0)
6060
end
61+
62+
it "exports the report without any errors" do
63+
sentiment_classification(post_1, positive_classification)
64+
sentiment_classification(post_2, negative_classification)
65+
sentiment_classification(pm, positive_classification)
66+
67+
exporter = Jobs::ExportCsvFile.new
68+
exporter.entity = "report"
69+
exporter.extra = HashWithIndifferentAccess.new(name: "overall_sentiment")
70+
exported_csv = []
71+
exporter.report_export { |entry| exported_csv << entry }
72+
expect(exported_csv[0]).to eq(["Day", "Overall sentiment (Positive - Negative)"])
73+
expect(exported_csv[1]).to eq([post_1.created_at.to_date.to_s, "0"])
74+
end
6175
end
6276

6377
describe "post_emotion report" do

0 commit comments

Comments
 (0)