2017-11-15 09:36:44 +01:00
|
|
|
import math, random
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import datetime, time
|
2017-12-04 16:44:44 +01:00
|
|
|
import logging
|
2017-11-15 09:36:44 +01:00
|
|
|
|
|
|
|
import util
|
2017-12-05 09:56:32 +01:00
|
|
|
from . import contributor_helper
|
2017-11-15 09:36:44 +01:00
|
|
|
|
2017-12-04 16:44:44 +01:00
|
|
|
|
2017-11-15 09:36:44 +01:00
|
|
|
class Users_helper:
|
|
|
|
def __init__(self, serv_redis_db, cfg):
|
|
|
|
self.serv_redis_db = serv_redis_db
|
|
|
|
self.cfg = cfg
|
2017-11-29 16:54:09 +01:00
|
|
|
# REDIS keys
|
2017-12-05 16:04:28 +01:00
|
|
|
self.keyTimestamp = "LOGIN_TIMESTAMP"
|
|
|
|
self.keyOrgLog = "LOGIN_ORG"
|
|
|
|
self.keyContribDay = contributor_helper.KEYDAY # Key to get monthly contribution
|
|
|
|
self.keyAllOrgLog = "LOGIN_ALL_ORG" # Key to get all organisation that logged in
|
2017-11-15 09:36:44 +01:00
|
|
|
|
2017-12-05 10:23:40 +01:00
|
|
|
#logger
|
|
|
|
logDir = cfg.get('Log', 'directory')
|
|
|
|
logfilename = cfg.get('Log', 'filename')
|
|
|
|
logPath = os.path.join(logDir, logfilename)
|
|
|
|
if not os.path.exists(logDir):
|
|
|
|
os.makedirs(logDir)
|
2017-12-05 10:32:12 +01:00
|
|
|
logging.basicConfig(filename=logPath, filemode='a', level=logging.INFO)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
|
2017-11-15 13:45:01 +01:00
|
|
|
def add_user_login(self, timestamp, org):
|
2017-11-15 09:36:44 +01:00
|
|
|
timestampDate = datetime.datetime.fromtimestamp(float(timestamp))
|
|
|
|
timestampDate_str = util.getDateStrFormat(timestampDate)
|
2017-11-15 13:45:01 +01:00
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
keyname_timestamp = "{}:{}".format(self.keyTimestamp, org)
|
|
|
|
self.serv_redis_db.zadd(keyname_timestamp, timestamp, timestamp)
|
|
|
|
self.logger.debug('Added to redis: keyname={}, org={}'.format(keyname_timestamp, timestamp))
|
2017-11-15 13:45:01 +01:00
|
|
|
|
2017-11-29 16:54:09 +01:00
|
|
|
keyname_org = "{}:{}".format(self.keyOrgLog, timestampDate_str)
|
2017-11-15 13:45:01 +01:00
|
|
|
self.serv_redis_db.zincrby(keyname_org, org, 1)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Added to redis: keyname={}, org={}'.format(keyname_org, org))
|
2017-11-15 09:36:44 +01:00
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
self.serv_redis_db.sadd(self.keyAllOrgLog, org)
|
|
|
|
self.logger.debug('Added to redis: keyname={}, org={}'.format(self.keyAllOrgLog, org))
|
|
|
|
|
|
|
|
def getAllOrg(self):
|
|
|
|
temp = self.serv_redis_db.smembers(self.keyAllOrgLog)
|
|
|
|
return [ org.decode('utf8') for org in temp ]
|
|
|
|
|
|
|
|
# return: All timestamps for one org for the spanned time or not
|
|
|
|
def getDates(self, org, date=None):
|
|
|
|
keyname = "{}:{}".format(self.keyTimestamp, org)
|
|
|
|
timestamps = self.serv_redis_db.zrange(keyname, 0, -1, desc=True, withscores=True)
|
|
|
|
if date is None:
|
2017-12-07 13:03:52 +01:00
|
|
|
to_return = [ datetime.datetime.fromtimestamp(float(t[1])) for t in timestamps ]
|
2017-12-05 16:04:28 +01:00
|
|
|
else:
|
|
|
|
to_return = []
|
|
|
|
for t in timestamps:
|
|
|
|
t = datetime.datetime.fromtimestamp(float(t[1]))
|
|
|
|
if util.getDateStrFormat(t) == util.getDateStrFormat(date): #same day
|
|
|
|
to_return.append(t)
|
2017-12-07 09:17:22 +01:00
|
|
|
elif util.getDateStrFormat(t) > util.getDateStrFormat(date):
|
|
|
|
continue # timestamps should be sorted, skipping to reach wanted date
|
2017-12-05 16:04:28 +01:00
|
|
|
else:
|
|
|
|
break # timestamps should be sorted, no need to process anymore
|
|
|
|
return to_return
|
|
|
|
|
|
|
|
|
|
|
|
# return: All dates for all orgs, if date is not supplied, return for all dates
|
|
|
|
def getUserLogins(self, date=None):
|
|
|
|
# get all orgs and retreive their timestamps
|
2017-12-05 16:31:08 +01:00
|
|
|
dates = []
|
2017-12-05 16:04:28 +01:00
|
|
|
for org in self.getAllOrg():
|
|
|
|
keyname = "{}:{}".format(self.keyOrgLog, org)
|
2017-12-05 16:31:08 +01:00
|
|
|
dates += self.getDates(org, date)
|
|
|
|
return dates
|
2017-11-15 09:36:44 +01:00
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
# return: All orgs that logged in for the time spanned
|
2017-11-15 15:40:28 +01:00
|
|
|
def getAllLoggedInOrgs(self, date, prev_days=31):
|
|
|
|
orgs = set()
|
|
|
|
for curDate in util.getXPrevDaysSpan(date, prev_days):
|
2017-11-29 16:54:09 +01:00
|
|
|
keyname = "{}:{}".format(self.keyOrgLog, util.getDateStrFormat(curDate))
|
2017-12-05 16:04:28 +01:00
|
|
|
data = self.serv_redis_db.zrange(keyname, 0, -1, desc=True)
|
2017-11-15 15:40:28 +01:00
|
|
|
for org in data:
|
2017-12-05 16:04:28 +01:00
|
|
|
orgs.add(org.decode('utf8'))
|
2017-11-15 15:40:28 +01:00
|
|
|
return list(orgs)
|
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
# return: list composed of the number of [log, contrib] for one org for the time spanned
|
2017-11-15 15:40:28 +01:00
|
|
|
def getOrgContribAndLogin(self, date, org, prev_days=31):
|
2017-11-29 16:54:09 +01:00
|
|
|
keyname_log = "{}:{}"
|
|
|
|
keyname_contrib = "{}:{}"
|
2017-11-15 15:40:28 +01:00
|
|
|
data = []
|
|
|
|
for curDate in util.getXPrevDaysSpan(date, prev_days):
|
2017-11-29 16:54:09 +01:00
|
|
|
log = self.serv_redis_db.zscore(keyname_log.format(self.keyOrgLog, util.getDateStrFormat(curDate)), org)
|
2017-11-15 15:40:28 +01:00
|
|
|
log = 0 if log is None else 1
|
2017-11-30 16:04:03 +01:00
|
|
|
contrib = self.serv_redis_db.zscore(keyname_contrib.format(self.keyContribDay, util.getDateStrFormat(curDate)), org)
|
2017-12-05 16:31:08 +01:00
|
|
|
contrib = 0 if contrib is None else contrib
|
2017-11-15 15:40:28 +01:00
|
|
|
data.append([log, contrib])
|
|
|
|
return data
|
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
# return: the computed ratio of contribution/login for a given array
|
2017-11-15 15:40:28 +01:00
|
|
|
def getContribOverLoginScore(self, array):
|
|
|
|
totLog = 0
|
|
|
|
totContrib = 0
|
|
|
|
for log, contrib in array:
|
|
|
|
totLog += log
|
|
|
|
totContrib += contrib
|
|
|
|
if totLog == 0: # avoid div by 0
|
|
|
|
totLog = 1
|
|
|
|
return totContrib/totLog
|
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
# return: list of org having the greatest ContribOverLoginScore for the time spanned
|
2017-11-24 16:46:46 +01:00
|
|
|
def getTopOrglogin(self, date, maxNum=12, prev_days=7):
|
2017-11-15 15:40:28 +01:00
|
|
|
all_logged_in_orgs = self.getAllLoggedInOrgs(date, prev_days)
|
|
|
|
data = []
|
|
|
|
for org in all_logged_in_orgs:
|
|
|
|
orgStatus = self.getOrgContribAndLogin(date, org, prev_days)
|
|
|
|
orgScore = self.getContribOverLoginScore(orgStatus)
|
|
|
|
data.append([org, orgScore])
|
|
|
|
data.sort(key=lambda x: x[1], reverse=True)
|
|
|
|
return data[:maxNum]
|
|
|
|
|
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
# return: array composed of [number of org that contributed, number of org that logged in without contribution]
|
|
|
|
# for the spanned time
|
2017-11-15 14:34:37 +01:00
|
|
|
def getLoginVSCOntribution(self, date):
|
2017-11-30 16:04:03 +01:00
|
|
|
keyname = "{}:{}".format(self.keyContribDay, util.getDateStrFormat(date))
|
2017-11-15 14:34:37 +01:00
|
|
|
orgs_contri = self.serv_redis_db.zrange(keyname, 0, -1, desc=True, withscores=False)
|
|
|
|
orgs_contri = [ org.decode('utf8') for org in orgs_contri ]
|
2017-12-05 16:04:28 +01:00
|
|
|
orgs_login = [ org for org in self.getAllLoggedInOrgs(date, prev_days=0) ]
|
2017-11-15 14:34:37 +01:00
|
|
|
contributed_num = 0
|
|
|
|
non_contributed_num = 0
|
|
|
|
for org in orgs_login:
|
|
|
|
if org in orgs_contri:
|
|
|
|
contributed_num += 1
|
|
|
|
else:
|
|
|
|
non_contributed_num +=1
|
|
|
|
return [contributed_num, non_contributed_num]
|
|
|
|
|
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
# return: list of day where day is a list of the number of time users logged in during an hour
|
|
|
|
def getUserLoginsForPunchCard(self, date, org=None, prev_days=6):
|
2017-11-15 09:36:44 +01:00
|
|
|
week = {}
|
|
|
|
for curDate in util.getXPrevDaysSpan(date, prev_days):
|
2017-12-05 16:04:28 +01:00
|
|
|
if org is None:
|
|
|
|
dates = self.getUserLogins(curDate)
|
|
|
|
else:
|
|
|
|
dates = self.getDates(org, date=curDate)
|
2017-11-15 09:36:44 +01:00
|
|
|
day = {}
|
2017-12-05 16:04:28 +01:00
|
|
|
for date in dates:
|
2017-11-15 09:36:44 +01:00
|
|
|
if date.hour not in day:
|
|
|
|
day[date.hour] = 0
|
|
|
|
day[date.hour] += 1
|
|
|
|
week[curDate.weekday()] = day
|
|
|
|
|
|
|
|
# Format data
|
|
|
|
data = []
|
|
|
|
for d in range(7):
|
|
|
|
try:
|
|
|
|
to_append = []
|
|
|
|
for h in range(24):
|
|
|
|
try:
|
|
|
|
to_append.append(week[d][h])
|
|
|
|
except KeyError:
|
|
|
|
to_append.append(0)
|
2017-11-17 10:06:43 +01:00
|
|
|
data.append(to_append)
|
2017-11-15 09:36:44 +01:00
|
|
|
except KeyError: # no data
|
|
|
|
data.append([0 for x in range(24)])
|
2017-12-07 13:03:52 +01:00
|
|
|
# swap: punchcard day starts on sunday
|
2017-11-15 13:45:01 +01:00
|
|
|
data = [data[6]]+data[:6]
|
2017-11-15 09:36:44 +01:00
|
|
|
return data
|
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
# return: a dico of the form {login: [[timestamp, count], ...], contrib: [[timestamp, 1/0], ...]}
|
|
|
|
# either for all orgs or the supplied one
|
|
|
|
def getUserLoginsAndContribOvertime(self, date, org=None, prev_days=6):
|
2017-11-21 11:59:07 +01:00
|
|
|
dico_hours_contrib = {}
|
2017-11-15 09:36:44 +01:00
|
|
|
dico_hours = {}
|
|
|
|
for curDate in util.getXPrevHoursSpan(date, prev_days*24):
|
|
|
|
dico_hours[util.getTimestamp(curDate)] = 0 # populate with empty data
|
2017-11-21 11:59:07 +01:00
|
|
|
dico_hours_contrib[util.getTimestamp(curDate)] = 0 # populate with empty data
|
2017-11-15 09:36:44 +01:00
|
|
|
|
|
|
|
for curDate in util.getXPrevDaysSpan(date, prev_days):
|
2017-12-05 16:04:28 +01:00
|
|
|
if org is None:
|
|
|
|
dates = self.getUserLogins(curDate)
|
|
|
|
else:
|
|
|
|
dates = self.getDates(org, date=curDate)
|
2017-11-30 16:04:03 +01:00
|
|
|
keyname = "{}:{}".format(self.keyContribDay, util.getDateStrFormat(curDate))
|
2017-11-21 11:59:07 +01:00
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
if org is None:
|
2017-12-05 16:31:08 +01:00
|
|
|
orgs_contri = self.serv_redis_db.zrange(keyname, 0, -1, desc=True, withscores=True)
|
|
|
|
orgs_contri_num = 0
|
2017-12-06 16:51:29 +01:00
|
|
|
for _, count in orgs_contri:
|
2017-12-05 16:31:08 +01:00
|
|
|
orgs_contri_num += count
|
2017-12-05 16:04:28 +01:00
|
|
|
else:
|
|
|
|
orgs_contri_num = self.serv_redis_db.zscore(keyname, org)
|
2017-12-05 16:31:08 +01:00
|
|
|
orgs_contri_num = orgs_contri_num if orgs_contri_num is not None else 0
|
2017-12-05 16:04:28 +01:00
|
|
|
|
2017-11-21 11:59:07 +01:00
|
|
|
for curDate in util.getHoursSpanOfDate(curDate, adaptToFitCurrentTime=True): #fill hole day
|
|
|
|
dico_hours_contrib[util.getTimestamp(curDate)] = orgs_contri_num
|
|
|
|
|
2017-12-05 16:04:28 +01:00
|
|
|
for d in dates: # sum occurence during the current hour
|
|
|
|
dateTimestamp = d.replace(minute=0, second=0, microsecond=0)
|
2017-11-21 09:26:58 +01:00
|
|
|
try:
|
|
|
|
dico_hours[util.getTimestamp(dateTimestamp)] += 1
|
|
|
|
except KeyError: # timestamp out of bound (greater than 1 week)
|
|
|
|
pass
|
2017-11-15 09:36:44 +01:00
|
|
|
|
|
|
|
# Format data
|
2017-11-21 11:59:07 +01:00
|
|
|
# login
|
|
|
|
to_ret = {}
|
2017-11-15 09:36:44 +01:00
|
|
|
data = []
|
2017-11-15 13:45:01 +01:00
|
|
|
for curDate, occ in dico_hours.items():
|
|
|
|
data.append([curDate, occ])
|
2017-11-15 09:36:44 +01:00
|
|
|
data.sort(key=lambda x: x[0])
|
2017-11-21 11:59:07 +01:00
|
|
|
to_ret['login'] = data
|
|
|
|
# contrib
|
|
|
|
data = []
|
|
|
|
for curDate, occ in dico_hours_contrib.items():
|
|
|
|
data.append([curDate, occ])
|
|
|
|
data.sort(key=lambda x: x[0])
|
|
|
|
to_ret['contrib'] = data
|
|
|
|
|
|
|
|
return to_ret
|