discourse/plugins/discourse-ai/lib/ai_moderation/spam_metric.rb
Jarek Radosz bf360a0539
DEV: Stop prometheus/ai spam in plugin backend specs (#39245)
No more of these

```
E, [2026-04-13T13:40:33.862133 #2069] ERROR -- : Prometheus Exporter, failed to send message Connection refused - connect(2) for "localhost" port 9405
```
2026-04-14 10:24:46 +02:00

30 lines
1.1 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscourseAi
module AiModeration
class SpamMetric
def self.update(new_status, reviewable)
return if !defined?(::DiscoursePrometheus)
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) unless Rails.env.test? # rubocop:disable Style/GlobalVars
end
end
end
end