Skip to content

Commit bdfa207

Browse files
committed
opt: only re-train the model with new message rather than all messages
1 parent b2a4671 commit bdfa207

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

app/jobs/classifier_trainer_job.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ class ClassifierTrainerJob < ApplicationJob
22
# Job to train classifier asynchronously
33
queue_as :training
44

5-
def perform(group_id, group_name)
5+
def perform(trained_message)
66
Rails.logger.info "Retrain all the classifiers for public"
7-
GroupClassifierState.for_public.find_each do |classifier|
8-
SpamClassifierService.rebuild_for_group(classifier.group_id, classifier.group_name)
7+
if trained_message.user_name?
8+
GroupClassifierState.username.find_each do |classifier|
9+
spam_classifier = SpamClassifierService.new(classifier.group_id, classifier.group_name)
10+
spam_classifier.train(trained_message)
11+
end
12+
elsif trained_message.message_content?
13+
GroupClassifierState.for_group.find_each do |classifier|
14+
spam_classifier = SpamClassifierService.new(classifier.group_id, classifier.group_name)
15+
spam_classifier.train(trained_message)
16+
end
917
end
1018
end
1119
end

app/models/trained_message.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,7 @@ def should_ban_user
5858
def retrain_classifier
5959
return if untrained?
6060

61-
if user_name?
62-
# Target is user_name
63-
ClassifierTrainerJob.perform_later(GroupClassifierState::USER_NAME_CLASSIFIER_GROUP_ID, GroupClassifierState::USER_NAME_CLASSIFIER_GROUP_NAME)
64-
else
65-
# For efficiency, we could queue this as a background job
66-
ClassifierTrainerJob.perform_later(group_id, group_name)
67-
end
61+
ClassifierTrainerJob.perform_later(self)
6862
end
6963

7064
private

app/views/group_classifier_states/index.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@
6666
<% end %>
6767
<% end %>
6868
</th>
69+
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
70+
<%= link_to group_classifier_states_path(params.permit!.merge(sort: 'updated_at', direction: params[:sort] == 'updated_at' && params[:direction] == 'asc' ? 'desc' : 'asc')), class: "hover:text-gray-700 flex items-center space-x-1" do %>
71+
<span>Updated</span>
72+
<% if params[:sort] == 'updated_at' %>
73+
<span><%= params[:direction] == 'asc' ? '↑' : '↓' %></span>
74+
<% end %>
75+
<% end %>
76+
</th>
6977
<th scope="col" class="relative px-6 py-3">
7078
<span class="sr-only">Actions</span>
7179
</th>
@@ -106,6 +114,10 @@
106114
<%= time_ago_in_words(group_classifier_state.created_at) %> ago
107115
</td>
108116

117+
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
118+
<%= time_ago_in_words(group_classifier_state.updated_at) %> ago
119+
</td>
120+
109121
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
110122
<div class="flex space-x-2">
111123
<%= link_to "View", group_classifier_state_path(group_classifier_state, request.query_parameters), class: "text-indigo-600 hover:text-indigo-900" %>

0 commit comments

Comments
 (0)