def test_profile_fetcher_returns_dict(test_ctx): fetcher = ProfileFetcher(test_ctx) mock_data = MOCK_DATA["profile"] mock_profile_controller = MockProfileController(mock_data) fetcher.set_client(mock_profile_controller) # Note that active_addons in the raw JSON source is remapped to # 'installed_addons' assert fetcher.get("random-client-id") == MOCK_DATA["expected_result"]
def test_profile_fetcher_returns_none(monkeypatch): monkeypatch.setattr(hbase_client.HBaseClient, 'get_client_profile', lambda x, y: None) monkeypatch.setattr(hbase_client.HBaseClient, '_get_hbase_hostname', lambda x: 'master-ip-address') fetcher = ProfileFetcher() assert fetcher.get("random-client-id") is None
def test_dont_crash_without_active_addons(test_ctx): mock_data = copy.deepcopy(MOCK_DATA["profile"]) del mock_data["active_addons"] mock_profile_controller = MockProfileController(mock_data) fetcher = ProfileFetcher(test_ctx) fetcher.set_client(mock_profile_controller) expected = copy.deepcopy(MOCK_DATA["expected_result"]) expected["installed_addons"][:] = [] assert fetcher.get("random-client-id") == expected
def test_profile_fetcher_returns_dict(monkeypatch): monkeypatch.setattr(hbase_client.HBaseClient, 'get_client_addons', get_client_addons_mock) monkeypatch.setattr(hbase_client.HBaseClient, '_get_master_address', lambda x: 'master-ip-address') fetcher = ProfileFetcher() fetcher.get("random-client-id") == { "installed_addons": ["*****@*****.**", "*****@*****.**"], }
def test_none_profile_returns_empty_list(monkeypatch): monkeypatch.setattr(hbase_client.HBaseClient, 'get_client_profile', lambda x, y: None) monkeypatch.setattr(hbase_client.HBaseClient, '_get_hbase_hostname', lambda x: 'master-ip-address') fetcher = ProfileFetcher() rec_manager = RecommendationManager(fetcher, ("fake-recommender", )) assert rec_manager.recommend("random-client-id", 10) == []
def get_test_ctx(): fetcher = ProfileFetcher(MockProfileController(None)) factory = MockRecommenderFactory() ctx = Context() ctx['profile_fetcher'] = fetcher ctx['recommender_factory'] = factory # Just populate the utils key for test when WeightCache is # instantiated ctx['utils'] = None return ctx.child()
def test_profile_fetcher_returns_dict(monkeypatch): monkeypatch.setattr(hbase_client.HBaseClient, 'get_client_profile', get_client_profile_mock) monkeypatch.setattr(hbase_client.HBaseClient, '_get_hbase_hostname', lambda x: 'master-ip-address') fetcher = ProfileFetcher() assert fetcher.get("random-client-id") == { "bookmark_count": 0, "disabled_addons_ids": [], "geo_city": "Rome", "os": "Windows_NT", "subsession_length": 3785, "tab_open_count": 46, "total_uri": 791, "unique_tlds": 11, "installed_addons": ["*****@*****.**", "*****@*****.**"], "locale": "it-IT" }
def check_proxy_manager(PROXY_MANAGER): if PROXY_MANAGER.getResource() is None: ctx = default_context() profile_fetcher = ProfileFetcher(ctx) ctx["profile_fetcher"] = profile_fetcher # Lock the context down after we've got basic bits installed root_ctx = ctx.child() r_factory = recommenders.RecommenderFactory(root_ctx) root_ctx["recommender_factory"] = r_factory instance = recommenders.RecommendationManager(root_ctx.child()) PROXY_MANAGER.setResource(instance) return PROXY_MANAGER.getResource()
def acquire_taar_singleton(PROXY_MANAGER): if PROXY_MANAGER.getTaarRM() is None: ctx = app_context() profile_fetcher = ProfileFetcher(ctx) ctx["profile_fetcher"] = profile_fetcher # Lock the context down after we've got basic bits installed root_ctx = ctx.child() r_factory = RecommenderFactory(root_ctx) root_ctx["recommender_factory"] = r_factory instance = RecommendationManager(root_ctx.child()) PROXY_MANAGER.setTaarRM(instance) return PROXY_MANAGER.getTaarRM()
def recommendations(uuid_client_id): """Return a list of recommendations provided a telemetry client_id.""" # Use the module global PROXY_MANAGER global PROXY_MANAGER # Coerce the uuid.UUID type into a string client_id = str(uuid_client_id) branch = request.args.get('branch', '') extra_data = {'branch': branch} locale = request.args.get('locale', None) if locale is not None: extra_data['locale'] = locale platform = request.args.get('platform', None) if platform is not None: extra_data['platform'] = platform if PROXY_MANAGER.getResource() is None: ctx = default_context() profile_fetcher = ProfileFetcher(ctx) ctx['profile_fetcher'] = profile_fetcher # Lock the context down after we've got basic bits installed root_ctx = ctx.child() r_factory = recommenders.RecommenderFactory(root_ctx) root_ctx['recommender_factory'] = r_factory instance = recommenders.RecommendationManager(root_ctx.child()) PROXY_MANAGER.setResource(instance) instance = PROXY_MANAGER.getResource() recommendations = instance.recommend(client_id=client_id, limit=TAAR_MAX_RESULTS, extra_data=extra_data) # Strip out weights from TAAR results to maintain compatibility # with TAAR 1.0 jdata = {"results": [x[0] for x in recommendations]} response = app.response_class(response=json.dumps(jdata), status=200, mimetype='application/json') return response
def recommendations(request, client_id): """Return a list of recommendations provided a telemetry client_id.""" recommendation_manager = cache.get("recommendation_manager") extra_data = {} locale = request.GET.get('locale', None) if locale is not None: extra_data['locale'] = locale platform = request.GET.get('platform', None) if platform is not None: extra_data['platform'] = platform if recommendation_manager is None: hbase_client = HBaseClient(settings.HBASE_HOST) profile_fetcher = ProfileFetcher(hbase_client) recommendation_manager = recommenders.RecommendationManager( profile_fetcher) cache.set("recommendation_manager", recommendation_manager, CACHE_EXPIRATION) recommendations = recommendation_manager.recommend( client_id, settings.TAAR_MAX_RESULTS, extra_data) return JsonResponse({"results": recommendations})
def test_profile_fetcher_returns_none(test_ctx): fetcher = ProfileFetcher(test_ctx) fetcher.set_client(MockProfileController(None)) assert fetcher.get("random-client-id") is None