Changeset View
Changeset View
Standalone View
Standalone View
swh/web/ui/views/api.py
Show All 17 Lines | def api_stats(): | ||||
Returns: | Returns: | ||||
SWH storage's statistics. | SWH storage's statistics. | ||||
""" | """ | ||||
return service.stat_counters() | return service.stat_counters() | ||||
@app.route('/api/1/search/') | @app.route('/api/1/search/', methods=['POST']) | ||||
@app.route('/api/1/search/<string:q>/') | @app.route('/api/1/search/<string:q>/') | ||||
def api_search(q): | def api_search(q): | ||||
"""Search a content per hash. | """Search a content per hash. | ||||
Args: | Args: | ||||
q is of the form algo_hash:hash with algo_hash in | q is of the form algo_hash:hash with algo_hash in | ||||
(sha1, sha1_git, sha256). | (sha1, sha1_git, sha256). | ||||
Returns: | Returns: | ||||
Dictionary with 'found' key and the associated result. | Dictionary with 'found' key and the associated result. | ||||
Raises: | Raises: | ||||
BadInputExc in case of unknown algo_hash or bad hash. | BadInputExc in case of unknown algo_hash or bad hash. | ||||
Example: | Example: | ||||
GET /api/1/search/sha1:bd819b5b28fcde3bf114d16a44ac46250da94ee5/ | GET /api/1/search/sha1:bd819b5b28fcde3bf114d16a44ac46250da94ee5/ | ||||
""" | """ | ||||
r = service.lookup_hash(q).get('found') | |||||
return {'found': True if r else False} | response = {'search_res': None, | ||||
'search_stats': None} | |||||
search_stats = {'nbfiles': 0, 'pct': 0} | |||||
search_res = None | |||||
# Single hash request route | |||||
if q: | |||||
r = service.search_hash(q) | |||||
search_res = [{'filename': None, | |||||
'sha1': q, | |||||
'found': r['found']}] | |||||
search_stats['nbfiles'] = 1 | |||||
search_stats['pct'] = 100 if r['found'] else 0 | |||||
# Post form submission with many hash requests | |||||
elif request.method == 'POST': | |||||
data = request.form | |||||
queries = [] | |||||
# Remove potential inputs with no associated value | |||||
for k, v in data.items(): | |||||
if v is not None: | |||||
if k == 'q': | |||||
queries.append({'filename': None, 'sha1': v}) | |||||
elif v != '': | |||||
queries.append({'filename': k, 'sha1': v}) | |||||
if len(queries) > 0: | |||||
lookup = service.lookup_multiple_hashes(queries) | |||||
result = [] | |||||
for el in lookup: | |||||
result.append({'filename': el['filename'], | |||||
'sha1': el['sha1'], | |||||
'found': el['found']}) | |||||
search_res = result | |||||
nbfound = len([x for x in lookup if x['found']]) | |||||
search_stats['nbfiles'] = len(queries) | |||||
search_stats['pct'] = (nbfound / len(queries))*100 | |||||
response['search_res'] = search_res | |||||
response['search_stats'] = search_stats | |||||
return response | |||||
def _api_lookup(criteria, | def _api_lookup(criteria, | ||||
lookup_fn, | lookup_fn, | ||||
error_msg_if_not_found, | error_msg_if_not_found, | ||||
enrich_fn=lambda x: x, | enrich_fn=lambda x: x, | ||||
*args): | *args): | ||||
"""Capture a redundant behavior of: | """Capture a redundant behavior of: | ||||
▲ Show 20 Lines • Show All 849 Lines • Show Last 20 Lines |