Skip to content

Commit

Permalink
Convert class variables to instance variables
Browse files Browse the repository at this point in the history
Using class variables is not necessary in this case.
  • Loading branch information
Mauro Doglio authored and Dexterp37 committed Aug 14, 2017
1 parent 66e8bbe commit 470a02a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
13 changes: 7 additions & 6 deletions taar/hbase_client.py
Expand Up @@ -7,15 +7,16 @@
class HBaseClient:
_hostname = None

def __init__(self):
def __init__(self, hbase_hostname=None):
self.tablename = 'main_summary'
self.column_family = b'cf'
self.column = b'cf:payload'
if HBaseClient._hostname is None:
HBaseClient._hostname = self._get_master_address()
print(HBaseClient._hostname)
if hbase_hostname is None:
self.hbase_hostname = self._get_hbase_hostname()
else:
self.hbase_hostname = hbase_hostname

def _get_master_address(self):
def _get_hbase_hostname(self):
client = boto3.client('ec2')
reservations = client.describe_instances(Filters=[
{'Name': 'tag:Name', 'Values': ['telemetry-hbase']},
Expand All @@ -37,7 +38,7 @@ def get_client_profile(self, client_id):
"""Retrieve the latest row for the given client in HBase
Only the last known version of the info is retrieved"""
with contextlib.closing(Connection(self._hostname)) as connection:
with contextlib.closing(Connection(self.hbase_hostname)) as connection:
table = connection.table(self.tablename)
row_start = "{}:{}".format(client_id, "99999999")
for key, data in table.scan(row_start=row_start, limit=1,
Expand Down
7 changes: 5 additions & 2 deletions taar/profile_fetcher.py
Expand Up @@ -4,8 +4,11 @@
class ProfileFetcher:
""" Fetch the latest information for a client on HBase.
"""
def __init__(self):
self.hbase_client = HBaseClient()
def __init__(self, hbase_client=None):
if hbase_client is None:
self.hbase_client = HBaseClient()
else:
self.hbase_client = hbase_client

def get(self, client_id):
profile_data = self.hbase_client.get_client_profile(client_id)
Expand Down
30 changes: 16 additions & 14 deletions taar/recommenders/recommendation_manager.py
Expand Up @@ -9,31 +9,33 @@


class RecommendationManager:
"""
"""
info_fetcher = None
recommenders = []

def __init__(self):
def __init__(self, profile_fetcher=None, recommenders=None):
# Instantiate the object to get the client info.
if RecommendationManager.info_fetcher is None:
logger.info("Initializing info_fetcher")
RecommendationManager.info_fetcher = ProfileFetcher()
if profile_fetcher is None:
logger.info("Initializing profile_fetcher")
self.profile_fetcher = ProfileFetcher()
else:
self.profile_fetcher = profile_fetcher

# Init the recommenders. Note: ORDER MATTERS!
# The recommendation logic will go through each recommender and pick the
# first one that "can_recommend".
if not RecommendationManager.recommenders:
if not recommenders:
logger.info("Initializing recommenders")
RecommendationManager.recommenders.append(CollaborativeRecommender())
RecommendationManager.recommenders.append(LocaleRecommender())
RecommendationManager.recommenders.append(EmptyRecommender())
self.recommenders = (
CollaborativeRecommender(),
LocaleRecommender(),
EmptyRecommender()
)
else:
self.recommenders = recommenders

def recommend(self, client_id, limit):
# Get the info for the requested client id.
client_info = RecommendationManager.info_fetcher.get(client_id)
client_info = self.profile_fetcher.get(client_id)

# Compute the recommendation.
for r in RecommendationManager.recommenders:
for r in self.recommenders:
if r.can_recommend(client_info):
return r.recommend(client_info, limit)
2 changes: 1 addition & 1 deletion tests/test_profile_fetcher.py
Expand Up @@ -18,7 +18,7 @@ def test_profile_fetcher_returns_dict(monkeypatch):
get_client_profile_mock)

monkeypatch.setattr(hbase_client.HBaseClient,
'_get_master_address',
'_get_hbase_hostname',
lambda x: 'master-ip-address')

fetcher = ProfileFetcher()
Expand Down

0 comments on commit 470a02a

Please sign in to comment.