updated ensemble recommender test cases and collaborative test cases

This commit is contained in:
Victor Ng 2018-08-07 16:31:10 -04:00
Родитель 29bf131142
Коммит 24e4b59c64
3 изменённых файлов: 40 добавлений и 46 удалений

Просмотреть файл

@ -5,8 +5,8 @@
from srgutil.interfaces import IMozLogging
import itertools
from .base_recommender import AbstractRecommender
import threading
import time
from .lazys3 import LazyJSONLoader
S3_BUCKET = 'telemetry-parquet'
ENSEMBLE_WEIGHTS = 'taar/ensemble/ensemble_weight.json'
@ -16,29 +16,15 @@ class WeightCache:
def __init__(self, ctx):
self._ctx = ctx
self._lock = threading.RLock()
self._weights = None
self._expiry = None
def now(self):
return time.time()
if 'ensemble_weights' in self._ctx:
self._weights = self._ctx['ensemble_weights']
else:
self._weights = LazyJSONLoader(ctx,
S3_BUCKET,
ENSEMBLE_WEIGHTS)
def getWeights(self):
with self._lock:
now = self.now()
if self._expiry is not None:
if self._expiry < now:
# Cache is expired.
self._weights = None
# Push expiry to 5 minutes from now
self._expiry = now + 300
if self._weights is None:
tmp = self._ctx['cache'].get_s3_json_content(S3_BUCKET, ENSEMBLE_WEIGHTS)
self._weights = tmp['ensemble_weights']
return self._weights
return self._weights.get()[0]['ensemble_weights']
class EnsembleRecommender(AbstractRecommender):

Просмотреть файл

@ -16,7 +16,7 @@ from taar.recommenders.collaborative_recommender import CollaborativeRecommender
from taar.recommenders.collaborative_recommender import positive_hash
from taar.recommenders.lazys3 import LazyJSONLoader
import json
import pytest
"""
We need to generate a synthetic list of addons and relative weights
@ -39,6 +39,9 @@ def install_none_mock_data(ctx):
ITEM_MATRIX_CONFIG[0],
ITEM_MATRIX_CONFIG[1])
# Don't reuse connections with moto. badness happens
conn = boto3.resource('s3', region_name='us-west-2')
conn.create_bucket(Bucket=ADDON_MAPPING_CONFIG[0])
conn.Object(ADDON_MAPPING_CONFIG[0], ADDON_MAPPING_CONFIG[1]).put(Body="")
ctx['collaborative_addon_mapping'] = LazyJSONLoader(ctx,

Просмотреть файл

@ -2,9 +2,11 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from taar.cache import JSONCache, Clock
from taar.recommenders.ensemble_recommender import WeightCache, EnsembleRecommender
from moto import mock_s3
import boto3
import json
from taar.recommenders.lazys3 import LazyJSONLoader
from .mocks import MockRecommenderFactory
EXPECTED = {'collaborative': 1000,
@ -12,28 +14,34 @@ EXPECTED = {'collaborative': 1000,
'locale': 10}
class Mocker:
def get_s3_json_content(self, *args, **kwargs):
return {'ensemble_weights': EXPECTED}
def install_mock_data(ctx):
DATA = {'ensemble_weights': EXPECTED}
S3_BUCKET = 'telemetry-parquet'
ENSEMBLE_WEIGHTS = 'taar/ensemble/ensemble_weight.json'
conn = boto3.resource('s3', region_name='us-west-2')
conn.create_bucket(Bucket=S3_BUCKET)
conn.Object(S3_BUCKET, ENSEMBLE_WEIGHTS).put(Body=json.dumps(DATA))
ctx['ensemble_weights'] = LazyJSONLoader(ctx,
S3_BUCKET,
ENSEMBLE_WEIGHTS)
return ctx
def test_weight_cache(test_ctx): # noqa
ctx = test_ctx
ctx['utils'] = Mocker()
ctx['clock'] = Clock()
ctx['cache'] = JSONCache(ctx)
wc = WeightCache(ctx.child())
@mock_s3
def test_weight_cache(test_ctx):
ctx = install_mock_data(test_ctx)
wc = WeightCache(ctx)
actual = wc.getWeights()
assert EXPECTED == actual
@mock_s3
def test_recommendations(test_ctx):
ctx = test_ctx
ctx['utils'] = Mocker()
ctx['clock'] = Clock()
ctx['cache'] = JSONCache(ctx)
ctx = install_mock_data(test_ctx)
EXPECTED_RESULTS = [('ghi', 3430.0),
('def', 3320.0),
@ -55,12 +63,9 @@ def test_recommendations(test_ctx):
assert recommendation_list == EXPECTED_RESULTS
@mock_s3
def test_preinstalled_guids(test_ctx):
ctx = test_ctx
ctx['utils'] = Mocker()
ctx['clock'] = Clock()
ctx['cache'] = JSONCache(ctx)
ctx = install_mock_data(test_ctx)
EXPECTED_RESULTS = [('ghi', 3430.0),
('ijk', 3200.0),