chg: [api] add endpoint: get tracked item_id by uuid and daterange

pull/389/head
Terrtia 2019-08-09 17:07:54 +02:00
parent 4b0819a644
commit 80f9535074
No known key found for this signature in database
GPG Key ID: 1E1B1F50D84613D0
3 changed files with 126 additions and 11 deletions

View File

@ -42,6 +42,19 @@ def is_in_role(user_id, role):
else: else:
return False 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): def is_valid_mail(email):
result = email_regex.match(email) result = email_regex.match(email)
if result: if result:
@ -95,6 +108,13 @@ def get_regex_tracked_words_dict():
dict_tracked_regex[regex] = re.compile(regex) dict_tracked_regex[regex] = re.compile(regex)
return dict_tracked_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): def is_term_tracked_in_global_level(term, term_type):
res = r_serv_term.smembers('all:tracked_term_uuid:{}:{}'.format(term_type, term)) res = r_serv_term.smembers('all:tracked_term_uuid:{}:{}'.format(term_type, term))
if res: if res:
@ -231,16 +251,9 @@ def add_tracked_term(term , term_type, user_id, level, tags, mails, dashboard=0)
return term_uuid return term_uuid
def parse_tracked_term_to_delete(dict_input, user_id): def parse_tracked_term_to_delete(dict_input, user_id):
term_uuid = dict_input.get('uuid', None) res = check_term_uuid_valid_access(term_uuid, user_id)
if not is_valid_uuid_v4(term_uuid): if res:
return ({"status": "error", "reason": "Invalid uuid"}, 400) return res
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)
delete_term(term_uuid) delete_term(term_uuid)
return ({"uuid": term_uuid}, 200) return ({"uuid": term_uuid}, 200)
@ -291,7 +304,7 @@ def add_tracked_item(term_uuid, item_id, item_date):
# track item # track item
r_serv_term.sadd('tracked_term:item:{}:{}'.format(term_uuid, item_date), item_id) r_serv_term.sadd('tracked_term:item:{}:{}'.format(term_uuid, item_date), item_id)
# track nb item by date # 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): def create_token_statistics(item_date, word, nb):
r_serv_term.zincrby('stat_token_per_item_by_day:{}'.format(item_date), word, 1) 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 epoch_update = 0
return float(epoch_update) 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)

View File

@ -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`<a name="delete_term_tracker"></a>
#### 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
```

View File

@ -332,6 +332,14 @@ def delete_tracker_term():
res = Term.parse_tracked_term_to_delete(data, user_id) 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] 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 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # IMPORT # # # # # # # # # # # # # # # # # #