refact: moved redis keyname in the helper constructor. Permit to have

cleaner code and ease the DB management.
pull/12/head
Sami Mokaddem 2017-11-29 16:46:45 +01:00
parent 84f3aa6aa1
commit 16d281b634
1 changed files with 23 additions and 15 deletions

View File

@ -11,6 +11,14 @@ class Trendings_helper:
self.serv_redis_db = serv_redis_db self.serv_redis_db = serv_redis_db
self.cfg = cfg self.cfg = cfg
# REDIS keys
self.keyEvent = "TRENDINGS_EVENTS"
self.keyCateg = "TRENDINGS_CATEGS"
self.keyTag = "TRENDINGS_TAGS"
self.keyDisc = "TRENDINGS_DISC"
self.keySigh = "TRENDINGS_SIGHT_sightings"
self.keyFalse = "TRENDINGS_SIGHT_false_positive"
''' SETTER ''' ''' SETTER '''
def addGenericTrending(self, trendingType, data, timestamp): def addGenericTrending(self, trendingType, data, timestamp):
@ -24,13 +32,13 @@ class Trendings_helper:
self.serv_redis_db.zincrby(keyname, to_save, 1) self.serv_redis_db.zincrby(keyname, to_save, 1)
def addTrendingEvent(self, eventName, timestamp): def addTrendingEvent(self, eventName, timestamp):
self.addGenericTrending('TRENDINGS_EVENTS', eventName, timestamp) self.addGenericTrending(self.keyEvent, eventName, timestamp)
def addTrendingCateg(self, categName, timestamp): def addTrendingCateg(self, categName, timestamp):
self.addGenericTrending('TRENDINGS_CATEGS', categName, timestamp) self.addGenericTrending(self.keyCateg, categName, timestamp)
def addTrendingDisc(self, eventName, timestamp): def addTrendingDisc(self, eventName, timestamp):
self.addGenericTrending('TRENDINGS_DISC', eventName, timestamp) self.addGenericTrending(self.keyDisc, eventName, timestamp)
def addTrendingTags(self, tags, timestamp): def addTrendingTags(self, tags, timestamp):
for tag in tags: for tag in tags:
@ -38,18 +46,18 @@ class Trendings_helper:
ordDic['id'] = tag['id'] ordDic['id'] = tag['id']
ordDic['name'] = tag['name'] ordDic['name'] = tag['name']
ordDic['colour'] = tag['colour'] ordDic['colour'] = tag['colour']
self.addGenericTrending('TRENDINGS_TAGS', ordDic, timestamp) self.addGenericTrending(self.keyTag, ordDic, timestamp)
def addSightings(self, timestamp): def addSightings(self, timestamp):
timestampDate = datetime.datetime.fromtimestamp(float(timestamp)) timestampDate = datetime.datetime.fromtimestamp(float(timestamp))
timestampDate_str = util.getDateStrFormat(timestampDate) timestampDate_str = util.getDateStrFormat(timestampDate)
keyname = "{}:{}".format("TRENDINGS_SIGHT_sightings", timestampDate_str) keyname = "{}:{}".format(self.keySigh, timestampDate_str)
self.serv_redis_db.incrby(keyname, 1) self.serv_redis_db.incrby(keyname, 1)
def addFalsePositive(self, timestamp): def addFalsePositive(self, timestamp):
timestampDate = datetime.datetime.fromtimestamp(float(timestamp)) timestampDate = datetime.datetime.fromtimestamp(float(timestamp))
timestampDate_str = util.getDateStrFormat(timestampDate) timestampDate_str = util.getDateStrFormat(timestampDate)
keyname = "{}:{}".format("TRENDINGS_SIGHT_false_positive", timestampDate_str) keyname = "{}:{}".format(self.keyFalse, timestampDate_str)
self.serv_redis_db.incrby(keyname, 1) self.serv_redis_db.incrby(keyname, 1)
''' GETTER ''' ''' GETTER '''
@ -77,20 +85,20 @@ class Trendings_helper:
def getTrendingEvents(self, dateS, dateE, specificLabel=None): def getTrendingEvents(self, dateS, dateE, specificLabel=None):
if specificLabel is None: if specificLabel is None:
return self.getGenericTrending('TRENDINGS_EVENTS', dateS, dateE) return self.getGenericTrending(self.keyEvent, dateS, dateE)
else: else:
specificLabel = specificLabel.replace('\\n', '\n'); # reset correctly label with their \n (CR) instead of their char value specificLabel = specificLabel.replace('\\n', '\n'); # reset correctly label with their \n (CR) instead of their char value
return self.getSpecificTrending('TRENDINGS_EVENTS', dateS, dateE, specificLabel) return self.getSpecificTrending(self.keyEvent, dateS, dateE, specificLabel)
def getTrendingCategs(self, dateS, dateE): def getTrendingCategs(self, dateS, dateE):
return self.getGenericTrending('TRENDINGS_CATEGS', dateS, dateE) return self.getGenericTrending(self.keyCateg, dateS, dateE)
# FIXME: Construct this when getting data # FIXME: Construct this when getting data
def getTrendingTags(self, dateS, dateE, topNum=12): def getTrendingTags(self, dateS, dateE, topNum=12):
to_ret = [] to_ret = []
prev_days = (dateE - dateS).days prev_days = (dateE - dateS).days
for curDate in util.getXPrevDaysSpan(dateE, prev_days): for curDate in util.getXPrevDaysSpan(dateE, prev_days):
keyname = "{}:{}".format('TRENDINGS_TAGS', util.getDateStrFormat(curDate)) keyname = "{}:{}".format(self.keyTag, util.getDateStrFormat(curDate))
data = self.serv_redis_db.zrange(keyname, 0, topNum-1, desc=True, withscores=True) data = self.serv_redis_db.zrange(keyname, 0, topNum-1, desc=True, withscores=True)
data = [ [record[0].decode('utf8'), record[1]] for record in data ] data = [ [record[0].decode('utf8'), record[1]] for record in data ]
data = data if data is not None else [] data = data if data is not None else []
@ -105,21 +113,21 @@ class Trendings_helper:
to_ret = [] to_ret = []
prev_days = (dateE - dateS).days prev_days = (dateE - dateS).days
for curDate in util.getXPrevDaysSpan(dateE, prev_days): for curDate in util.getXPrevDaysSpan(dateE, prev_days):
keyname = "{}:{}".format("TRENDINGS_SIGHT_sightings", util.getDateStrFormat(curDate)) keyname = "{}:{}".format(self.keySigh, util.getDateStrFormat(curDate))
sight = self.serv_redis_db.get(keyname) sight = self.serv_redis_db.get(keyname)
sight = 0 if sight is None else int(sight.decode('utf8')) sight = 0 if sight is None else int(sight.decode('utf8'))
keyname = "{}:{}".format("TRENDINGS_SIGHT_false_positive", util.getDateStrFormat(curDate)) keyname = "{}:{}".format(self.keyFalse, util.getDateStrFormat(curDate))
fp = self.serv_redis_db.get(keyname) fp = self.serv_redis_db.get(keyname)
fp = 0 if fp is None else int(fp.decode('utf8')) fp = 0 if fp is None else int(fp.decode('utf8'))
to_ret.append([util.getTimestamp(curDate), { 'sightings': sight, 'false_positive': fp}]) to_ret.append([util.getTimestamp(curDate), { 'sightings': sight, 'false_positive': fp}])
return to_ret return to_ret
def getTrendingDisc(self, dateS, dateE): def getTrendingDisc(self, dateS, dateE):
return self.getGenericTrending('TRENDINGS_DISC', dateS, dateE) return self.getGenericTrending(self.keyDisc, dateS, dateE)
def getTypeaheadData(self, dateS, dateE): def getTypeaheadData(self, dateS, dateE):
to_ret = {} to_ret = {}
for trendingType in ['TRENDINGS_EVENTS', 'TRENDINGS_CATEGS']: for trendingType in [self.keyEvent, self.keyCateg]:
allSet = set() allSet = set()
prev_days = (dateE - dateS).days prev_days = (dateE - dateS).days
for curDate in util.getXPrevDaysSpan(dateE, prev_days): for curDate in util.getXPrevDaysSpan(dateE, prev_days):
@ -135,5 +143,5 @@ class Trendings_helper:
for tag in tagList: for tag in tagList:
tag = tag[0] tag = tag[0]
tagSet.add(tag['name']) tagSet.add(tag['name'])
to_ret['TRENDINGS_TAGS'] = list(tagSet) to_ret[self.keyTag] = list(tagSet)
return to_ret return to_ret