discourse/plugins/discourse-ai/app/models/model_accuracy.rb
David Taylor 3332e9f4c3
DEV: Always pass --force to annotaterb and reorder annotations (#39977)
`.annotaterb.yml` has carried `classified_sort: true` since the project
switched from `annotate` to `annotaterb` (commit 0eab7daea4, July 2025),
but annotaterb's default behaviour is to compare the existing schema
block against what it would generate and skip the rewrite when the
column list matches — even when the *ordering* of those columns differs.
The result is that models which haven't had a schema change since the
config landed never get reordered, and `classified_sort` drift
accumulates indefinitely.

`--force` makes annotaterb always rewrite, so a single `bin/rake
annotate:clean` run brings every model into the canonical format and
keeps them there. Every schema block is now grouped primary-key →
regular columns → timestamps → foreign keys (alphabetical within each
group). Pure annotation comment change — no code modifications.

Also cleans up the rake task to avoid string interpolation for `system`
calls.
2026-05-13 14:12:48 +01:00

52 lines
1.3 KiB
Ruby
Vendored

# frozen_string_literal: true
class ModelAccuracy < ActiveRecord::Base
def self.adjust_model_accuracy(new_status, reviewable)
return if %i[approved rejected].exclude?(new_status)
return if [ReviewableAiPost, ReviewableAiChatMessage].exclude?(reviewable.class)
verdicts = reviewable.payload.to_h["verdicts"] || {}
verdicts.each do |model_name, verdict|
accuracy_model = find_by(model: model_name)
attribute =
if verdict
new_status == :approved ? :flags_agreed : :flags_disagreed
else
new_status == :rejected ? :flags_agreed : :flags_disagreed
end
accuracy_model.increment!(attribute)
end
end
def calculate_accuracy
return 0 if total_flags.zero?
(flags_agreed * 100) / total_flags
end
private
def total_flags
flags_agreed + flags_disagreed
end
end
# == Schema Information
#
# Table name: model_accuracies
#
# id :bigint not null, primary key
# classification_type :string not null
# flags_agreed :integer default(0), not null
# flags_disagreed :integer default(0), not null
# model :string not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_model_accuracies_on_model (model) UNIQUE
#