From f51bfdc6e9c52c094da2d3a070f35e0b640b16d5 Mon Sep 17 00:00:00 2001 From: Nat Date: Tue, 21 Jan 2025 16:21:43 +0800 Subject: [PATCH 1/3] DEV: Expose AI spam scanning metrics --- lib/ai_moderation/spam_metric.rb | 29 +++++++++++++++++++++++++++++ plugin.rb | 3 +++ 2 files changed, 32 insertions(+) create mode 100644 lib/ai_moderation/spam_metric.rb diff --git a/lib/ai_moderation/spam_metric.rb b/lib/ai_moderation/spam_metric.rb new file mode 100644 index 000000000..a98bf3eb2 --- /dev/null +++ b/lib/ai_moderation/spam_metric.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module DiscourseAi + module AiModeration + class SpamMetric + def self.update(new_status, reviewable) + ai_spam_log = AiSpamLog.find_by(reviewable:) + return if ai_spam_log.nil? + + increment("scanned") + increment("is_spam") if new_status == :approved && ai_spam_log.is_spam + increment("false_positive") if new_status == :rejected && ai_spam_log.is_spam + increment("false_negative") if new_status == :rejected && !ai_spam_log.is_spam + end + + private + + def self.increment(type, value = 1) + metric = DiscoursePrometheus::InternalMetric::Custom.new + metric.name = "discourse_ai_spam_detection" + metric.type = "Counter" + metric.description = "AI spam scanning statistics" + metric.labels = { db: RailsMultisite::ConnectionManagement.current_db, type: } + metric.value = value + $prometheus_client.send_json(metric.to_h) + end + end + end +end diff --git a/plugin.rb b/plugin.rb index e02a102f9..ea30c8358 100644 --- a/plugin.rb +++ b/plugin.rb @@ -83,6 +83,9 @@ def self.public_asset_path(name) on(:reviewable_transitioned_to) do |new_status, reviewable| ModelAccuracy.adjust_model_accuracy(new_status, reviewable) + if DiscourseAi::AiModeration::SpamScanner.enabled? + DiscourseAi::AiModeration::SpamMetric.update(new_status, reviewable) + end end if Rails.env.test? From 52668a2db49b3f86580b301e0d1c97478552133b Mon Sep 17 00:00:00 2001 From: Natalie Tay Date: Mon, 27 Jan 2025 10:44:26 +0800 Subject: [PATCH 2/3] Update lib/ai_moderation/spam_metric.rb Co-authored-by: Roman Rizzi --- lib/ai_moderation/spam_metric.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ai_moderation/spam_metric.rb b/lib/ai_moderation/spam_metric.rb index a98bf3eb2..dbceacd15 100644 --- a/lib/ai_moderation/spam_metric.rb +++ b/lib/ai_moderation/spam_metric.rb @@ -16,7 +16,7 @@ def self.update(new_status, reviewable) private def self.increment(type, value = 1) - metric = DiscoursePrometheus::InternalMetric::Custom.new + metric = ::DiscoursePrometheus::InternalMetric::Custom.new metric.name = "discourse_ai_spam_detection" metric.type = "Counter" metric.description = "AI spam scanning statistics" From 8dd7556bda0ba8d61c2306b2c57b4284201ed3b3 Mon Sep 17 00:00:00 2001 From: Nat Date: Mon, 27 Jan 2025 11:36:47 +0800 Subject: [PATCH 3/3] Add prometheus as test dependency --- .rubocop.yml | 3 +++ about.json | 7 +++++++ 2 files changed, 10 insertions(+) create mode 100644 about.json diff --git a/.rubocop.yml b/.rubocop.yml index 09a7846b2..9a12b2d91 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,3 +2,6 @@ inherit_gem: rubocop-discourse: stree-compat.yml RSpec/NamedSubject: Enabled: false + +Style/GlobalVars: + AllowedVariables: [$prometheus_client] diff --git a/about.json b/about.json new file mode 100644 index 000000000..48d9cee04 --- /dev/null +++ b/about.json @@ -0,0 +1,7 @@ +{ + "tests": { + "requiredPlugins": [ + "https://github.com/discourse/discourse-prometheus" + ] + } +}