Skip to content

Commit

Permalink
Update/Proxy: assume choices are list
Browse files Browse the repository at this point in the history
  • Loading branch information
colethienes committed Dec 18, 2019
1 parent 7b1113d commit c54b0aa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions nboost/maps.py
Expand Up @@ -48,8 +48,8 @@
'topk_path': '(body.size) | (url.query.size)',
'true_cids_path': 'body.nboost.cids',
'choices_path': 'body.hits.hits',
'cvalues_path': '[*]._source.*',
'cids_path': '[*]._id',
'cvalues_path': '_source.*',
'cids_path': '_id',
'capture_path': '/.*/_search',
'default_topk': 10
}
Expand Down
14 changes: 9 additions & 5 deletions nboost/proxy.py
Expand Up @@ -320,13 +320,17 @@ def get_request_paths(request, configs) -> Tuple[str, int, list]:
def get_response_paths(response, configs) -> Tuple[list, list, list]:
"""Get the request jsonpaths noted in the configs"""
choices = get_jsonpath(response, configs['choices_path'])

if not isinstance(choices, list):
raise InvalidChoices('choices were not a list')

choices = flatten(choices)
cids = get_jsonpath(choices, configs['cids_path'])
cvalues = get_jsonpath(choices, configs['cvalues_path'])
cids = get_jsonpath(choices, '[*].' + configs['cids_path'])
cvalues = get_jsonpath(choices, '[*].' + configs['cvalues_path'])

# check for errors
if not len(choices) == len(cids) == len(cvalues):
raise InvalidChoices
raise InvalidChoices('number of choices, cids, and cvalues differ')

return choices, cids, cvalues

Expand Down Expand Up @@ -401,8 +405,8 @@ def loop(self, client_socket: socket.socket, address: Tuple[str, str]):
self.proxy_send(client_socket, server_socket, buffer)
self.proxy_recv(client_socket, server_socket)

except InvalidChoices:
self.logger.warning('Request (%s:%s): invalid choices', *address)
except InvalidChoices as exc:
self.logger.warning('Request (%s:%s): %s', *address, *exc.args)
self.proxy_send(client_socket, server_socket, buffer)
self.proxy_recv(client_socket, server_socket)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_config_map.py
Expand Up @@ -43,10 +43,10 @@ def test_response_1(self):
choices = flatten(choices)
self.assertEqual(1.4, choices[0]['_score'])

cvalues = get_jsonpath(choices, config['cvalues_path'])
cvalues = get_jsonpath(choices, '[*].' + config['cvalues_path'])
self.assertEqual(["trying out Elasticsearch", "second result", "third result"], cvalues)

cids = get_jsonpath(choices, config['cids_path'])
cids = get_jsonpath(choices, '[*].' + config['cids_path'])
self.assertEqual(['0', '1', '2'], cids)


Expand Down

0 comments on commit c54b0aa

Please sign in to comment.