From 80f95350747d60a859c3a4ea3ed677f736c816d4 Mon Sep 17 00:00:00 2001 From: Terrtia Date: Fri, 9 Aug 2019 17:07:54 +0200 Subject: [PATCH] chg: [api] add endpoint: get tracked item_id by uuid and daterange --- bin/packages/Term.py | 62 ++++++++++++++++++---- doc/README.md | 67 ++++++++++++++++++++++++ var/www/modules/restApi/Flask_restApi.py | 8 +++ 3 files changed, 126 insertions(+), 11 deletions(-) diff --git a/bin/packages/Term.py b/bin/packages/Term.py index 30911252..7a6655ee 100755 --- a/bin/packages/Term.py +++ b/bin/packages/Term.py @@ -42,6 +42,19 @@ def is_in_role(user_id, role): else: return False +def check_term_uuid_valid_access(term_uuid, user_id): + if not is_valid_uuid_v4(term_uuid): + return ({"status": "error", "reason": "Invalid uuid"}, 400) + level = r_serv_term.hget('tracked_term:{}'.format(term_uuid), 'level') + if not level: + return ({"status": "error", "reason": "Unknown uuid"}, 404) + if level == 0: + if r_serv_term.hget('tracked_term:{}'.format(term_uuid), 'user_id') != user_id: + if not is_in_role(user_id, 'admin'): + return ({"status": "error", "reason": "Unknown uuid"}, 404) + return None + + def is_valid_mail(email): result = email_regex.match(email) if result: @@ -95,6 +108,13 @@ def get_regex_tracked_words_dict(): dict_tracked_regex[regex] = re.compile(regex) return dict_tracked_regex +def get_tracked_term_list_item(term_uuid, date_from, date_to): + all_item_id = [] + if date_from and date_to: + for date in r_serv_term.zrangebyscore('tracked_term:stat:{}'.format(term_uuid), int(date_from), int(date_to)): + all_item_id = all_item_id + list(r_serv_term.smembers('tracked_term:item:{}:{}'.format(term_uuid, date))) + return all_item_id + def is_term_tracked_in_global_level(term, term_type): res = r_serv_term.smembers('all:tracked_term_uuid:{}:{}'.format(term_type, term)) if res: @@ -231,16 +251,9 @@ def add_tracked_term(term , term_type, user_id, level, tags, mails, dashboard=0) return term_uuid def parse_tracked_term_to_delete(dict_input, user_id): - term_uuid = dict_input.get('uuid', None) - if not is_valid_uuid_v4(term_uuid): - return ({"status": "error", "reason": "Invalid uuid"}, 400) - level = r_serv_term.hget('tracked_term:{}'.format(term_uuid), 'level') - if not level: - return ({"status": "error", "reason": "Unknown uuid"}, 404) - if level == 0: - if r_serv_term.hget('tracked_term:{}'.format(term_uuid), 'user_id') != user_id: - if not is_in_role(user_id, 'admin'): - return ({"status": "error", "reason": "Unknown uuid"}, 404) + res = check_term_uuid_valid_access(term_uuid, user_id) + if res: + return res delete_term(term_uuid) return ({"uuid": term_uuid}, 200) @@ -291,7 +304,7 @@ def add_tracked_item(term_uuid, item_id, item_date): # track item r_serv_term.sadd('tracked_term:item:{}:{}'.format(term_uuid, item_date), item_id) # track nb item by date - r_serv_term.zincrby('tracked_term:stat:{}'.format(term_uuid), item_date, 1) + r_serv_term.zadd('tracked_term:stat:{}'.format(term_uuid), item_date, int(item_date)) def create_token_statistics(item_date, word, nb): r_serv_term.zincrby('stat_token_per_item_by_day:{}'.format(item_date), word, 1) @@ -312,8 +325,35 @@ def get_tracked_term_last_updated_by_type(term_type): epoch_update = 0 return float(epoch_update) +def parse_get_tracker_term_item(dict_input, user_id): + term_uuid = dict_input.get('uuid', None) + res = check_term_uuid_valid_access(term_uuid, user_id) + if res: + return res + date_from = dict_input.get('date_from', None) + date_to = dict_input.get('date_to', None) + + if date_from is None: + date_from = r_serv_term.zrevrange('tracked_term:stat:{}'.format(term_uuid), 0, 0) + if date_from: + date_from = date_from[0] + + if date_to is None: + date_to = date_from + + if date_from > date_to: + date_from = date_to + + all_item_id = get_tracked_term_list_item(term_uuid, date_from, date_to) + + res_dict = {} + res_dict['uuid'] = term_uuid + res_dict['date_from'] = date_from + res_dict['date_to'] = date_to + res_dict['items'] = all_item_id + return (res_dict, 200) diff --git a/doc/README.md b/doc/README.md index 764b4ff5..91b486d0 100644 --- a/doc/README.md +++ b/doc/README.md @@ -708,6 +708,73 @@ curl https://127.0.0.1:7000/api/v1/add/tracker/term --header "Authorization: iHc +### Delete term tracker: `api/v1/add/tracker/term/item` + +#### Description +Delete term tracker + +**Method** : `POST` + +#### Parameters +- `uuid` + - tracked term uuid + - *uuid4* + - mandatory +- `date_from` + - date from + - *str - YYMMDD* + - default: last tracked items date +- `date_to` + - date to + - *str - YYMMDD* + - default: `None` + +#### JSON response +- `uuid` + - term uuid + - *uuid4* +- `date_from` + - date from + - *str - YYMMDD* +- `date_to` + - date to + - *str - YYMMDD* +- `items` + - list of item id + - *list* + +#### Example +``` +curl https://127.0.0.1:7000/api/v1/add/tracker/term --header "Authorization: iHc1_ChZxj1aXmiFiF1mkxxQkzawwriEaZpPqyTQj " -H "Content-Type: application/json" --data @input.json -X POST +``` + +#### input.json Example +```json + { + + } +``` + +#### Expected Success Response +**HTTP Status Code** : `200` + +```json + { + + } +``` + +#### Expected Fail Response +**HTTP Status Code** : `400` + +```json + +``` + + + + + diff --git a/var/www/modules/restApi/Flask_restApi.py b/var/www/modules/restApi/Flask_restApi.py index 3544f705..e57157fe 100644 --- a/var/www/modules/restApi/Flask_restApi.py +++ b/var/www/modules/restApi/Flask_restApi.py @@ -332,6 +332,14 @@ def delete_tracker_term(): res = Term.parse_tracked_term_to_delete(data, user_id) return Response(json.dumps(res[0], indent=2, sort_keys=True), mimetype='application/json'), res[1] +@restApi.route("api/v1/get/tracker/term/item", methods=['POST']) +@token_required('analyst') +def get_tracker_term_item(): + data = request.get_json() + user_token = get_auth_from_header() + user_id = get_user_from_token(user_token) + res = Term.parse_get_tracker_term_item(data, user_id) + return Response(json.dumps(res[0], indent=2, sort_keys=True), mimetype='application/json'), res[1] # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # IMPORT # # # # # # # # # # # # # # # # # #