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

Commit 576affc

Browse files
committed
DEV: Add size filter
1 parent 5ef594a commit 576affc

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ export default class AdminReportSentimentAnalysis extends Component {
2121
}
2222

2323
get transformedData() {
24-
console.log(this.args.model);
2524
return this.args.model.data.map((data) => {
26-
console.log("called", data);
2725
return {
2826
title: data.category_name || data.tag_name,
2927
scores: [
@@ -32,6 +30,7 @@ export default class AdminReportSentimentAnalysis extends Component {
3230
data.negative_count,
3331
],
3432
posts: data.posts,
33+
total_score: data.total_count,
3534
};
3635
});
3736
}
@@ -73,19 +72,19 @@ export default class AdminReportSentimentAnalysis extends Component {
7372
}
7473

7574
doughnutTitle(data) {
76-
if (data.posts?.length > 0) {
77-
return `${data.title} (${data.posts.length})`;
75+
if (data?.total_score) {
76+
return `${data.title} (${data.total_score})`;
7877
} else {
7978
return data.title;
8079
}
8180
}
8281

8382
<template>
84-
{{! TODO add more details about posts on click + tag data }}
8583
<div class="admin-report-sentiment-analysis">
8684
{{#each this.transformedData as |data|}}
8785
<div
8886
class="admin-report-sentiment-analysis__chart-wrapper"
87+
role="button"
8988
{{on "click" (fn this.showDetails data)}}
9089
{{closeOnClickOutside
9190
(fn (mut this.selectedChart) null)

lib/sentiment/sentiment_analysis_report.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ def self.register!(plugin)
1717
allow_any: false,
1818
auto_insert_none_item: false,
1919
)
20-
20+
size_filter = report.filters.dig(:sort_by) || :size
2121
report.add_filter(
2222
"sort_by",
2323
type: "list",
24-
default: "size",
25-
choices: [{ id: "size", name: "Size" }, { id: "name", name: "Name" }],
24+
default: size_filter,
25+
choices: [{ id: "size", name: "Size" }, { id: "alphabetical", name: "Alphabetical" }],
26+
allow_any: false,
27+
auto_insert_none_item: false,
2628
)
2729

2830
sentiment_data = DiscourseAi::Sentiment::SentimentAnalysisReport.fetch_data(report)
@@ -41,6 +43,7 @@ def self.register!(plugin)
4143

4244
def self.fetch_data(report)
4345
grouping = report.filters.dig(:group_by).to_sym
46+
sorting = report.filters.dig(:sort_by).to_sym
4447
threshold = 0.6
4548

4649
sentiment_count_sql = Proc.new { |sentiment| <<~SQL }
@@ -78,6 +81,16 @@ def self.fetch_data(report)
7881
raise Discourse::InvalidParameters
7982
end
8083

84+
order_by_clause =
85+
case sorting
86+
when :size
87+
"ORDER BY total_count DESC"
88+
when :alphabetical
89+
"ORDER BY 1 ASC"
90+
else
91+
raise Discourse::InvalidParameters
92+
end
93+
8194
grouped_sentiments =
8295
DB.query(
8396
<<~SQL,
@@ -97,7 +110,7 @@ def self.fetch_data(report)
97110
cr.model_used = 'cardiffnlp/twitter-roberta-base-sentiment-latest' AND
98111
(p.created_at > :report_start AND p.created_at < :report_end)
99112
GROUP BY 1
100-
ORDER BY 1 ASC
113+
#{order_by_clause}
101114
SQL
102115
report_start: report.start_date,
103116
report_end: report.end_date,

0 commit comments

Comments
 (0)