feat: add assignment service
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
class AutoAssignment::AssignmentService
|
||||
pattr_initialize [:inbox!]
|
||||
|
||||
def perform_for_conversation(conversation)
|
||||
return false unless assignable?(conversation)
|
||||
|
||||
agent = find_available_agent
|
||||
return false unless agent
|
||||
|
||||
assign_conversation(conversation, agent)
|
||||
end
|
||||
|
||||
def perform_bulk_assignment(limit: 100)
|
||||
return 0 unless inbox.enable_auto_assignment?
|
||||
|
||||
assigned_count = 0
|
||||
|
||||
unassigned_conversations(limit).find_each do |conversation|
|
||||
assigned_count += 1 if perform_for_conversation(conversation)
|
||||
end
|
||||
|
||||
assigned_count
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def assignable?(conversation)
|
||||
inbox.enable_auto_assignment? &&
|
||||
conversation.status == 'open' &&
|
||||
conversation.assignee_id.nil?
|
||||
end
|
||||
|
||||
def unassigned_conversations(limit)
|
||||
scope = inbox.conversations.unassigned.open
|
||||
|
||||
# Apply conversation priority from config
|
||||
scope = apply_conversation_priority(scope)
|
||||
scope.limit(limit)
|
||||
end
|
||||
|
||||
def apply_conversation_priority(scope)
|
||||
case assignment_config['conversation_priority']
|
||||
when 'longest_waiting'
|
||||
scope.order(last_activity_at: :asc, created_at: :asc)
|
||||
else
|
||||
scope.order(created_at: :asc)
|
||||
end
|
||||
end
|
||||
|
||||
def find_available_agent
|
||||
agents = filter_agents_by_rate_limit(inbox.available_agents)
|
||||
return nil if agents.empty?
|
||||
|
||||
round_robin_selector.select_agent(agents)
|
||||
end
|
||||
|
||||
def filter_agents_by_rate_limit(agents)
|
||||
agents.select do |agent_member|
|
||||
rate_limiter = build_rate_limiter(agent_member.user)
|
||||
rate_limiter.within_limit?
|
||||
end
|
||||
end
|
||||
|
||||
def assign_conversation(conversation, agent)
|
||||
conversation.update!(assignee: agent)
|
||||
|
||||
rate_limiter = build_rate_limiter(agent)
|
||||
rate_limiter.track_assignment(conversation)
|
||||
|
||||
dispatch_assignment_event(conversation, agent)
|
||||
true
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
Rails.logger.error "AutoAssignment failed for conversation #{conversation.id}: #{e.message}"
|
||||
false
|
||||
end
|
||||
|
||||
def dispatch_assignment_event(conversation, agent)
|
||||
Rails.configuration.dispatcher.dispatch(
|
||||
Events::Types::ASSIGNEE_CHANGED,
|
||||
Time.zone.now,
|
||||
conversation: conversation,
|
||||
user: agent
|
||||
)
|
||||
end
|
||||
|
||||
def build_rate_limiter(agent)
|
||||
AutoAssignment::RateLimiter.new(inbox: inbox, agent: agent)
|
||||
end
|
||||
|
||||
def round_robin_selector
|
||||
@round_robin_selector ||= AutoAssignment::RoundRobinSelector.new(inbox: inbox)
|
||||
end
|
||||
|
||||
def assignment_config
|
||||
@assignment_config ||= inbox.auto_assignment_config || {}
|
||||
end
|
||||
end
|
||||
|
||||
AutoAssignment::AssignmentService.prepend_mod_with('AutoAssignment::AssignmentService')
|
||||
@@ -0,0 +1,49 @@
|
||||
class AutoAssignment::RateLimiter
|
||||
pattr_initialize [:inbox!, :agent!]
|
||||
|
||||
def within_limit?
|
||||
return true unless enabled?
|
||||
|
||||
current_count < limit
|
||||
end
|
||||
|
||||
def track_assignment(conversation)
|
||||
return unless enabled?
|
||||
|
||||
assignment_key = build_assignment_key(conversation.id)
|
||||
Redis::Alfred.set(assignment_key, conversation.id.to_s, ex: window)
|
||||
end
|
||||
|
||||
def current_count
|
||||
return 0 unless enabled?
|
||||
|
||||
pattern = assignment_key_pattern
|
||||
Redis::Alfred.keys_count(pattern)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def enabled?
|
||||
limit.present? && limit.positive?
|
||||
end
|
||||
|
||||
def limit
|
||||
config['fair_distribution_limit']&.to_i
|
||||
end
|
||||
|
||||
def window
|
||||
config['fair_distribution_window']&.to_i || 3600
|
||||
end
|
||||
|
||||
def config
|
||||
@config ||= inbox.auto_assignment_config || {}
|
||||
end
|
||||
|
||||
def assignment_key_pattern
|
||||
"assignment:#{inbox.id}:agent:#{agent.id}:*"
|
||||
end
|
||||
|
||||
def build_assignment_key(conversation_id)
|
||||
"assignment:#{inbox.id}:agent:#{agent.id}:conversation:#{conversation_id}"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
class AutoAssignment::RoundRobinSelector
|
||||
pattr_initialize [:inbox!]
|
||||
|
||||
def select_agent(available_agents)
|
||||
return nil if available_agents.empty?
|
||||
|
||||
agent_user_ids = available_agents.map(&:user_id).map(&:to_s)
|
||||
round_robin_service.available_agent(allowed_agent_ids: agent_user_ids)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def round_robin_service
|
||||
@round_robin_service ||= AutoAssignment::InboxRoundRobinService.new(inbox: inbox)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user