Skip to content

Commit f57e48b

Browse files
committed
Optimize bulk_update to support re-train classifier on demand
1 parent 7b00016 commit f57e48b

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

app/controllers/trained_messages_controller.rb

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,34 @@ def bulk_update
7171

7272
messages = TrainedMessage.where(id: message_ids)
7373
update_count = messages.count
74-
case params[:commit]
75-
when "Mark as Ham"
76-
messages.update_all(message_type: :ham)
77-
flash[:notice] = "Successfully marked #{update_count} messages as Ham"
78-
when "Mark as Spam"
79-
messages.update_all(message_type: :spam)
80-
flash[:notice] = "Successfully marked #{update_count} messages as Spam"
74+
75+
new_message_type_symbol = case params[:commit]
76+
when "Mark as Ham" then :ham
77+
when "Mark as Spam" then :spam
8178
else
82-
flash[:alert] = "Invalid action."
79+
flash[:alert] = "Invalid action."
80+
redirect_to trained_messages_path(params.except(:commit, :trained_message_ids, :authenticity_token, :controller, :action).to_unsafe_h)
81+
return
82+
end
83+
84+
messages.update_all(message_type: new_message_type_symbol)
85+
86+
messages.each do |message|
87+
if message.spam? || message.ham?
88+
BatchProcessor.add_to_batch(
89+
"classifier_training_batch_key",
90+
"ClassifierTrainerJob",
91+
message,
92+
{},
93+
batch_size: 100,
94+
batch_window: 5.minutes
95+
)
96+
end
8397
end
8498

85-
redirect_to trained_messages_path(
86-
params.except(:commit, :trained_message_ids, :authenticity_token, :controller, :action).to_unsafe_h
87-
)
99+
message_type_name = TrainedMessage.message_types[new_message_type_symbol]
100+
flash[:notice] = "Successfully marked #{update_count} messages as #{message_type_name}"
101+
redirect_to trained_messages_path(params.except(:commit, :trained_message_ids, :authenticity_token, :controller, :action).to_unsafe_h)
88102
end
89103

90104
# POST /trained_messages or /trained_messages.json

0 commit comments

Comments
 (0)