From 1da5cc61ea85c8d9da33ee3313a26f5dcb05af4d Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Fri, 17 Nov 2017 11:47:20 +0100 Subject: [PATCH] update: consider only one login of user per hour --- users_helper.py | 22 ++++++++++++++++++++-- util.py | 3 +++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/users_helper.py b/users_helper.py index 3a8021e..18942b3 100644 --- a/users_helper.py +++ b/users_helper.py @@ -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) - keyname_timestamp = "{}:{}".format('LOGIN_TIMESTAMP', timestampDate_str) - self.serv_redis_db.sadd(keyname_timestamp, timestamp) + 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) diff --git a/util.py b/util.py index 535480e..1b79a40 100644 --- a/util.py +++ b/util.py @@ -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()))