Commit cd7ac6d2 authored by Girish Raman's avatar Girish Raman Committed by Donne Martin
Browse files

Fix broken URL (#194)

parent 9c4c603b
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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-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)."
] ]
}, },
{ {
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
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-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).
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Design a call center # Design a call center
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Constraints and assumptions ## Constraints and assumptions
* What levels of employees are in the call center? * What levels of employees are in the call center?
* Operator, supervisor, director * Operator, supervisor, director
* Can we assume operators always get the initial calls? * Can we assume operators always get the initial calls?
* Yes * Yes
* If there is no free operators or the operator can't handle the call, does the call go to the supervisors? * If there is no free operators or the operator can't handle the call, does the call go to the supervisors?
* Yes * Yes
* If there is no free supervisors or the supervisor can't handle the call, does the call go to the directors? * If there is no free supervisors or the supervisor can't handle the call, does the call go to the directors?
* Yes * Yes
* Can we assume the directors can handle all calls? * Can we assume the directors can handle all calls?
* Yes * Yes
* What happens if nobody can answer the call? * What happens if nobody can answer the call?
* It gets queued * It gets queued
* Do we need to handle 'VIP' calls where we put someone to the front of the line? * Do we need to handle 'VIP' calls where we put someone to the front of the line?
* No * No
* Can we assume inputs are valid or do we have to validate them? * Can we assume inputs are valid or do we have to validate them?
* Assume they're valid * Assume they're valid
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Solution ## Solution
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%%writefile call_center.py %%writefile call_center.py
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from collections import deque from collections import deque
from enum import Enum from enum import Enum
class Rank(Enum): class Rank(Enum):
OPERATOR = 0 OPERATOR = 0
SUPERVISOR = 1 SUPERVISOR = 1
DIRECTOR = 2 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.employee_id = employee_id
self.name = name self.name = name
self.rank = rank self.rank = rank
self.call = None self.call = None
self.call_center = call_center self.call_center = call_center
def take_call(self, call): def take_call(self, call):
"""Assume the employee will always successfully take the call.""" """Assume the employee will always successfully take the call."""
self.call = call self.call = call
self.call.employee = self self.call.employee = self
self.call.state = CallState.IN_PROGRESS self.call.state = CallState.IN_PROGRESS
def complete_call(self): def complete_call(self):
self.call.state = CallState.COMPLETE self.call.state = CallState.COMPLETE
self.call_center.notify_call_completed(self.call) self.call_center.notify_call_completed(self.call)
@abstractmethod @abstractmethod
def escalate_call(self): def escalate_call(self):
pass pass
def _escalate_call(self): def _escalate_call(self):
self.call.state = CallState.READY self.call.state = CallState.READY
call = self.call call = self.call
self.call = None 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): def __init__(self, employee_id, name):
super(Operator, self).__init__(employee_id, name, Rank.OPERATOR) super(Operator, self).__init__(employee_id, name, Rank.OPERATOR)
def escalate_call(self): def escalate_call(self):
self.call.level = Rank.SUPERVISOR self.call.level = Rank.SUPERVISOR
self._escalate_call() self._escalate_call()
class Supervisor(Employee): class Supervisor(Employee):
def __init__(self, employee_id, name): def __init__(self, employee_id, name):
super(Operator, self).__init__(employee_id, name, Rank.SUPERVISOR) super(Operator, self).__init__(employee_id, name, Rank.SUPERVISOR)
def escalate_call(self): def escalate_call(self):
self.call.level = Rank.DIRECTOR self.call.level = Rank.DIRECTOR
self._escalate_call() self._escalate_call()
class Director(Employee): class Director(Employee):
def __init__(self, employee_id, name): def __init__(self, employee_id, name):
super(Operator, self).__init__(employee_id, name, Rank.DIRECTOR) super(Operator, self).__init__(employee_id, name, Rank.DIRECTOR)
def escalate_call(self): def escalate_call(self):
raise NotImplemented('Directors must be able to handle any call') raise NotImplemented('Directors must be able to handle any call')
class CallState(Enum): class CallState(Enum):
READY = 0 READY = 0
IN_PROGRESS = 1 IN_PROGRESS = 1
COMPLETE = 2 COMPLETE = 2
class Call(object): class Call(object):
def __init__(self, rank): def __init__(self, rank):
self.state = CallState.READY self.state = CallState.READY
self.rank = rank self.rank = rank
self.employee = None 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.operators = operators
self.supervisors = supervisors self.supervisors = supervisors
self.directors = directors self.directors = directors
self.queued_calls = deque() self.queued_calls = deque()
def dispatch_call(self, call): def dispatch_call(self, call):
if call.rank not in (Rank.OPERATOR, Rank.SUPERVISOR, Rank.DIRECTOR): if call.rank not in (Rank.OPERATOR, Rank.SUPERVISOR, Rank.DIRECTOR):
raise ValueError('Invalid call rank: {}'.format(call.rank)) raise ValueError('Invalid call rank: {}'.format(call.rank))
employee = None employee = None
if call.rank == Rank.OPERATOR: 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: 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: 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: 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: for employee in employees:
if employee.call is None: if employee.call is None:
employee.take_call(call) employee.take_call(call)
return employee return employee
return None return None
def notify_call_escalated(self, call): # ... def notify_call_escalated(self, call): # ...
def notify_call_completed(self, call): # ... def notify_call_completed(self, call): # ...
def dispatch_queued_call_to_newly_freed_employee(self, call, employee): # ... def dispatch_queued_call_to_newly_freed_employee(self, call, employee): # ...
``` ```
%%%% Output: stream %%%% Output: stream
Overwriting call_center.py Overwriting call_center.py
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment