mirror of
https://github.com/donnemartin/system-design-primer.git
synced 2025-09-17 09:30:39 +03:00
revert to origin
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin) . Source and license info is on [GitHub](https://github.com/donnemartin/system-design-primer) ."
|
||||
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/system-design-primer)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -67,118 +67,118 @@
|
||||
"from enum import Enum\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Rank(Enum) :\n",
|
||||
"class Rank(Enum):\n",
|
||||
"\n",
|
||||
" OPERATOR = 0\n",
|
||||
" SUPERVISOR = 1\n",
|
||||
" DIRECTOR = 2\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Employee(metaclass=ABCMeta) :\n",
|
||||
"class Employee(metaclass=ABCMeta):\n",
|
||||
"\n",
|
||||
" def __init__(self, employee_id, name, rank, call_center) :\n",
|
||||
" def __init__(self, employee_id, name, rank, call_center):\n",
|
||||
" self.employee_id = employee_id\n",
|
||||
" self.name = name\n",
|
||||
" self.rank = rank\n",
|
||||
" self.call = None\n",
|
||||
" self.call_center = call_center\n",
|
||||
"\n",
|
||||
" def take_call(self, call) :\n",
|
||||
" def take_call(self, call):\n",
|
||||
" \"\"\"Assume the employee will always successfully take the call.\"\"\"\n",
|
||||
" self.call = call\n",
|
||||
" self.call.employee = self\n",
|
||||
" self.call.state = CallState.IN_PROGRESS\n",
|
||||
"\n",
|
||||
" def complete_call(self) :\n",
|
||||
" def complete_call(self):\n",
|
||||
" self.call.state = CallState.COMPLETE\n",
|
||||
" self.call_center.notify_call_completed(self.call) \n",
|
||||
" self.call_center.notify_call_completed(self.call)\n",
|
||||
"\n",
|
||||
" @abstractmethod\n",
|
||||
" def escalate_call(self) :\n",
|
||||
" def escalate_call(self):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def _escalate_call(self) :\n",
|
||||
" def _escalate_call(self):\n",
|
||||
" self.call.state = CallState.READY\n",
|
||||
" call = self.call\n",
|
||||
" self.call = None\n",
|
||||
" self.call_center.notify_call_escalated(call) \n",
|
||||
" self.call_center.notify_call_escalated(call)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Operator(Employee) :\n",
|
||||
"class Operator(Employee):\n",
|
||||
"\n",
|
||||
" def __init__(self, employee_id, name) :\n",
|
||||
" super(Operator, self) .__init__(employee_id, name, Rank.OPERATOR) \n",
|
||||
" def __init__(self, employee_id, name):\n",
|
||||
" super(Operator, self).__init__(employee_id, name, Rank.OPERATOR)\n",
|
||||
"\n",
|
||||
" def escalate_call(self) :\n",
|
||||
" def escalate_call(self):\n",
|
||||
" self.call.level = Rank.SUPERVISOR\n",
|
||||
" self._escalate_call() \n",
|
||||
" self._escalate_call()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Supervisor(Employee) :\n",
|
||||
"class Supervisor(Employee):\n",
|
||||
"\n",
|
||||
" def __init__(self, employee_id, name) :\n",
|
||||
" super(Operator, self) .__init__(employee_id, name, Rank.SUPERVISOR) \n",
|
||||
" def __init__(self, employee_id, name):\n",
|
||||
" super(Operator, self).__init__(employee_id, name, Rank.SUPERVISOR)\n",
|
||||
"\n",
|
||||
" def escalate_call(self) :\n",
|
||||
" def escalate_call(self):\n",
|
||||
" self.call.level = Rank.DIRECTOR\n",
|
||||
" self._escalate_call() \n",
|
||||
" self._escalate_call()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Director(Employee) :\n",
|
||||
"class Director(Employee):\n",
|
||||
"\n",
|
||||
" def __init__(self, employee_id, name) :\n",
|
||||
" super(Operator, self) .__init__(employee_id, name, Rank.DIRECTOR) \n",
|
||||
" def __init__(self, employee_id, name):\n",
|
||||
" super(Operator, self).__init__(employee_id, name, Rank.DIRECTOR)\n",
|
||||
"\n",
|
||||
" def escalate_call(self) :\n",
|
||||
" raise NotImplemented('Directors must be able to handle any call') \n",
|
||||
" def escalate_call(self):\n",
|
||||
" raise NotImplemented('Directors must be able to handle any call')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class CallState(Enum) :\n",
|
||||
"class CallState(Enum):\n",
|
||||
"\n",
|
||||
" READY = 0\n",
|
||||
" IN_PROGRESS = 1\n",
|
||||
" COMPLETE = 2\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Call(object) :\n",
|
||||
"class Call(object):\n",
|
||||
"\n",
|
||||
" def __init__(self, rank) :\n",
|
||||
" def __init__(self, rank):\n",
|
||||
" self.state = CallState.READY\n",
|
||||
" self.rank = rank\n",
|
||||
" self.employee = None\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class CallCenter(object) :\n",
|
||||
"class CallCenter(object):\n",
|
||||
"\n",
|
||||
" def __init__(self, operators, supervisors, directors) :\n",
|
||||
" def __init__(self, operators, supervisors, directors):\n",
|
||||
" self.operators = operators\n",
|
||||
" self.supervisors = supervisors\n",
|
||||
" self.directors = directors\n",
|
||||
" self.queued_calls = deque() \n",
|
||||
" self.queued_calls = deque()\n",
|
||||
"\n",
|
||||
" def dispatch_call(self, call) :\n",
|
||||
" if call.rank not in (Rank.OPERATOR, Rank.SUPERVISOR, Rank.DIRECTOR) :\n",
|
||||
" def dispatch_call(self, call):\n",
|
||||
" if call.rank not in (Rank.OPERATOR, Rank.SUPERVISOR, Rank.DIRECTOR):\n",
|
||||
" raise ValueError('Invalid call rank: {}'.format(call.rank))\n",
|
||||
" employee = None\n",
|
||||
" if call.rank == Rank.OPERATOR:\n",
|
||||
" employee = self._dispatch_call(call, self.operators) \n",
|
||||
" employee = self._dispatch_call(call, self.operators)\n",
|
||||
" if call.rank == Rank.SUPERVISOR or employee is None:\n",
|
||||
" employee = self._dispatch_call(call, self.supervisors) \n",
|
||||
" employee = self._dispatch_call(call, self.supervisors)\n",
|
||||
" if call.rank == Rank.DIRECTOR or employee is None:\n",
|
||||
" employee = self._dispatch_call(call, self.directors) \n",
|
||||
" employee = self._dispatch_call(call, self.directors)\n",
|
||||
" if employee is None:\n",
|
||||
" self.queued_calls.append(call) \n",
|
||||
" self.queued_calls.append(call)\n",
|
||||
"\n",
|
||||
" def _dispatch_call(self, call, employees) :\n",
|
||||
" def _dispatch_call(self, call, employees):\n",
|
||||
" for employee in employees:\n",
|
||||
" if employee.call is None:\n",
|
||||
" employee.take_call(call) \n",
|
||||
" employee.take_call(call)\n",
|
||||
" return employee\n",
|
||||
" return None\n",
|
||||
"\n",
|
||||
" def notify_call_escalated(self, call) : # ...\n",
|
||||
" def notify_call_completed(self, call) : # ...\n",
|
||||
" def dispatch_queued_call_to_newly_freed_employee(self, call, employee) : # ..."
|
||||
" def notify_call_escalated(self, call): # ...\n",
|
||||
" def notify_call_completed(self, call): # ...\n",
|
||||
" def dispatch_queued_call_to_newly_freed_employee(self, call, employee): # ..."
|
||||
]
|
||||
}
|
||||
],
|
||||
|
@@ -3,120 +3,120 @@ from collections import deque
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class Rank(Enum) :
|
||||
class Rank(Enum):
|
||||
|
||||
OPERATOR = 0
|
||||
SUPERVISOR = 1
|
||||
DIRECTOR = 2
|
||||
|
||||
|
||||
class Employee(metaclass=ABCMeta) :
|
||||
class Employee(metaclass=ABCMeta):
|
||||
|
||||
def __init__(self, employee_id, name, rank, call_center) :
|
||||
def __init__(self, employee_id, name, rank, call_center):
|
||||
self.employee_id = employee_id
|
||||
self.name = name
|
||||
self.rank = rank
|
||||
self.call = None
|
||||
self.call_center = call_center
|
||||
|
||||
def take_call(self, call) :
|
||||
def take_call(self, call):
|
||||
"""Assume the employee will always successfully take the call."""
|
||||
self.call = call
|
||||
self.call.employee = self
|
||||
self.call.state = CallState.IN_PROGRESS
|
||||
|
||||
def complete_call(self) :
|
||||
def complete_call(self):
|
||||
self.call.state = CallState.COMPLETE
|
||||
self.call_center.notify_call_completed(self.call)
|
||||
self.call_center.notify_call_completed(self.call)
|
||||
|
||||
@abstractmethod
|
||||
def escalate_call(self) :
|
||||
def escalate_call(self):
|
||||
pass
|
||||
|
||||
def _escalate_call(self) :
|
||||
def _escalate_call(self):
|
||||
self.call.state = CallState.READY
|
||||
call = self.call
|
||||
self.call = None
|
||||
self.call_center.notify_call_escalated(call)
|
||||
self.call_center.notify_call_escalated(call)
|
||||
|
||||
|
||||
class Operator(Employee) :
|
||||
class Operator(Employee):
|
||||
|
||||
def __init__(self, employee_id, name) :
|
||||
super(Operator, self) .__init__(employee_id, name, Rank.OPERATOR)
|
||||
def __init__(self, employee_id, name):
|
||||
super(Operator, self).__init__(employee_id, name, Rank.OPERATOR)
|
||||
|
||||
def escalate_call(self) :
|
||||
def escalate_call(self):
|
||||
self.call.level = Rank.SUPERVISOR
|
||||
self._escalate_call()
|
||||
self._escalate_call()
|
||||
|
||||
|
||||
class Supervisor(Employee) :
|
||||
class Supervisor(Employee):
|
||||
|
||||
def __init__(self, employee_id, name) :
|
||||
super(Operator, self) .__init__(employee_id, name, Rank.SUPERVISOR)
|
||||
def __init__(self, employee_id, name):
|
||||
super(Operator, self).__init__(employee_id, name, Rank.SUPERVISOR)
|
||||
|
||||
def escalate_call(self) :
|
||||
def escalate_call(self):
|
||||
self.call.level = Rank.DIRECTOR
|
||||
self._escalate_call()
|
||||
self._escalate_call()
|
||||
|
||||
|
||||
class Director(Employee) :
|
||||
class Director(Employee):
|
||||
|
||||
def __init__(self, employee_id, name) :
|
||||
super(Operator, self) .__init__(employee_id, name, Rank.DIRECTOR)
|
||||
def __init__(self, employee_id, name):
|
||||
super(Operator, self).__init__(employee_id, name, Rank.DIRECTOR)
|
||||
|
||||
def escalate_call(self) :
|
||||
raise NotImplementedError('Directors must be able to handle any call')
|
||||
def escalate_call(self):
|
||||
raise NotImplementedError('Directors must be able to handle any call')
|
||||
|
||||
|
||||
class CallState(Enum) :
|
||||
class CallState(Enum):
|
||||
|
||||
READY = 0
|
||||
IN_PROGRESS = 1
|
||||
COMPLETE = 2
|
||||
|
||||
|
||||
class Call(object) :
|
||||
class Call(object):
|
||||
|
||||
def __init__(self, rank) :
|
||||
def __init__(self, rank):
|
||||
self.state = CallState.READY
|
||||
self.rank = rank
|
||||
self.employee = None
|
||||
|
||||
|
||||
class CallCenter(object) :
|
||||
class CallCenter(object):
|
||||
|
||||
def __init__(self, operators, supervisors, directors) :
|
||||
def __init__(self, operators, supervisors, directors):
|
||||
self.operators = operators
|
||||
self.supervisors = supervisors
|
||||
self.directors = directors
|
||||
self.queued_calls = deque()
|
||||
self.queued_calls = deque()
|
||||
|
||||
def dispatch_call(self, call) :
|
||||
if call.rank not in (Rank.OPERATOR, Rank.SUPERVISOR, Rank.DIRECTOR) :
|
||||
def dispatch_call(self, call):
|
||||
if call.rank not in (Rank.OPERATOR, Rank.SUPERVISOR, Rank.DIRECTOR):
|
||||
raise ValueError('Invalid call rank: {}'.format(call.rank))
|
||||
employee = None
|
||||
if call.rank == Rank.OPERATOR:
|
||||
employee = self._dispatch_call(call, self.operators)
|
||||
employee = self._dispatch_call(call, self.operators)
|
||||
if call.rank == Rank.SUPERVISOR or employee is None:
|
||||
employee = self._dispatch_call(call, self.supervisors)
|
||||
employee = self._dispatch_call(call, self.supervisors)
|
||||
if call.rank == Rank.DIRECTOR or employee is None:
|
||||
employee = self._dispatch_call(call, self.directors)
|
||||
employee = self._dispatch_call(call, self.directors)
|
||||
if employee is None:
|
||||
self.queued_calls.append(call)
|
||||
self.queued_calls.append(call)
|
||||
|
||||
def _dispatch_call(self, call, employees) :
|
||||
def _dispatch_call(self, call, employees):
|
||||
for employee in employees:
|
||||
if employee.call is None:
|
||||
employee.take_call(call)
|
||||
employee.take_call(call)
|
||||
return employee
|
||||
return None
|
||||
|
||||
def notify_call_escalated(self, call) :
|
||||
def notify_call_escalated(self, call):
|
||||
pass
|
||||
|
||||
def notify_call_completed(self, call) :
|
||||
def notify_call_completed(self, call):
|
||||
pass
|
||||
|
||||
def dispatch_queued_call_to_newly_freed_employee(self, call, employee) :
|
||||
def dispatch_queued_call_to_newly_freed_employee(self, call, employee):
|
||||
pass
|
||||
|
Reference in New Issue
Block a user