2019-08-06 17:03:49 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
import os
|
2019-08-07 12:08:24 +02:00
|
|
|
import re
|
2019-08-06 17:03:49 +02:00
|
|
|
import sys
|
2019-08-09 14:20:13 +02:00
|
|
|
import time
|
2019-08-06 17:03:49 +02:00
|
|
|
import uuid
|
|
|
|
import redis
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
from nltk.tokenize import RegexpTokenizer
|
|
|
|
from textblob import TextBlob
|
|
|
|
|
2019-11-05 15:18:03 +01:00
|
|
|
sys.path.append(os.path.join(os.environ['AIL_BIN'], 'lib/'))
|
|
|
|
import ConfigLoader
|
2020-08-12 09:28:36 +02:00
|
|
|
import Tracker
|
2019-11-05 15:18:03 +01:00
|
|
|
|
2019-09-13 11:06:41 +02:00
|
|
|
from flask import escape
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-14 09:44:49 +02:00
|
|
|
import Date
|
2019-09-11 15:33:04 +02:00
|
|
|
import Item
|
2019-08-14 09:44:49 +02:00
|
|
|
|
2019-11-05 15:18:03 +01:00
|
|
|
config_loader = ConfigLoader.ConfigLoader()
|
|
|
|
r_serv_term = config_loader.get_redis_conn("ARDB_Tracker")
|
|
|
|
config_loader = None
|
|
|
|
|
|
|
|
email_regex = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}'
|
|
|
|
email_regex = re.compile(email_regex)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
special_characters = set('[<>~!?@#$%^&*|()_-+={}":;,.\'\n\r\t]/\\')
|
|
|
|
special_characters.add('\\s')
|
|
|
|
|
|
|
|
# NLTK tokenizer
|
|
|
|
tokenizer = RegexpTokenizer('[\&\~\:\;\,\.\(\)\{\}\|\[\]\\\\/\-/\=\'\"\%\$\?\@\+\#\_\^\<\>\!\*\n\r\t\s]+',
|
|
|
|
gaps=True, discard_empty=True)
|
|
|
|
|
2019-08-09 15:22:57 +02:00
|
|
|
def is_valid_uuid_v4(UUID):
|
2020-12-08 16:47:55 +01:00
|
|
|
if not UUID:
|
|
|
|
return False
|
2019-08-09 15:22:57 +02:00
|
|
|
UUID = UUID.replace('-', '')
|
|
|
|
try:
|
|
|
|
uuid_test = uuid.UUID(hex=UUID, version=4)
|
|
|
|
return uuid_test.hex == UUID
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# # TODO: use new package => duplicate fct
|
|
|
|
def is_in_role(user_id, role):
|
|
|
|
if r_serv_db.sismember('user_role:{}'.format(role), user_id):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-08-09 17:07:54 +02:00
|
|
|
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)
|
2019-09-11 15:33:04 +02:00
|
|
|
level = r_serv_term.hget('tracker:{}'.format(term_uuid), 'level')
|
2019-08-09 17:07:54 +02:00
|
|
|
if not level:
|
|
|
|
return ({"status": "error", "reason": "Unknown uuid"}, 404)
|
|
|
|
if level == 0:
|
2019-09-11 15:33:04 +02:00
|
|
|
if r_serv_term.hget('tracker:{}'.format(term_uuid), 'user_id') != user_id:
|
2019-08-09 17:07:54 +02:00
|
|
|
if not is_in_role(user_id, 'admin'):
|
|
|
|
return ({"status": "error", "reason": "Unknown uuid"}, 404)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2019-08-07 12:08:24 +02:00
|
|
|
def is_valid_mail(email):
|
|
|
|
result = email_regex.match(email)
|
|
|
|
if result:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def verify_mail_list(mail_list):
|
|
|
|
for mail in mail_list:
|
|
|
|
if not is_valid_mail(mail):
|
|
|
|
return ({'status': 'error', 'reason': 'Invalid email', 'value': mail}, 400)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def is_valid_regex(term_regex):
|
|
|
|
try:
|
|
|
|
re.compile(term_regex)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2019-08-06 17:03:49 +02:00
|
|
|
def get_text_word_frequency(item_content, filtering=True):
|
|
|
|
item_content = item_content.lower()
|
|
|
|
words_dict = defaultdict(int)
|
|
|
|
|
|
|
|
if filtering:
|
|
|
|
blob = TextBlob(item_content , tokenizer=tokenizer)
|
|
|
|
else:
|
|
|
|
blob = TextBlob(item_content)
|
|
|
|
for word in blob.tokens:
|
|
|
|
words_dict[word] += 1
|
|
|
|
return words_dict
|
|
|
|
|
|
|
|
# # TODO: create all tracked words
|
|
|
|
def get_tracked_words_list():
|
2019-09-11 15:33:04 +02:00
|
|
|
return list(r_serv_term.smembers('all:tracker:word'))
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
def get_set_tracked_words_list():
|
2019-09-11 15:33:04 +02:00
|
|
|
set_list = r_serv_term.smembers('all:tracker:set')
|
2019-08-06 17:03:49 +02:00
|
|
|
all_set_list = []
|
|
|
|
for elem in set_list:
|
2019-08-07 12:08:24 +02:00
|
|
|
res = elem.split(';')
|
|
|
|
num_words = int(res[1])
|
|
|
|
ter_set = res[0].split(',')
|
|
|
|
all_set_list.append((ter_set, num_words, elem))
|
|
|
|
return all_set_list
|
|
|
|
|
2019-08-09 14:20:13 +02:00
|
|
|
def get_regex_tracked_words_dict():
|
2019-09-11 15:33:04 +02:00
|
|
|
regex_list = r_serv_term.smembers('all:tracker:regex')
|
2019-08-09 14:20:13 +02:00
|
|
|
dict_tracked_regex = {}
|
|
|
|
for regex in regex_list:
|
|
|
|
dict_tracked_regex[regex] = re.compile(regex)
|
|
|
|
return dict_tracked_regex
|
|
|
|
|
2019-08-09 17:07:54 +02:00
|
|
|
def get_tracked_term_list_item(term_uuid, date_from, date_to):
|
|
|
|
all_item_id = []
|
|
|
|
if date_from and date_to:
|
2019-09-11 15:33:04 +02:00
|
|
|
for date in r_serv_term.zrangebyscore('tracker:stat:{}'.format(term_uuid), int(date_from), int(date_to)):
|
|
|
|
all_item_id = all_item_id + list(r_serv_term.smembers('tracker:item:{}:{}'.format(term_uuid, date)))
|
2019-08-09 17:07:54 +02:00
|
|
|
return all_item_id
|
|
|
|
|
2019-08-09 14:20:13 +02:00
|
|
|
def is_term_tracked_in_global_level(term, term_type):
|
2019-09-11 15:33:04 +02:00
|
|
|
res = r_serv_term.smembers('all:tracker_uuid:{}:{}'.format(term_type, term))
|
2019-08-07 12:08:24 +02:00
|
|
|
if res:
|
|
|
|
for elem_uuid in res:
|
2019-09-11 15:33:04 +02:00
|
|
|
if r_serv_term.hget('tracker:{}'.format(elem_uuid), 'level')=='1':
|
2019-08-07 12:08:24 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-08-09 14:20:13 +02:00
|
|
|
def is_term_tracked_in_user_level(term, term_type, user_id):
|
2019-09-11 15:33:04 +02:00
|
|
|
res = r_serv_term.smembers('user:tracker:{}'.format(user_id))
|
2019-08-09 14:20:13 +02:00
|
|
|
if res:
|
|
|
|
for elem_uuid in res:
|
2019-09-11 15:33:04 +02:00
|
|
|
if r_serv_term.hget('tracker:{}'.format(elem_uuid), 'tracked')== term:
|
|
|
|
if r_serv_term.hget('tracker:{}'.format(elem_uuid), 'type')== term_type:
|
2019-08-09 14:20:13 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-08-07 12:08:24 +02:00
|
|
|
def parse_json_term_to_add(dict_input, user_id):
|
2019-08-06 17:03:49 +02:00
|
|
|
term = dict_input.get('term', None)
|
|
|
|
if not term:
|
|
|
|
return ({"status": "error", "reason": "Term not provided"}, 400)
|
2019-08-07 12:08:24 +02:00
|
|
|
term_type = dict_input.get('type', None)
|
2019-08-06 17:03:49 +02:00
|
|
|
if not term_type:
|
|
|
|
return ({"status": "error", "reason": "Term type not provided"}, 400)
|
|
|
|
nb_words = dict_input.get('nb_words', 1)
|
2019-09-13 16:33:34 +02:00
|
|
|
description = dict_input.get('description', '')
|
|
|
|
description = escape(description)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
res = parse_tracked_term_to_add(term , term_type, nb_words=nb_words)
|
2019-08-07 12:08:24 +02:00
|
|
|
if res[1]!=200:
|
2019-08-06 17:03:49 +02:00
|
|
|
return res
|
2019-08-07 12:08:24 +02:00
|
|
|
term = res[0]['term']
|
|
|
|
term_type = res[0]['type']
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
tags = dict_input.get('tags', [])
|
|
|
|
mails = dict_input.get('mails', [])
|
2019-08-07 12:08:24 +02:00
|
|
|
res = verify_mail_list(mails)
|
|
|
|
if res:
|
|
|
|
return res
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
## TODO: add dashboard key
|
|
|
|
level = dict_input.get('level', 1)
|
|
|
|
try:
|
|
|
|
level = int(level)
|
|
|
|
if level not in range(0, 1):
|
|
|
|
level = 1
|
|
|
|
except:
|
|
|
|
level = 1
|
|
|
|
|
2019-08-07 12:08:24 +02:00
|
|
|
# check if term already tracked in global
|
|
|
|
if level==1:
|
2019-08-09 14:20:13 +02:00
|
|
|
if is_term_tracked_in_global_level(term, term_type):
|
|
|
|
return ({"status": "error", "reason": "Term already tracked"}, 409)
|
|
|
|
else:
|
|
|
|
if is_term_tracked_in_user_level(term, term_type, user_id):
|
2019-08-07 12:08:24 +02:00
|
|
|
return ({"status": "error", "reason": "Term already tracked"}, 409)
|
|
|
|
|
2019-09-13 16:33:34 +02:00
|
|
|
term_uuid = add_tracked_term(term , term_type, user_id, level, tags, mails, description)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-07 12:08:24 +02:00
|
|
|
return ({'term': term, 'type': term_type, 'uuid': term_uuid}, 200)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def parse_tracked_term_to_add(term , term_type, nb_words=1):
|
|
|
|
if term_type=='regex':
|
2019-08-07 12:08:24 +02:00
|
|
|
if not is_valid_regex(term):
|
|
|
|
return ({"status": "error", "reason": "Invalid regex"}, 400)
|
2019-08-06 17:03:49 +02:00
|
|
|
elif term_type=='word' or term_type=='set':
|
|
|
|
# force lowercase
|
|
|
|
term = term.lower()
|
|
|
|
word_set = set(term)
|
|
|
|
set_inter = word_set.intersection(special_characters)
|
|
|
|
if set_inter:
|
|
|
|
return ({"status": "error", "reason": "special character not allowed", "message": "Please use a regex or remove all special characters"}, 400)
|
|
|
|
words = term.split()
|
|
|
|
# not a word
|
2019-08-07 12:08:24 +02:00
|
|
|
if term_type=='word' and len(words)>1:
|
2019-08-06 17:03:49 +02:00
|
|
|
term_type = 'set'
|
|
|
|
|
|
|
|
# ouput format: term1,term2,term3;2
|
|
|
|
if term_type=='set':
|
|
|
|
try:
|
|
|
|
nb_words = int(nb_words)
|
|
|
|
except:
|
|
|
|
nb_words = 1
|
2019-08-07 12:08:24 +02:00
|
|
|
if nb_words==0:
|
|
|
|
nb_words = 1
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
words_set = set(words)
|
|
|
|
words_set = sorted(words_set)
|
2019-08-07 12:08:24 +02:00
|
|
|
|
2020-12-08 16:47:55 +01:00
|
|
|
if nb_words > len(words_set):
|
|
|
|
nb_words = len(words_set)
|
|
|
|
|
2019-08-06 17:03:49 +02:00
|
|
|
term = ",".join(words_set)
|
|
|
|
term = "{};{}".format(term, nb_words)
|
|
|
|
|
2020-08-12 09:28:36 +02:00
|
|
|
elif term_type=='yara_custom':
|
|
|
|
if not Tracker.is_valid_yara_rule(term):
|
|
|
|
return ({"status": "error", "reason": "Invalid custom Yara Rule"}, 400)
|
|
|
|
elif term_type=='yara_default':
|
|
|
|
if not Tracker.is_valid_default_yara_rule(term):
|
|
|
|
return ({"status": "error", "reason": "The Yara Rule doesn't exist"}, 400)
|
2019-08-06 17:03:49 +02:00
|
|
|
else:
|
|
|
|
return ({"status": "error", "reason": "Incorrect type"}, 400)
|
2019-08-07 12:08:24 +02:00
|
|
|
return ({"status": "success", "term": term, "type": term_type}, 200)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-09-13 16:33:34 +02:00
|
|
|
def add_tracked_term(term , term_type, user_id, level, tags, mails, description, dashboard=0):
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
term_uuid = str(uuid.uuid4())
|
|
|
|
|
2020-08-12 09:28:36 +02:00
|
|
|
# YARA
|
|
|
|
if term_type == 'yara_custom' or term_type == 'yara_default':
|
|
|
|
term = Tracker.save_yara_rule(term_type, term, tracker_uuid=term_uuid)
|
|
|
|
term_type = 'yara'
|
|
|
|
|
2019-08-06 17:03:49 +02:00
|
|
|
# create metadata
|
2020-08-12 09:28:36 +02:00
|
|
|
r_serv_term.hset('tracker:{}'.format(term_uuid), 'tracked',term) # # TODO: use hash
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.hset('tracker:{}'.format(term_uuid), 'type', term_type)
|
|
|
|
r_serv_term.hset('tracker:{}'.format(term_uuid), 'date', datetime.date.today().strftime("%Y%m%d"))
|
|
|
|
r_serv_term.hset('tracker:{}'.format(term_uuid), 'user_id', user_id)
|
|
|
|
r_serv_term.hset('tracker:{}'.format(term_uuid), 'level', level)
|
|
|
|
r_serv_term.hset('tracker:{}'.format(term_uuid), 'dashboard', dashboard)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-09-13 16:33:34 +02:00
|
|
|
if description:
|
|
|
|
r_serv_term.hset('tracker:{}'.format(term_uuid), 'description', description)
|
|
|
|
|
2019-08-06 17:03:49 +02:00
|
|
|
# create all term set
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.sadd('all:tracker:{}'.format(term_type), term)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
# create term - uuid map
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.sadd('all:tracker_uuid:{}:{}'.format(term_type, term), term_uuid)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
# add display level set
|
|
|
|
if level == 0: # user only
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.sadd('user:tracker:{}'.format(user_id), term_uuid)
|
|
|
|
r_serv_term.sadd('user:tracker:{}:{}'.format(user_id, term_type), term_uuid)
|
2019-08-06 17:03:49 +02:00
|
|
|
elif level == 1: # global
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.sadd('global:tracker', term_uuid)
|
|
|
|
r_serv_term.sadd('global:tracker:{}'.format(term_type), term_uuid)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
# create term tags list
|
|
|
|
for tag in tags:
|
2019-09-13 11:06:41 +02:00
|
|
|
r_serv_term.sadd('tracker:tags:{}'.format(term_uuid), escape(tag) )
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
# create term tags mail notification list
|
|
|
|
for mail in mails:
|
2019-09-13 11:06:41 +02:00
|
|
|
r_serv_term.sadd('tracker:mail:{}'.format(term_uuid), escape(mail) )
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-09 14:20:13 +02:00
|
|
|
# toggle refresh module tracker list/set
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.set('tracker:refresh:{}'.format(term_type), time.time())
|
2019-08-09 14:20:13 +02:00
|
|
|
|
2019-08-06 17:03:49 +02:00
|
|
|
return term_uuid
|
|
|
|
|
2019-08-09 15:22:57 +02:00
|
|
|
def parse_tracked_term_to_delete(dict_input, user_id):
|
2019-09-11 15:33:04 +02:00
|
|
|
term_uuid = dict_input.get("uuid", None)
|
2019-08-09 17:07:54 +02:00
|
|
|
res = check_term_uuid_valid_access(term_uuid, user_id)
|
|
|
|
if res:
|
|
|
|
return res
|
2019-08-09 15:22:57 +02:00
|
|
|
|
|
|
|
delete_term(term_uuid)
|
|
|
|
return ({"uuid": term_uuid}, 200)
|
|
|
|
|
2019-08-07 12:08:24 +02:00
|
|
|
def delete_term(term_uuid):
|
2019-09-11 15:33:04 +02:00
|
|
|
term = r_serv_term.hget('tracker:{}'.format(term_uuid), 'tracked')
|
|
|
|
term_type = r_serv_term.hget('tracker:{}'.format(term_uuid), 'type')
|
|
|
|
level = r_serv_term.hget('tracker:{}'.format(term_uuid), 'level')
|
|
|
|
r_serv_term.srem('all:tracker_uuid:{}:{}'.format(term_type, term), term_uuid)
|
2019-08-09 14:20:13 +02:00
|
|
|
# Term not tracked by other users
|
2019-09-11 15:33:04 +02:00
|
|
|
if not r_serv_term.exists('all:tracker_uuid:{}:{}'.format(term_type, term)):
|
|
|
|
r_serv_term.srem('all:tracker:{}'.format(term_type), term)
|
2019-08-07 12:08:24 +02:00
|
|
|
|
2019-08-09 14:20:13 +02:00
|
|
|
# toggle refresh module tracker list/set
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.set('tracker:refresh:{}'.format(term_type), time.time())
|
2019-08-07 12:08:24 +02:00
|
|
|
|
2019-09-11 15:33:04 +02:00
|
|
|
if level == '0': # user only
|
|
|
|
user_id = term_type = r_serv_term.hget('tracker:{}'.format(term_uuid), 'user_id')
|
|
|
|
r_serv_term.srem('user:tracker:{}'.format(user_id), term_uuid)
|
|
|
|
r_serv_term.srem('user:tracker:{}:{}'.format(user_id, term_type), term_uuid)
|
|
|
|
elif level == '1': # global
|
|
|
|
r_serv_term.srem('global:tracker', term_uuid)
|
|
|
|
r_serv_term.srem('global:tracker:{}'.format(term_type), term_uuid)
|
2019-08-07 12:08:24 +02:00
|
|
|
|
|
|
|
# delete metatadata
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.delete('tracker:{}'.format(term_uuid))
|
2019-08-07 12:08:24 +02:00
|
|
|
|
|
|
|
# remove tags
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.delete('tracker:tags:{}'.format(term_uuid))
|
2019-08-07 12:08:24 +02:00
|
|
|
|
|
|
|
# remove mails
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.delete('tracker:mail:{}'.format(term_uuid))
|
2019-08-07 12:08:24 +02:00
|
|
|
|
|
|
|
# remove item set
|
2019-09-11 15:33:04 +02:00
|
|
|
all_item_date = r_serv_term.zrange('tracker:stat:{}'.format(term_uuid), 0, -1)
|
2019-08-09 15:22:57 +02:00
|
|
|
for date in all_item_date:
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.delete('tracker:item:{}:{}'.format(term_uuid, date))
|
|
|
|
r_serv_term.delete('tracker:stat:{}'.format(term_uuid))
|
2019-08-07 12:08:24 +02:00
|
|
|
|
2020-08-12 09:28:36 +02:00
|
|
|
if term_type == 'yara':
|
2020-12-08 16:47:55 +01:00
|
|
|
# delete custom rule
|
|
|
|
if not Tracker.is_default_yara_rule(term):
|
|
|
|
filepath = Tracker.get_yara_rule_file_by_tracker_name(term)
|
|
|
|
if filepath:
|
|
|
|
os.remove(filepath)
|
2020-08-12 09:28:36 +02:00
|
|
|
|
2019-09-13 16:33:34 +02:00
|
|
|
def replace_tracker_description(term_uuid, description):
|
|
|
|
description = escape(description)
|
|
|
|
r_serv_term.hset('tracker:{}'.format(term_uuid), 'description', description)
|
|
|
|
|
2019-08-14 09:44:49 +02:00
|
|
|
def replace_tracked_term_tags(term_uuid, tags):
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.delete('tracker:tags:{}'.format(term_uuid))
|
2019-08-14 09:44:49 +02:00
|
|
|
for tag in tags:
|
2019-09-13 16:33:34 +02:00
|
|
|
tag = escape(tag)
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.sadd('tracker:tags:{}'.format(term_uuid), tag)
|
2019-08-14 09:44:49 +02:00
|
|
|
|
|
|
|
def replace_tracked_term_mails(term_uuid, mails):
|
|
|
|
res = verify_mail_list(mails)
|
|
|
|
if res:
|
|
|
|
return res
|
|
|
|
else:
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.delete('tracker:mail:{}'.format(term_uuid))
|
2019-08-14 09:44:49 +02:00
|
|
|
for mail in mails:
|
2019-09-13 16:33:34 +02:00
|
|
|
mail = escape(mail)
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.sadd('tracker:mail:{}'.format(term_uuid), mail)
|
2019-08-14 09:44:49 +02:00
|
|
|
|
2019-08-09 14:20:13 +02:00
|
|
|
def get_term_uuid_list(term, term_type):
|
2019-09-11 15:33:04 +02:00
|
|
|
return list(r_serv_term.smembers('all:tracker_uuid:{}:{}'.format(term_type, term)))
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-07 12:08:24 +02:00
|
|
|
def get_term_tags(term_uuid):
|
2019-09-11 15:33:04 +02:00
|
|
|
return list(r_serv_term.smembers('tracker:tags:{}'.format(term_uuid)))
|
2019-08-07 12:08:24 +02:00
|
|
|
|
|
|
|
def get_term_mails(term_uuid):
|
2019-09-11 15:33:04 +02:00
|
|
|
return list(r_serv_term.smembers('tracker:mail:{}'.format(term_uuid)))
|
2019-08-07 12:08:24 +02:00
|
|
|
|
2019-08-09 14:20:13 +02:00
|
|
|
def add_tracked_item(term_uuid, item_id, item_date):
|
|
|
|
# track item
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.sadd('tracker:item:{}:{}'.format(term_uuid, item_date), item_id)
|
2019-08-09 14:20:13 +02:00
|
|
|
# track nb item by date
|
2019-09-11 15:33:04 +02:00
|
|
|
r_serv_term.zadd('tracker:stat:{}'.format(term_uuid), item_date, int(item_date))
|
2019-08-09 14:20:13 +02:00
|
|
|
|
|
|
|
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_total_by_day:{}'.format(item_date), word, nb)
|
|
|
|
r_serv_term.sadd('stat_token_history', item_date)
|
|
|
|
|
|
|
|
def delete_token_statistics_by_date(item_date):
|
|
|
|
r_serv_term.delete('stat_token_per_item_by_day:{}'.format(item_date))
|
|
|
|
r_serv_term.delete('stat_token_total_by_day:{}'.format(item_date))
|
|
|
|
r_serv_term.srem('stat_token_history', item_date)
|
|
|
|
|
|
|
|
def get_all_token_stat_history():
|
|
|
|
return r_serv_term.smembers('stat_token_history')
|
|
|
|
|
|
|
|
def get_tracked_term_last_updated_by_type(term_type):
|
2019-09-11 15:33:04 +02:00
|
|
|
epoch_update = r_serv_term.get('tracker:refresh:{}'.format(term_type))
|
2019-08-09 14:20:13 +02:00
|
|
|
if not epoch_update:
|
|
|
|
epoch_update = 0
|
|
|
|
return float(epoch_update)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-09 17:07:54 +02:00
|
|
|
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:
|
2019-08-14 09:44:49 +02:00
|
|
|
date_from = get_tracked_term_first_seen(term_uuid)
|
2019-08-09 17:07:54 +02:00
|
|
|
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
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-09 17:07:54 +02:00
|
|
|
all_item_id = get_tracked_term_list_item(term_uuid, date_from, date_to)
|
2019-09-11 15:33:04 +02:00
|
|
|
all_item_id = Item.get_item_list_desc(all_item_id)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-09 17:07:54 +02:00
|
|
|
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)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-14 09:44:49 +02:00
|
|
|
def get_tracked_term_first_seen(term_uuid):
|
2019-09-11 15:33:04 +02:00
|
|
|
res = r_serv_term.zrange('tracker:stat:{}'.format(term_uuid), 0, 0)
|
2019-08-14 09:44:49 +02:00
|
|
|
if res:
|
|
|
|
return res[0]
|
|
|
|
else:
|
|
|
|
return None
|
2019-08-06 17:03:49 +02:00
|
|
|
|
|
|
|
|
2019-08-14 09:44:49 +02:00
|
|
|
def get_tracked_term_last_seen(term_uuid):
|
2019-09-11 15:33:04 +02:00
|
|
|
res = r_serv_term.zrevrange('tracker:stat:{}'.format(term_uuid), 0, 0)
|
2019-08-14 09:44:49 +02:00
|
|
|
if res:
|
|
|
|
return res[0]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2019-09-13 16:33:34 +02:00
|
|
|
def get_term_metedata(term_uuid, user_id=False, description=False, level=False, tags=False, mails=False, sparkline=False):
|
2019-08-14 09:44:49 +02:00
|
|
|
dict_uuid = {}
|
2019-09-11 15:33:04 +02:00
|
|
|
dict_uuid['term'] = r_serv_term.hget('tracker:{}'.format(term_uuid), 'tracked')
|
|
|
|
dict_uuid['type'] = r_serv_term.hget('tracker:{}'.format(term_uuid), 'type')
|
|
|
|
dict_uuid['date'] = r_serv_term.hget('tracker:{}'.format(term_uuid), 'date')
|
2019-09-13 16:33:34 +02:00
|
|
|
dict_uuid['description'] = r_serv_term.hget('tracker:{}'.format(term_uuid), 'description')
|
2019-08-14 09:44:49 +02:00
|
|
|
dict_uuid['first_seen'] = get_tracked_term_first_seen(term_uuid)
|
|
|
|
dict_uuid['last_seen'] = get_tracked_term_last_seen(term_uuid)
|
|
|
|
if user_id:
|
2019-09-11 15:33:04 +02:00
|
|
|
dict_uuid['user_id'] = r_serv_term.hget('tracker:{}'.format(term_uuid), 'user_id')
|
2019-08-14 09:44:49 +02:00
|
|
|
if level:
|
2019-09-11 15:33:04 +02:00
|
|
|
dict_uuid['level'] = r_serv_term.hget('tracker:{}'.format(term_uuid), 'level')
|
2019-08-14 09:44:49 +02:00
|
|
|
if mails:
|
|
|
|
dict_uuid['mails'] = get_list_trackeed_term_mails(term_uuid)
|
|
|
|
if tags:
|
|
|
|
dict_uuid['tags'] = get_list_trackeed_term_tags(term_uuid)
|
|
|
|
if sparkline:
|
|
|
|
dict_uuid['sparkline'] = get_tracked_term_sparkline(term_uuid)
|
|
|
|
dict_uuid['uuid'] = term_uuid
|
|
|
|
return dict_uuid
|
|
|
|
|
2019-09-11 15:33:04 +02:00
|
|
|
def get_tracked_term_sparkline(tracker_uuid, num_day=6):
|
2019-08-14 09:44:49 +02:00
|
|
|
date_range_sparkline = Date.get_date_range(num_day)
|
|
|
|
sparklines_value = []
|
|
|
|
for date_day in date_range_sparkline:
|
2019-09-11 15:33:04 +02:00
|
|
|
nb_seen_this_day = r_serv_term.scard('tracker:item:{}:{}'.format(tracker_uuid, date_day))
|
2019-08-14 09:44:49 +02:00
|
|
|
if nb_seen_this_day is None:
|
|
|
|
nb_seen_this_day = 0
|
|
|
|
sparklines_value.append(int(nb_seen_this_day))
|
|
|
|
return sparklines_value
|
|
|
|
|
2019-09-11 15:33:04 +02:00
|
|
|
def get_list_tracked_term_stats_by_day(list_tracker_uuid, num_day=31, date_from=None, date_to=None):
|
|
|
|
if date_from and date_to:
|
|
|
|
date_range = Date.substract_date(date_from, date_to)
|
|
|
|
else:
|
|
|
|
date_range = Date.get_date_range(num_day)
|
|
|
|
list_tracker_stats = []
|
|
|
|
for tracker_uuid in list_tracker_uuid:
|
|
|
|
dict_tracker_data = []
|
|
|
|
tracker = r_serv_term.hget('tracker:{}'.format(tracker_uuid), 'tracked')
|
|
|
|
for date_day in date_range:
|
|
|
|
nb_seen_this_day = r_serv_term.scard('tracker:item:{}:{}'.format(tracker_uuid, date_day))
|
|
|
|
if nb_seen_this_day is None:
|
|
|
|
nb_seen_this_day = 0
|
|
|
|
dict_tracker_data.append({"date": date_day,"value": int(nb_seen_this_day)})
|
|
|
|
list_tracker_stats.append({"name": tracker,"Data": dict_tracker_data})
|
|
|
|
return list_tracker_stats
|
|
|
|
|
2019-08-14 09:44:49 +02:00
|
|
|
def get_list_trackeed_term_tags(term_uuid):
|
2019-09-11 15:33:04 +02:00
|
|
|
res = r_serv_term.smembers('tracker:tags:{}'.format(term_uuid))
|
2019-08-14 09:44:49 +02:00
|
|
|
if res:
|
|
|
|
return list(res)
|
|
|
|
else:
|
|
|
|
return []
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-14 09:44:49 +02:00
|
|
|
def get_list_trackeed_term_mails(term_uuid):
|
2019-09-11 15:33:04 +02:00
|
|
|
res = r_serv_term.smembers('tracker:mail:{}'.format(term_uuid))
|
2019-08-14 09:44:49 +02:00
|
|
|
if res:
|
|
|
|
return list(res)
|
|
|
|
else:
|
|
|
|
return []
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-09-11 15:33:04 +02:00
|
|
|
def get_user_tracked_term_uuid(user_id, filter_type=None):
|
|
|
|
if filter_type:
|
|
|
|
return list(r_serv_term.smembers('user:tracker:{}:{}'.format(user_id,filter_type)))
|
|
|
|
else:
|
|
|
|
return list(r_serv_term.smembers('user:tracker:{}'.format(user_id)))
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-09-11 15:33:04 +02:00
|
|
|
def get_global_tracked_term_uuid(filter_type=None):
|
|
|
|
if filter_type:
|
|
|
|
return list(r_serv_term.smembers('global:tracker:{}'.format(filter_type)))
|
|
|
|
else:
|
|
|
|
return list(r_serv_term.smembers('global:tracker'))
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-09-11 15:33:04 +02:00
|
|
|
def get_all_user_tracked_terms(user_id, filter_type=None):
|
2019-08-14 09:44:49 +02:00
|
|
|
all_user_term = []
|
2019-09-11 15:33:04 +02:00
|
|
|
all_user_term_uuid = get_user_tracked_term_uuid(user_id, filter_type=filter_type)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-14 09:44:49 +02:00
|
|
|
for term_uuid in all_user_term_uuid:
|
2019-09-11 15:33:04 +02:00
|
|
|
all_user_term.append(get_term_metedata(term_uuid, tags=True, mails=True, sparkline=True))
|
2019-08-14 09:44:49 +02:00
|
|
|
return all_user_term
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-09-11 15:33:04 +02:00
|
|
|
def get_all_global_tracked_terms(filter_type=None):
|
2019-08-14 09:44:49 +02:00
|
|
|
all_user_term = []
|
2019-09-11 15:33:04 +02:00
|
|
|
all_user_term_uuid = get_global_tracked_term_uuid(filter_type=filter_type)
|
2019-08-06 17:03:49 +02:00
|
|
|
|
2019-08-14 09:44:49 +02:00
|
|
|
for term_uuid in all_user_term_uuid:
|
2019-09-11 15:33:04 +02:00
|
|
|
all_user_term.append(get_term_metedata(term_uuid, user_id=True, tags=True, mails=True, sparkline=True))
|
2019-08-14 09:44:49 +02:00
|
|
|
return all_user_term
|