2019-09-12 11:25:29 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
|
2022-11-23 10:02:33 +00:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
from packages import Term
|
2019-09-12 11:25:29 +00:00
|
|
|
|
2022-11-23 10:02:33 +00:00
|
|
|
from lib import ConfigLoader
|
2019-11-05 14:18:03 +00:00
|
|
|
|
2019-09-12 11:25:29 +00:00
|
|
|
|
|
|
|
def rreplace(s, old, new, occurrence):
|
|
|
|
li = s.rsplit(old, occurrence)
|
|
|
|
return new.join(li)
|
|
|
|
|
2022-11-23 10:02:33 +00:00
|
|
|
def get_item_id(full_path):
|
|
|
|
return full_path.replace(PASTES_FOLDER, '', 1)
|
|
|
|
|
|
|
|
def get_item_date(id_item):
|
|
|
|
l_dir = id_item.split('/')
|
|
|
|
return f'{l_dir[-4]}{l_dir[-3]}{l_dir[-2]}'
|
|
|
|
|
2019-09-12 11:25:29 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
start_deb = time.time()
|
|
|
|
|
2019-11-05 14:18:03 +00:00
|
|
|
config_loader = ConfigLoader.ConfigLoader()
|
2022-11-23 10:02:33 +00:00
|
|
|
PASTES_FOLDER = os.path.join(os.environ['AIL_HOME'], config_loader.get_config_str("Directories", "pastes")) + '/'
|
|
|
|
PASTES_FOLDER = os.path.join(os.path.realpath(PASTES_FOLDER), '')
|
2019-11-05 14:18:03 +00:00
|
|
|
|
2022-11-23 10:02:33 +00:00
|
|
|
r_serv = config_loader.get_redis_conn("ARDB_DB")
|
2019-11-05 14:18:03 +00:00
|
|
|
r_serv_term_stats = config_loader.get_redis_conn("ARDB_Trending")
|
|
|
|
r_serv_termfreq = config_loader.get_redis_conn("ARDB_TermFreq")
|
|
|
|
config_loader = None
|
2019-09-12 11:25:29 +00:00
|
|
|
|
|
|
|
r_serv_term_stats.flushdb()
|
|
|
|
|
2022-11-23 10:02:33 +00:00
|
|
|
# convert all regex:
|
2019-09-12 11:25:29 +00:00
|
|
|
all_regex = r_serv_termfreq.smembers('TrackedRegexSet')
|
|
|
|
for regex in all_regex:
|
2022-11-23 10:02:33 +00:00
|
|
|
tags = list(r_serv_termfreq.smembers('TrackedNotificationTags_{}'.format(regex)))
|
|
|
|
mails = list(r_serv_termfreq.smembers('TrackedNotificationEmails_{}'.format(regex)))
|
2019-09-12 11:25:29 +00:00
|
|
|
|
|
|
|
new_term = regex[1:-1]
|
2022-11-23 10:02:33 +00:00
|
|
|
res = Term.parse_json_term_to_add({"term": new_term, "type": 'regex', "tags": tags, "mails": mails, "level": 1},
|
|
|
|
'admin@admin.test')
|
2019-09-12 11:25:29 +00:00
|
|
|
if res[1] == 200:
|
|
|
|
term_uuid = res[0]['uuid']
|
|
|
|
list_items = r_serv_termfreq.smembers('regex_{}'.format(regex))
|
|
|
|
for paste_item in list_items:
|
2022-11-23 10:02:33 +00:00
|
|
|
item_id = get_item_id(paste_item)
|
|
|
|
item_date = get_item_date(item_id)
|
2019-09-12 11:25:29 +00:00
|
|
|
Term.add_tracked_item(term_uuid, item_id, item_date)
|
|
|
|
|
|
|
|
# Invalid Tracker => remove it
|
|
|
|
else:
|
|
|
|
print('Invalid Regex Removed: {}'.format(regex))
|
|
|
|
print(res[0])
|
|
|
|
# allow reprocess
|
|
|
|
r_serv_termfreq.srem('TrackedRegexSet', regex)
|
|
|
|
|
|
|
|
all_tokens = r_serv_termfreq.smembers('TrackedSetTermSet')
|
|
|
|
for token in all_tokens:
|
2022-11-23 10:02:33 +00:00
|
|
|
tags = list(r_serv_termfreq.smembers('TrackedNotificationTags_{}'.format(token)))
|
|
|
|
mails = list(r_serv_termfreq.smembers('TrackedNotificationEmails_{}'.format(token)))
|
2019-09-12 11:25:29 +00:00
|
|
|
|
|
|
|
res = Term.parse_json_term_to_add({"term": token, "type": 'word', "tags": tags, "mails": mails, "level": 1}, 'admin@admin.test')
|
|
|
|
if res[1] == 200:
|
|
|
|
term_uuid = res[0]['uuid']
|
|
|
|
list_items = r_serv_termfreq.smembers('tracked_{}'.format(token))
|
|
|
|
for paste_item in list_items:
|
2022-11-23 10:02:33 +00:00
|
|
|
item_id = get_item_id(paste_item)
|
|
|
|
item_date = get_item_date(item_id)
|
2019-09-12 11:25:29 +00:00
|
|
|
Term.add_tracked_item(term_uuid, item_id, item_date)
|
|
|
|
# Invalid Tracker => remove it
|
|
|
|
else:
|
|
|
|
print('Invalid Token Removed: {}'.format(token))
|
|
|
|
print(res[0])
|
|
|
|
# allow reprocess
|
|
|
|
r_serv_termfreq.srem('TrackedSetTermSet', token)
|
|
|
|
|
|
|
|
all_set = r_serv_termfreq.smembers('TrackedSetSet')
|
|
|
|
for curr_set in all_set:
|
2022-11-23 10:02:33 +00:00
|
|
|
tags = list(r_serv_termfreq.smembers('TrackedNotificationTags_{}'.format(curr_set)))
|
|
|
|
mails = list(r_serv_termfreq.smembers('TrackedNotificationEmails_{}'.format(curr_set)))
|
2019-09-12 11:25:29 +00:00
|
|
|
|
|
|
|
to_remove = ',{}'.format(curr_set.split(',')[-1])
|
|
|
|
new_set = rreplace(curr_set, to_remove, '', 1)
|
|
|
|
new_set = new_set[2:]
|
|
|
|
new_set = new_set.replace(',', '')
|
|
|
|
|
|
|
|
res = Term.parse_json_term_to_add({"term": new_set, "type": 'set', "nb_words": 1, "tags": tags, "mails": mails, "level": 1}, 'admin@admin.test')
|
|
|
|
if res[1] == 200:
|
|
|
|
term_uuid = res[0]['uuid']
|
|
|
|
list_items = r_serv_termfreq.smembers('tracked_{}'.format(curr_set))
|
|
|
|
for paste_item in list_items:
|
2022-11-23 10:02:33 +00:00
|
|
|
item_id = get_item_id(paste_item)
|
|
|
|
item_date = get_item_date(item_id)
|
2019-09-12 11:25:29 +00:00
|
|
|
Term.add_tracked_item(term_uuid, item_id, item_date)
|
|
|
|
# Invalid Tracker => remove it
|
|
|
|
else:
|
|
|
|
print('Invalid Set Removed: {}'.format(curr_set))
|
|
|
|
print(res[0])
|
|
|
|
# allow reprocess
|
|
|
|
r_serv_termfreq.srem('TrackedSetSet', curr_set)
|
|
|
|
|
|
|
|
r_serv_termfreq.flushdb()
|
|
|
|
|
2022-11-23 10:02:33 +00:00
|
|
|
# Set current ail version
|
2019-09-12 11:25:29 +00:00
|
|
|
r_serv.set('ail:version', 'v2.2')
|
|
|
|
|
2022-11-23 10:02:33 +00:00
|
|
|
# Set current ail version
|
2019-09-12 11:25:29 +00:00
|
|
|
r_serv.hset('ail:update_date', 'v2.2', datetime.datetime.now().strftime("%Y%m%d"))
|