def searches(from_, to, hashids=None, device=None, query_name=None, source=None, total_hits=None, tz=None, format_=None, **opts): """ Return number of searches in a period grouped by date. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'device': device, 'query_name': query_name, 'source': source, 'total_hits': total_hits, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/searches', query_params)
def searches_top(from_, to, hashids=None, device=None, query_name=None, exclude=None, total_hits=None, tz=None, format_=None, **opts): """ Returns list of most common searches in a period. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'device': device, 'query_name': query_name, 'exclude': exclude, 'total_hits': total_hits, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/searches/top', query_params)
def reindex_to_temp(hashid, name, **opts): params = {} if 'reset' in opts: params['reset'] = opts['reset'] api_client = ManagementAPIClient(**opts) return api_client.post(_get_index_url(hashid, name) + '/_reindex_to_temp', query_params=params)
def create_temp(hashid, name, **opts): params = {} if 'destination_server' in opts: params['destination_server'] = opts['destination_server'] api_client = ManagementAPIClient(**opts) return api_client.post(_get_index_url(hashid, name) + '/temp', query_params=params)
def __init__(self, hashid, name, rpp=None, **opts): super(Scroll, self).__init__() self.scroll_id = None self.rpp = rpp self.hashid = hashid self.name = name self.api_client = ManagementAPIClient(**opts)
class Scroll(): @staticmethod def __get_url(hashid, name): return f'/api/v2/search_engines/{hashid}/indices/{name}/items/' def __init__(self, hashid, name, rpp=None, **opts): super(Scroll, self).__init__() self.scroll_id = None self.rpp = rpp self.hashid = hashid self.name = name self.api_client = ManagementAPIClient(**opts) def __iter__(self): scroll_page = self.new() while scroll_page['items']: for item in scroll_page['items']: yield item scroll_page = self.__next_with_retry() def __next_with_retry(self): tries = 0 while True: try: return self.next() except TooManyRequestsError: if tries < 3: tries += 1 sleep(1) else: raise @property def _query_params(self): params = {} if self.scroll_id: params['scroll_id'] = self.scroll_id if self.rpp: params['rpp'] = self.rpp return params def new(self): scroll_page = self.api_client.get( self.__get_url(self.hashid, self.name), self._query_params) self.scroll_id = scroll_page['scroll_id'] return scroll_page def next(self): return self.api_client.get(self.__get_url(self.hashid, self.name), self._query_params)
def query_log_iter(from_, to, hashids=None, **opts): """ Returns an iterator with all the queries done to the search API in a period. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids }) api_client = ManagementAPIClient(stream=True, **opts) return api_client.request('GET', '/api/v2/stats/query_log', query_params)
def facets(from_, to, hashids=None, tz=None, format_=None, **opts): """ Returns how many times facets filters have been used in a period, grouped by facet field name. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/facets', query_params)
def inits(from_, to, hashids=None, device=None, tz=None, format_=None, **opts): """ Returns number of total unique search sessions in a period group by date. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'device': device, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/inits', query_params)
def sales(from_, to, hashids=None, tz=None, format_=None, **opts): """ Returns the total price for sales checkouts in a period, where session_id identifies every sale. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/sales', query_params)
def usage(from_, to, hashids=None, type_=None, format_=None, **opts): """ Returns usage to search engines during a period. It sums the query and API requests made to the service. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'type': type_, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/usage', query_params)
def facets_top(from_, to, hashids=None, tz=None, format_=None, **opts): """ Returns most common facets filters used, how many times they have been used, and which filter has been applied in a period. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/facets/top', query_params)
def test_can_set_per_request_host(self, requests_mock): response_mock = mock.Mock() response_mock.status_code = 200 response_mock.text = 'OK' session_mock = mock.Mock() session_mock.request.return_value = response_mock requests_mock.Session.return_value = session_mock pydoof.management_url = 'https://eu1-api.doofinder.com' api_client = ManagementAPIClient( management_url='http://localhost:8000') api_client.request('GET', url='/') session_mock.request.assert_called_with('GET', url='http://localhost:8000/', params=mock.ANY, json=mock.ANY, headers=mock.ANY, auth=mock.ANY)
def clicks(from_, to, hashids=None, device=None, tz=None, format_=None, **opts): """ Returns number of times a user clicked an item in a period grouped by date. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'device': device, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/clicks', query_params)
def redirects(from_, to, hashids=None, redirect_id=None, tz=None, format_=None, **opts): """ Returns how many times users have been redirected by a custom redirections, and to which url in a period. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'id': redirect_id, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/redirects', query_params)
def test_raise(self, status_code, error_code, expected_exception, requests_mock): response_mock = mock.Mock() response_mock.status_code = status_code response_mock.json.return_value = { 'error': { 'code': error_code, 'details': 'Details', 'message': 'Message' } } session_mock = mock.Mock() session_mock.request.return_value = response_mock requests_mock.Session.return_value = session_mock api_client = ManagementAPIClient() with self.assertRaises(expected_exception) as cm: api_client.request('GET', url='/') self.assertEqual(cm.exception.details, 'Details') self.assertEqual(cm.exception.message, 'Message')
def banners(from_, to, hashids=None, banner_id=None, tz=None, format_=None, **opts): """ Returns how many times banners have been displayed and clicked in a period, grouped by banner id. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'id': banner_id, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/banners', query_params)
def inits_locations(from_, to, hashids=None, device=None, tz=None, format_=None, **opts): """ Returns all unique sessions geolocation(longituted and latitude pairs) for a period. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'device': device, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/inits/locations', query_params)
def clicked_items_searches(from_, to, dfid, hashids=None, device=None, tz=None, format_=None, **opts): """ Returns the most common searched for a clicked item, and how many times it has been clicked from those searches. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'dfid': dfid, 'hashid': hashids, 'device': device, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/clicked_items/searches', query_params)
def clicked_items(from_, to, hashids=None, query=None, device=None, limit=None, tz=None, format_=None, **opts): """ Returns most commonly clicked items in a period. """ query_params = parse_query_params({ 'from': from_, 'to': to, 'hashid': hashids, 'query': query, 'device': device, 'limit': limit, 'tz': tz, 'format': format_ }) api_client = ManagementAPIClient(**opts) return api_client.get('/api/v2/stats/clicked_items', query_params)
def update(hashid, name, index, **opts): api_client = ManagementAPIClient(**opts) return api_client.patch(_get_index_url(hashid, name), index)
def delete_temp(hashid, name, **opts): api_client = ManagementAPIClient(**opts) api_client.delete(_get_index_url(hashid, name) + '/temp')
def delete(hashid, **opts): api_client = ManagementAPIClient(**opts) api_client.delete(_get_searchengine_url(hashid))
def create(hashid, index, **opts): api_client = ManagementAPIClient(**opts) return api_client.post(_get_indices_url(hashid), index)
def schedule_process(hashid, **opts): api_client = ManagementAPIClient(**opts) return api_client.post(_get_process_url(hashid))
def get_process_status(hashid, **opts): api_client = ManagementAPIClient(**opts) return api_client.get(_get_process_url(hashid))
def get_reindex_status(hashid, name, **opts): api_client = ManagementAPIClient(**opts) return api_client.get(_get_index_url(hashid, name) + '/_reindex_to_temp')
def get(hashid, name, **opts): api_client = ManagementAPIClient(**opts) return api_client.get(_get_index_url(hashid, name))
def replace_by_temp(hashid, name, **opts): api_client = ManagementAPIClient(**opts) return api_client.post(_get_index_url(hashid, name) + '/_replace_by_temp')
def list(hashid, **opts): api_client = ManagementAPIClient(**opts) return api_client.get(_get_indices_url(hashid))