update: consider only one login of user per hour

pull/8/head
Sami Mokaddem 2017-11-17 11:47:20 +01:00
parent 445c56b07f
commit 1da5cc61ea
2 changed files with 23 additions and 2 deletions

View File

@ -10,12 +10,30 @@ class Users_helper:
self.serv_redis_db = serv_redis_db
self.cfg = cfg
def addTemporary(self, org, timestamp):
timestampDate = datetime.datetime.fromtimestamp(float(timestamp))
timestampDate_str = util.getDateHoursStrFormat(timestampDate)
keyname_timestamp = "{}:{}".format('LOGIN_TIMESTAMPSET', timestampDate_str)
self.serv_redis_db.sadd(keyname_timestamp, org)
self.serv_redis_db.expire(keyname_timestamp, 60*60)
def hasAlreadyBeenAdded(self, org, timestamp):
timestampDate = datetime.datetime.fromtimestamp(float(timestamp))
timestampDate_str = util.getDateHoursStrFormat(timestampDate)
keyname_timestamp = "{}:{}".format('LOGIN_TIMESTAMPSET', timestampDate_str)
orgs = [ org.decode('utf8') for org in self.serv_redis_db.smembers(keyname_timestamp) ]
if orgs is None:
return False
return (org in orgs)
def add_user_login(self, timestamp, org):
timestampDate = datetime.datetime.fromtimestamp(float(timestamp))
timestampDate_str = util.getDateStrFormat(timestampDate)
if not self.hasAlreadyBeenAdded(org, timestamp):
keyname_timestamp = "{}:{}".format('LOGIN_TIMESTAMP', timestampDate_str)
self.serv_redis_db.sadd(keyname_timestamp, timestamp)
self.addTemporary(org, timestamp)
keyname_org = "{}:{}".format('LOGIN_ORG', timestampDate_str)
self.serv_redis_db.zincrby(keyname_org, org, 1)

View File

@ -38,5 +38,8 @@ def getXPrevHoursSpan(date, hours):
def getDateStrFormat(date):
return str(date.year)+str(date.month).zfill(2)+str(date.day).zfill(2)
def getDateHoursStrFormat(date):
return getDateStrFormat(date)+str(date.hour)
def getTimestamp(date):
return int(time.mktime(date.timetuple()))