2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-08-22 13:35:49 +02:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
|
2016-08-22 20:59:56 +02:00
|
|
|
This module manage top sets for terms frequency.
|
|
|
|
Every 'refresh_rate' update the weekly and monthly set
|
2016-08-22 13:35:49 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import redis
|
|
|
|
import time
|
2016-11-25 11:54:16 +01:00
|
|
|
import datetime
|
2016-08-22 13:35:49 +02:00
|
|
|
import copy
|
|
|
|
from pubsublogger import publisher
|
|
|
|
from packages import lib_words
|
|
|
|
import datetime
|
|
|
|
import calendar
|
2016-08-24 17:28:39 +02:00
|
|
|
import os
|
2018-04-16 14:50:04 +02:00
|
|
|
import configparser
|
2016-08-22 13:35:49 +02:00
|
|
|
|
|
|
|
# Config Variables
|
2016-08-22 20:59:56 +02:00
|
|
|
Refresh_rate = 60*5 #sec
|
2016-08-22 13:35:49 +02:00
|
|
|
BlackListTermsSet_Name = "BlackListSetTermSet"
|
|
|
|
TrackedTermsSet_Name = "TrackedSetTermSet"
|
|
|
|
top_term_freq_max_set_cardinality = 20 # Max cardinality of the terms frequences set
|
|
|
|
oneDay = 60*60*24
|
|
|
|
num_day_month = 31
|
|
|
|
num_day_week = 7
|
|
|
|
|
|
|
|
top_termFreq_setName_day = ["TopTermFreq_set_day_", 1]
|
|
|
|
top_termFreq_setName_week = ["TopTermFreq_set_week", 7]
|
|
|
|
top_termFreq_setName_month = ["TopTermFreq_set_month", 31]
|
|
|
|
top_termFreq_set_array = [top_termFreq_setName_day,top_termFreq_setName_week, top_termFreq_setName_month]
|
|
|
|
|
|
|
|
|
|
|
|
def manage_top_set():
|
|
|
|
startDate = datetime.datetime.now()
|
|
|
|
startDate = startDate.replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
|
startDate = calendar.timegm(startDate.timetuple())
|
2016-12-08 09:13:31 +01:00
|
|
|
blacklist_size = int(server_term.scard(BlackListTermsSet_Name))
|
2016-08-22 13:35:49 +02:00
|
|
|
|
|
|
|
dico = {}
|
2017-02-15 16:29:02 +01:00
|
|
|
dico_per_paste = {}
|
2016-08-22 13:35:49 +02:00
|
|
|
|
2016-12-08 09:13:31 +01:00
|
|
|
# Retreive top data (max_card + blacklist_size) from days sets
|
2016-08-22 14:54:37 +02:00
|
|
|
for timestamp in range(startDate, startDate - top_termFreq_setName_month[1]*oneDay, -oneDay):
|
|
|
|
curr_set = top_termFreq_setName_day[0] + str(timestamp)
|
2016-12-08 09:13:31 +01:00
|
|
|
array_top_day = server_term.zrevrangebyscore(curr_set, '+inf', '-inf', withscores=True, start=0, num=top_term_freq_max_set_cardinality+blacklist_size)
|
2017-02-15 16:29:02 +01:00
|
|
|
array_top_day_per_paste = server_term.zrevrangebyscore("per_paste_" + curr_set, '+inf', '-inf', withscores=True, start=0, num=top_term_freq_max_set_cardinality+blacklist_size)
|
2016-08-22 13:35:49 +02:00
|
|
|
|
|
|
|
for word, value in array_top_day:
|
2016-08-22 16:01:42 +02:00
|
|
|
if word not in server_term.smembers(BlackListTermsSet_Name):
|
|
|
|
if word in dico.keys():
|
|
|
|
dico[word] += value
|
|
|
|
else:
|
|
|
|
dico[word] = value
|
2016-08-22 13:35:49 +02:00
|
|
|
|
2017-02-28 15:01:48 +01:00
|
|
|
for word, value in array_top_day_per_paste:
|
2017-02-15 16:29:02 +01:00
|
|
|
if word not in server_term.smembers(BlackListTermsSet_Name):
|
|
|
|
if word in dico_per_paste.keys():
|
|
|
|
dico_per_paste[word] += value
|
|
|
|
else:
|
|
|
|
dico_per_paste[word] = value
|
|
|
|
|
2016-08-22 13:35:49 +02:00
|
|
|
if timestamp == startDate - num_day_week*oneDay:
|
|
|
|
dico_week = copy.deepcopy(dico)
|
2017-02-15 16:29:02 +01:00
|
|
|
dico_week_per_paste = copy.deepcopy(dico_per_paste)
|
2016-08-22 13:35:49 +02:00
|
|
|
|
|
|
|
# convert dico into sorted array
|
|
|
|
array_month = []
|
2018-04-16 14:50:04 +02:00
|
|
|
for w, v in dico.items():
|
2016-08-22 13:35:49 +02:00
|
|
|
array_month.append((w, v))
|
|
|
|
array_month.sort(key=lambda tup: -tup[1])
|
2016-08-22 14:54:37 +02:00
|
|
|
array_month = array_month[0:20]
|
2016-08-22 13:35:49 +02:00
|
|
|
|
|
|
|
array_week = []
|
2018-04-16 14:50:04 +02:00
|
|
|
for w, v in dico_week.items():
|
2016-08-22 13:35:49 +02:00
|
|
|
array_week.append((w, v))
|
|
|
|
array_week.sort(key=lambda tup: -tup[1])
|
2016-08-22 14:54:37 +02:00
|
|
|
array_week = array_week[0:20]
|
2016-08-22 13:35:49 +02:00
|
|
|
|
2017-02-15 16:29:02 +01:00
|
|
|
# convert dico_per_paste into sorted array
|
|
|
|
array_month_per_paste = []
|
2018-04-16 14:50:04 +02:00
|
|
|
for w, v in dico_per_paste.items():
|
2017-02-15 16:29:02 +01:00
|
|
|
array_month_per_paste.append((w, v))
|
|
|
|
array_month_per_paste.sort(key=lambda tup: -tup[1])
|
|
|
|
array_month_per_paste = array_month_per_paste[0:20]
|
|
|
|
|
|
|
|
array_week_per_paste = []
|
2018-04-16 14:50:04 +02:00
|
|
|
for w, v in dico_week_per_paste.items():
|
2017-02-15 16:29:02 +01:00
|
|
|
array_week_per_paste.append((w, v))
|
|
|
|
array_week_per_paste.sort(key=lambda tup: -tup[1])
|
|
|
|
array_week_per_paste = array_week_per_paste[0:20]
|
|
|
|
|
|
|
|
|
2016-08-22 13:35:49 +02:00
|
|
|
# suppress every terms in top sets
|
|
|
|
for curr_set, curr_num_day in top_termFreq_set_array[1:3]:
|
|
|
|
for w in server_term.zrange(curr_set, 0, -1):
|
|
|
|
server_term.zrem(curr_set, w)
|
2017-02-15 16:29:02 +01:00
|
|
|
for w in server_term.zrange("per_paste_" + curr_set, 0, -1):
|
|
|
|
server_term.zrem("per_paste_" + curr_set, w)
|
2016-08-22 13:35:49 +02:00
|
|
|
|
|
|
|
# Add top term from sorted array in their respective sorted sets
|
|
|
|
for elem in array_week:
|
|
|
|
server_term.zadd(top_termFreq_setName_week[0], float(elem[1]), elem[0])
|
2017-02-15 16:29:02 +01:00
|
|
|
for elem in array_week_per_paste:
|
|
|
|
server_term.zadd("per_paste_" + top_termFreq_setName_week[0], float(elem[1]), elem[0])
|
2018-04-16 14:50:04 +02:00
|
|
|
|
2016-08-22 13:35:49 +02:00
|
|
|
for elem in array_month:
|
|
|
|
server_term.zadd(top_termFreq_setName_month[0], float(elem[1]), elem[0])
|
2017-02-15 16:29:02 +01:00
|
|
|
for elem in array_month_per_paste:
|
|
|
|
server_term.zadd("per_paste_" + top_termFreq_setName_month[0], float(elem[1]), elem[0])
|
2016-08-22 13:35:49 +02:00
|
|
|
|
2016-11-25 11:54:16 +01:00
|
|
|
timestamp = int(time.mktime(datetime.datetime.now().timetuple()))
|
|
|
|
value = str(timestamp) + ", " + "-"
|
|
|
|
r_temp.set("MODULE_"+ "CurveManageTopSets" + "_" + str(os.getpid()), value)
|
2018-04-16 14:50:04 +02:00
|
|
|
print("refreshed module")
|
2016-11-25 11:54:16 +01:00
|
|
|
|
2016-08-22 13:35:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# If you wish to use an other port of channel, do not forget to run a subscriber accordingly (see launch_logs.sh)
|
|
|
|
# Port of the redis instance used by pubsublogger
|
|
|
|
publisher.port = 6380
|
|
|
|
# Script is the default channel used for the modules.
|
|
|
|
publisher.channel = 'Script'
|
|
|
|
|
2016-08-24 15:21:41 +02:00
|
|
|
configfile = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg')
|
|
|
|
if not os.path.exists(configfile):
|
|
|
|
raise Exception('Unable to find the configuration file. \
|
|
|
|
Did you set environment variables? \
|
|
|
|
Or activate the virtualenv.')
|
2018-04-16 14:50:04 +02:00
|
|
|
|
|
|
|
cfg = configparser.ConfigParser()
|
2016-08-24 15:21:41 +02:00
|
|
|
cfg.read(configfile)
|
2016-08-22 13:35:49 +02:00
|
|
|
|
2016-11-25 11:54:16 +01:00
|
|
|
|
|
|
|
# For Module Manager
|
|
|
|
r_temp = redis.StrictRedis(
|
|
|
|
host=cfg.get('RedisPubSub', 'host'),
|
|
|
|
port=cfg.getint('RedisPubSub', 'port'),
|
2018-05-04 13:53:29 +02:00
|
|
|
db=cfg.getint('RedisPubSub', 'db'),
|
|
|
|
decode_responses=True)
|
2016-11-25 11:54:16 +01:00
|
|
|
|
|
|
|
timestamp = int(time.mktime(datetime.datetime.now().timetuple()))
|
|
|
|
value = str(timestamp) + ", " + "-"
|
|
|
|
r_temp.set("MODULE_"+ "CurveManageTopSets" + "_" + str(os.getpid()), value)
|
|
|
|
r_temp.sadd("MODULE_TYPE_"+ "CurveManageTopSets" , str(os.getpid()))
|
|
|
|
|
2016-08-22 13:35:49 +02:00
|
|
|
server_term = redis.StrictRedis(
|
2018-05-07 14:50:40 +02:00
|
|
|
host=cfg.get("ARDB_TermFreq", "host"),
|
|
|
|
port=cfg.getint("ARDB_TermFreq", "port"),
|
|
|
|
db=cfg.getint("ARDB_TermFreq", "db"),
|
2018-05-04 13:53:29 +02:00
|
|
|
decode_responses=True)
|
2016-08-22 13:35:49 +02:00
|
|
|
|
|
|
|
publisher.info("Script Curve_manage_top_set started")
|
|
|
|
|
|
|
|
# Sent to the logging a description of the module
|
|
|
|
publisher.info("Manage the top sets with the data created by the module curve.")
|
|
|
|
|
|
|
|
manage_top_set()
|
|
|
|
|
|
|
|
while True:
|
2016-08-22 20:59:56 +02:00
|
|
|
# Get one message from the input queue (module only work if linked with a queue)
|
2016-08-25 10:43:04 +02:00
|
|
|
time.sleep(Refresh_rate) # sleep a long time then manage the set
|
|
|
|
manage_top_set()
|