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.
This commit is contained in:
Keegan George 2025-05-30 11:58:28 -07:00 committed by GitHub
parent 38f7e9c2c4
commit 34c98de864
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 10 deletions

View File

@ -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

View File

@ -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