2017-11-06 10:42:51 +01:00
|
|
|
import util
|
2017-11-29 16:09:39 +01:00
|
|
|
from util import getZrange
|
2017-11-06 10:42:51 +01:00
|
|
|
import math, random
|
2017-12-01 15:39:17 +01:00
|
|
|
import time
|
2017-11-07 12:54:08 +01:00
|
|
|
import os
|
2017-11-06 10:42:51 +01:00
|
|
|
import configparser
|
|
|
|
import json
|
|
|
|
import datetime
|
2017-12-04 16:44:44 +01:00
|
|
|
import logging
|
2017-12-01 15:39:17 +01:00
|
|
|
import redis
|
|
|
|
|
|
|
|
import util
|
2017-12-05 09:56:32 +01:00
|
|
|
from . import users_helper
|
2017-12-01 15:39:17 +01:00
|
|
|
KEYDAY = "CONTRIB_DAY" # To be used by other module
|
2017-12-05 16:04:28 +01:00
|
|
|
KEYALLORG = "CONTRIB_ALL_ORG" # To be used by other module
|
2017-11-06 10:42:51 +01:00
|
|
|
|
|
|
|
class Contributor_helper:
|
|
|
|
def __init__(self, serv_redis_db, cfg):
|
|
|
|
self.serv_redis_db = serv_redis_db
|
2017-12-01 15:39:17 +01:00
|
|
|
self.serv_log = redis.StrictRedis(
|
|
|
|
host=cfg.get('RedisGlobal', 'host'),
|
|
|
|
port=cfg.getint('RedisGlobal', 'port'),
|
|
|
|
db=cfg.getint('RedisLog', 'db'))
|
2017-11-06 10:42:51 +01:00
|
|
|
self.cfg = cfg
|
2017-11-07 12:54:08 +01:00
|
|
|
self.cfg_org_rank = configparser.ConfigParser()
|
2018-03-31 12:43:50 +02:00
|
|
|
self.cfg_org_rank.read(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../config/ranking.cfg'))
|
2017-11-29 16:09:39 +01:00
|
|
|
self.CHANNEL_LASTAWARDS = cfg.get('RedisLog', 'channelLastAwards')
|
2017-12-01 15:39:17 +01:00
|
|
|
self.CHANNEL_LASTCONTRIB = cfg.get('RedisLog', 'channelLastContributor')
|
|
|
|
self.users_helper = users_helper.Users_helper(serv_redis_db, cfg)
|
2017-11-07 12:54:08 +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-08 11:29:07 +01:00
|
|
|
#honorBadge
|
2017-11-08 15:34:02 +01:00
|
|
|
self.honorBadgeNum = len(self.cfg_org_rank.options('HonorBadge'))
|
|
|
|
self.heavilyCount = self.cfg_org_rank.getint('rankRequirementsMisc', 'heavilyCount')
|
|
|
|
self.recentDays = self.cfg_org_rank.getint('rankRequirementsMisc', 'recentDays')
|
|
|
|
self.regularlyDays = self.cfg_org_rank.getint('rankRequirementsMisc', 'regularlyDays')
|
|
|
|
|
2017-11-08 11:29:07 +01:00
|
|
|
self.org_honor_badge_title = {}
|
2017-11-08 15:34:02 +01:00
|
|
|
for badgeNum in range(1, self.honorBadgeNum+1): #get Num of honorBadge
|
2017-11-08 11:29:07 +01:00
|
|
|
self.org_honor_badge_title[badgeNum] = self.cfg_org_rank.get('HonorBadge', str(badgeNum))
|
|
|
|
|
2017-11-24 15:09:34 +01:00
|
|
|
self.trophyMapping = json.loads(self.cfg_org_rank.get('TrophyDifficulty', 'trophyMapping'))
|
2018-09-25 14:48:13 +02:00
|
|
|
self.trophyMappingIncremental = [sum(self.trophyMapping[:i]) for i in range(len(self.trophyMapping)+1)]
|
2017-11-17 14:56:35 +01:00
|
|
|
self.trophyNum = len(self.cfg_org_rank.options('HonorTrophy'))-1 #0 is not a trophy
|
2017-11-13 12:57:05 +01:00
|
|
|
self.categories_in_trophy = json.loads(self.cfg_org_rank.get('HonorTrophyCateg', 'categ'))
|
|
|
|
self.trophy_title = {}
|
|
|
|
for trophyNum in range(0, len(self.cfg_org_rank.options('HonorTrophy'))): #get Num of trophy
|
|
|
|
self.trophy_title[trophyNum] = self.cfg_org_rank.get('HonorTrophy', str(trophyNum))
|
|
|
|
|
2017-11-08 11:29:07 +01:00
|
|
|
#GLOBAL RANKING
|
2017-11-07 12:54:08 +01:00
|
|
|
self.org_rank_maxLevel = self.cfg_org_rank.getint('rankTitle', 'maxLevel')
|
|
|
|
self.org_rank = {}
|
|
|
|
for rank in range(1, self.org_rank_maxLevel+1):
|
|
|
|
self.org_rank[rank] = self.cfg_org_rank.get('rankTitle', str(rank))
|
2017-11-08 11:29:07 +01:00
|
|
|
self.org_rank_requirement_pnts = {}
|
|
|
|
for rank in range(1, self.org_rank_maxLevel+1):
|
|
|
|
self.org_rank_requirement_pnts[rank] = self.cfg_org_rank.getint('rankRequirementsPnts', str(rank))
|
|
|
|
self.org_rank_requirement_text = {}
|
|
|
|
for rank in range(1, self.org_rank_maxLevel+1):
|
|
|
|
self.org_rank_requirement_text[rank] = self.cfg_org_rank.get('rankRequirementsText', str(rank))
|
|
|
|
self.org_rank_additional_info = json.loads(self.cfg_org_rank.get('additionalInfo', 'textsArray'))
|
2017-11-06 10:42:51 +01:00
|
|
|
|
2017-11-08 11:29:07 +01:00
|
|
|
#WEB STUFF
|
2017-11-06 18:40:44 +01:00
|
|
|
self.misp_web_url = cfg.get('RedisGlobal', 'misp_web_url')
|
2017-11-06 10:42:51 +01:00
|
|
|
self.MAX_NUMBER_OF_LAST_CONTRIBUTOR = cfg.getint('CONTRIB', 'max_number_of_last_contributor')
|
2017-11-29 16:09:39 +01:00
|
|
|
self.categories_in_datatable = json.loads(self.cfg_org_rank.get('monthlyRanking', 'categories_in_datatable'))
|
2017-11-08 11:29:07 +01:00
|
|
|
|
|
|
|
#MONTHLY RANKING
|
2017-11-29 16:09:39 +01:00
|
|
|
self.default_pnts_per_contribution = json.loads(self.cfg_org_rank.get('monthlyRanking', 'default_pnts_per_contribution'))
|
|
|
|
temp = json.loads(self.cfg_org_rank.get('monthlyRanking', 'pnts_per_contribution'))
|
2017-11-06 17:14:10 +01:00
|
|
|
self.DICO_PNTS_REWARD = {}
|
|
|
|
for categ, pnts in temp:
|
|
|
|
self.DICO_PNTS_REWARD[categ] = pnts
|
|
|
|
# fill other categ with default points
|
|
|
|
for categ in self.categories_in_datatable:
|
|
|
|
if categ in self.DICO_PNTS_REWARD:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
self.DICO_PNTS_REWARD[categ] = self.default_pnts_per_contribution
|
|
|
|
|
2017-11-29 16:09:39 +01:00
|
|
|
self.rankMultiplier = self.cfg_org_rank.getfloat('monthlyRanking' ,'rankMultiplier')
|
2017-12-01 15:49:20 +01:00
|
|
|
self.levelMax = self.cfg_org_rank.getint('monthlyRanking' ,'levelMax')
|
2017-11-29 16:09:39 +01:00
|
|
|
|
|
|
|
# REDIS KEYS
|
2017-12-01 15:39:17 +01:00
|
|
|
self.keyDay = KEYDAY
|
2017-11-29 16:09:39 +01:00
|
|
|
self.keyCateg = "CONTRIB_CATEG"
|
|
|
|
self.keyLastContrib = "CONTRIB_LAST"
|
2017-12-05 16:04:28 +01:00
|
|
|
self.keyAllOrg = KEYALLORG
|
2017-11-29 16:09:39 +01:00
|
|
|
self.keyContribReq = "CONTRIB_ORG"
|
|
|
|
self.keyTrophy = "CONTRIB_TROPHY"
|
|
|
|
self.keyLastAward = "CONTRIB_LAST_AWARDS"
|
2017-11-06 10:42:51 +01:00
|
|
|
|
2017-11-08 15:34:02 +01:00
|
|
|
|
|
|
|
''' HELPER '''
|
2017-11-09 10:58:18 +01:00
|
|
|
def getOrgLogoFromMISP(self, org):
|
2017-11-08 15:34:02 +01:00
|
|
|
return "{}/img/orgs/{}.png".format(self.misp_web_url, org)
|
|
|
|
|
2017-11-10 15:03:33 +01:00
|
|
|
def addContributionToCateg(self, date, categ, org, count=1):
|
|
|
|
today_str = util.getDateStrFormat(date)
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = "{}:{}:{}".format(self.keyCateg, today_str, categ)
|
2017-11-10 15:03:33 +01:00
|
|
|
self.serv_redis_db.zincrby(keyname, org, count)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Added to redis: keyname={}, org={}, count={}'.format(keyname, org, count))
|
2017-11-10 15:03:33 +01:00
|
|
|
|
2017-11-29 16:09:39 +01:00
|
|
|
def publish_log(self, zmq_name, name, content, channel=""):
|
|
|
|
to_send = { 'name': name, 'log': json.dumps(content), 'zmqName': zmq_name }
|
2017-12-01 15:39:17 +01:00
|
|
|
self.serv_log.publish(channel, json.dumps(to_send))
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Published: {}'.format(json.dumps(to_send)))
|
2017-11-29 16:09:39 +01:00
|
|
|
|
|
|
|
''' HANDLER '''
|
|
|
|
#pntMultiplier if one contribution rewards more than others. (e.g. shighting may gives more points than editing)
|
|
|
|
def handleContribution(self, zmq_name, org, contribType, categ, action, pntMultiplier=1, eventTime=datetime.datetime.now(), isLabeled=False):
|
|
|
|
if action in ['edit', None]:
|
|
|
|
pass
|
|
|
|
#return #not a contribution?
|
|
|
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
nowSec = int(time.time())
|
|
|
|
pnts_to_add = self.default_pnts_per_contribution
|
|
|
|
|
2017-12-05 16:31:08 +01:00
|
|
|
# Do not consider contribution as login anymore
|
|
|
|
#self.users_helper.add_user_login(nowSec, org)
|
2017-11-29 16:09:39 +01:00
|
|
|
|
|
|
|
# is a valid contribution
|
|
|
|
if categ is not None:
|
|
|
|
try:
|
2017-12-01 15:39:17 +01:00
|
|
|
pnts_to_add = self.DICO_PNTS_REWARD[util.noSpaceLower(categ)]
|
2017-11-29 16:09:39 +01:00
|
|
|
except KeyError:
|
|
|
|
pnts_to_add = self.default_pnts_per_contribution
|
|
|
|
pnts_to_add *= pntMultiplier
|
|
|
|
|
|
|
|
util.push_to_redis_zset(self.serv_redis_db, self.keyDay, org, count=pnts_to_add)
|
|
|
|
#CONTRIB_CATEG retain the contribution per category, not the point earned in this categ
|
2017-12-01 15:39:17 +01:00
|
|
|
util.push_to_redis_zset(self.serv_redis_db, self.keyCateg, org, count=1, endSubkey=':'+util.noSpaceLower(categ))
|
|
|
|
self.publish_log(zmq_name, 'CONTRIBUTION', {'org': org, 'categ': categ, 'action': action, 'epoch': nowSec }, channel=self.CHANNEL_LASTCONTRIB)
|
2017-11-29 16:09:39 +01:00
|
|
|
else:
|
|
|
|
categ = ""
|
|
|
|
|
2017-12-01 15:39:17 +01:00
|
|
|
self.serv_redis_db.sadd(self.keyAllOrg, org)
|
2017-11-29 16:09:39 +01:00
|
|
|
|
|
|
|
keyname = "{}:{}".format(self.keyLastContrib, util.getDateStrFormat(now))
|
2017-12-01 15:39:17 +01:00
|
|
|
self.serv_redis_db.zadd(keyname, nowSec, org)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Added to redis: keyname={}, nowSec={}, org={}'.format(keyname, nowSec, org))
|
2017-12-01 15:39:17 +01:00
|
|
|
self.serv_redis_db.expire(keyname, util.ONE_DAY*7) #expire after 7 day
|
2017-11-29 16:09:39 +01:00
|
|
|
|
2017-12-01 15:39:17 +01:00
|
|
|
awards_given = self.updateOrgContributionRank(org, pnts_to_add, action, contribType, eventTime=datetime.datetime.now(), isLabeled=isLabeled, categ=util.noSpaceLower(categ))
|
2017-11-29 16:09:39 +01:00
|
|
|
|
|
|
|
for award in awards_given:
|
|
|
|
# update awards given
|
|
|
|
keyname = "{}:{}".format(self.keyLastAward, util.getDateStrFormat(now))
|
2017-12-01 15:39:17 +01:00
|
|
|
self.serv_redis_db.zadd(keyname, nowSec, json.dumps({'org': org, 'award': award, 'epoch': nowSec }))
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Added to redis: keyname={}, nowSec={}, content={}'.format(keyname, nowSec, json.dumps({'org': org, 'award': award, 'epoch': nowSec })))
|
2017-12-01 15:39:17 +01:00
|
|
|
self.serv_redis_db.expire(keyname, util.ONE_DAY*7) #expire after 7 day
|
2017-11-29 16:09:39 +01:00
|
|
|
# publish
|
|
|
|
self.publish_log(zmq_name, 'CONTRIBUTION', {'org': org, 'award': award, 'epoch': nowSec }, channel=self.CHANNEL_LASTAWARDS)
|
|
|
|
|
2017-11-08 15:34:02 +01:00
|
|
|
''' CONTRIBUTION RANK '''
|
2017-11-09 13:43:02 +01:00
|
|
|
def getOrgContributionTotalPoints(self, org):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{org}:{orgCateg}'
|
|
|
|
pnts = self.serv_redis_db.get(keyname.format(mainKey=self.keyContribReq, org=org, orgCateg='points'))
|
2017-11-09 13:43:02 +01:00
|
|
|
if pnts is None:
|
|
|
|
pnts = 0
|
|
|
|
else:
|
|
|
|
pnts = int(pnts.decode('utf8'))
|
|
|
|
return pnts
|
|
|
|
|
2017-11-08 15:34:02 +01:00
|
|
|
# return: [final_rank, requirement_fulfilled, requirement_not_fulfilled]
|
|
|
|
def getOrgContributionRank(self, org):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{org}:{orgCateg}'
|
2017-11-08 15:34:02 +01:00
|
|
|
final_rank = 0
|
|
|
|
requirement_fulfilled = []
|
|
|
|
requirement_not_fulfilled = []
|
|
|
|
for i in range(1, self.org_rank_maxLevel+1):
|
2017-11-29 16:09:39 +01:00
|
|
|
key = keyname.format(mainKey=self.keyContribReq, org=org, orgCateg='CONTRIB_REQ_'+str(i))
|
2017-11-08 15:34:02 +01:00
|
|
|
if self.serv_redis_db.get(key) is None: #non existing
|
|
|
|
requirement_not_fulfilled.append(i)
|
|
|
|
else:
|
|
|
|
requirement_fulfilled.append(i)
|
|
|
|
final_rank += 1
|
2017-11-09 16:31:04 +01:00
|
|
|
#num_of_previous_req_not_fulfilled = len([x for x in requirement_not_fulfilled if x<final_rank])
|
|
|
|
#final_rank = final_rank - num_of_previous_req_not_fulfilled
|
|
|
|
final_rank = len(requirement_fulfilled)
|
2017-11-09 10:58:18 +01:00
|
|
|
return {'final_rank': final_rank, 'req_fulfilled': requirement_fulfilled, 'req_not_fulfilled': requirement_not_fulfilled}
|
2017-11-08 15:34:02 +01:00
|
|
|
|
|
|
|
def giveContribRankToOrg(self, org, rankNum):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{org}:{orgCateg}'
|
|
|
|
self.serv_redis_db.set(keyname.format(mainKey=self.keyContribReq, org=org, orgCateg='CONTRIB_REQ_'+str(rankNum)), 1)
|
2017-11-08 15:34:02 +01:00
|
|
|
|
|
|
|
def removeContribRankFromOrg(self, org, rankNum):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{org}:{orgCateg}'
|
|
|
|
self.serv_redis_db.delete(keyname.format(mainKey=self.keyContribReq, org=org, orgCateg='CONTRIB_REQ_'+str(rankNum)))
|
2017-11-08 15:34:02 +01:00
|
|
|
|
|
|
|
# 1 for fulfilled, 0 for not fulfilled, -1 for not relevant
|
|
|
|
def getCurrentContributionStatus(self, org):
|
2017-11-09 10:58:18 +01:00
|
|
|
temp = self.getOrgContributionRank(org)
|
|
|
|
final_rank = temp['final_rank']
|
|
|
|
requirement_fulfilled = temp['req_fulfilled']
|
|
|
|
requirement_not_fulfilled = temp['req_not_fulfilled']
|
2017-11-08 15:34:02 +01:00
|
|
|
to_ret = {}
|
|
|
|
for i in range(1, self.org_rank_maxLevel+1):
|
|
|
|
if i in requirement_fulfilled:
|
|
|
|
to_ret[i] = 1
|
|
|
|
elif i in requirement_not_fulfilled and i<=final_rank:
|
|
|
|
to_ret[i] = 0
|
|
|
|
else:
|
|
|
|
to_ret[i] = -1
|
2017-11-09 13:43:02 +01:00
|
|
|
return {'rank': final_rank, 'status': to_ret, 'totPoints': self.getOrgContributionTotalPoints(org)}
|
2017-11-08 17:37:03 +01:00
|
|
|
|
2017-11-14 09:50:20 +01:00
|
|
|
# return the awards given to the organisation
|
2017-11-14 14:05:54 +01:00
|
|
|
def updateOrgContributionRank(self, orgName, pnts_to_add, action, contribType, eventTime, isLabeled, categ=""):
|
|
|
|
ContributionStatus = self.getCurrentContributionStatus(orgName)
|
2017-11-14 09:50:20 +01:00
|
|
|
oldContributionStatus = ContributionStatus['status']
|
2017-11-14 14:05:54 +01:00
|
|
|
oldHonorBadges = self.getOrgHonorBadges(orgName)
|
2017-11-14 14:56:15 +01:00
|
|
|
oldTrophy = self.getOrgTrophies(orgName)
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = self.keyContribReq+':{org}:{orgCateg}'
|
2017-11-09 16:31:04 +01:00
|
|
|
# update total points
|
2017-11-09 13:43:02 +01:00
|
|
|
totOrgPnts = self.serv_redis_db.incrby(keyname.format(org=orgName, orgCateg='points'), pnts_to_add)
|
2017-11-09 16:31:04 +01:00
|
|
|
|
2017-11-14 14:05:54 +01:00
|
|
|
#FIXME TEMPORARY, JUST TO TEST IF IT WORKS CORRECLTY
|
|
|
|
self.giveTrophyPointsToOrg(orgName, categ, 1)
|
|
|
|
|
2017-11-09 16:31:04 +01:00
|
|
|
# update date variables
|
|
|
|
if contribType == 'Attribute':
|
|
|
|
attributeWeekCount = self.serv_redis_db.incrby(keyname.format(org=orgName, orgCateg='ATTR_WEEK_COUNT'), 1)
|
|
|
|
self.serv_redis_db.expire(keyname.format(org=orgName, orgCateg='ATTR_WEEK_COUNT'), util.ONE_DAY*7)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Incrby: keyname={}'.format(keyname.format(org=orgName, orgCateg='ATTR_WEEK_COUNT')))
|
2017-11-09 16:31:04 +01:00
|
|
|
|
|
|
|
if contribType == 'Proposal':
|
|
|
|
proposalWeekCount = self.serv_redis_db.incrby(keyname.format(org=orgName, orgCateg='PROP_WEEK_COUNT'), 1)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Incrby: keyname={}'.format(keyname.format(org=orgName, orgCateg='PROP_WEEK_COUNT')))
|
2017-11-09 16:31:04 +01:00
|
|
|
self.serv_redis_db.expire(keyname.format(org=orgName, orgCateg='PROP_WEEK_COUNT'), util.ONE_DAY*7)
|
2017-11-10 15:03:33 +01:00
|
|
|
addContributionToCateg(datetime.datetime.now(), 'proposal')
|
2017-11-09 16:31:04 +01:00
|
|
|
|
|
|
|
if contribType == 'Sighting':
|
|
|
|
sightingWeekCount = self.serv_redis_db.incrby(keyname.format(org=orgName, orgCateg='SIGHT_WEEK_COUNT'), 1)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Incrby: keyname={}'.format(keyname.format(org=orgName, orgCateg='SIGHT_WEEK_COUNT')))
|
2017-11-09 16:31:04 +01:00
|
|
|
self.serv_redis_db.expire(keyname.format(org=orgName, orgCateg='SIGHT_WEEK_COUNT'), util.ONE_DAY*7)
|
2017-11-10 15:03:33 +01:00
|
|
|
self.addContributionToCateg(datetime.datetime.now(), 'sighting', orgName)
|
|
|
|
|
|
|
|
if contribType == 'Discussion':
|
|
|
|
self.addContributionToCateg(datetime.datetime.now(), 'discussion', orgName)
|
2017-11-09 16:31:04 +01:00
|
|
|
|
|
|
|
if contribType == 'Event':
|
|
|
|
eventWeekCount = self.serv_redis_db.incrby(keyname.format(org=orgName, orgCateg='EVENT_WEEK_COUNT'), 1)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Incrby: keyname={}'.format(keyname.format(org=orgName, orgCateg='EVENT_WEEK_COUNT')))
|
2017-11-09 16:31:04 +01:00
|
|
|
self.serv_redis_db.expire(keyname.format(org=orgName, orgCateg='EVENT_WEEK_COUNT'), util.ONE_DAY*7)
|
|
|
|
|
|
|
|
eventMonthCount = self.serv_redis_db.incrby(keyname.format(org=orgName, orgCateg='EVENT_MONTH_COUNT'), 1)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Incrby: keyname={}'.format(keyname.format(org=orgName, orgCateg='EVENT_MONTH_COUNT')))
|
2017-11-09 16:31:04 +01:00
|
|
|
self.serv_redis_db.expire(keyname.format(org=orgName, orgCateg='EVENT_MONTH_COUNT'), util.ONE_DAY*7)
|
|
|
|
|
2017-11-09 13:43:02 +01:00
|
|
|
# getRequirement parameters
|
2017-11-08 17:37:03 +01:00
|
|
|
heavilyCount = self.heavilyCount
|
|
|
|
recentDays = self.recentDays
|
|
|
|
regularlyDays = self.regularlyDays
|
|
|
|
isRecent = (datetime.datetime.now() - eventTime).days > recentDays
|
|
|
|
|
2017-11-09 13:43:02 +01:00
|
|
|
#update contribution Requirement
|
2017-11-08 17:37:03 +01:00
|
|
|
contrib = [] #[[contrib_level, contrib_ttl], [], ...]
|
2017-11-09 13:43:02 +01:00
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[1] and contribType == 'Sighting':
|
2017-11-08 17:37:03 +01:00
|
|
|
#[contrib_level, contrib_ttl]
|
2017-11-09 13:43:02 +01:00
|
|
|
contrib.append([1, util.ONE_DAY*365])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[2] and contribType == 'Attribute' or contribType == 'Object':
|
|
|
|
contrib.append([2, util.ONE_DAY*365])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[3] and contribType == 'Proposal' or contribType == 'Discussion':
|
|
|
|
contrib.append([3, util.ONE_DAY*365])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[4] and contribType == 'Sighting' and isRecent:
|
|
|
|
contrib.append([4, util.ONE_DAY*recentDays])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[5] and contribType == 'Proposal' and isRecent:
|
|
|
|
contrib.append([5, util.ONE_DAY*recentDays])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[6] and contribType == 'Event':
|
|
|
|
contrib.append([6, util.ONE_DAY*365])
|
2017-11-09 16:31:04 +01:00
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[7] and contribType == 'Event' and eventMonthCount>=1:
|
2017-11-09 13:43:02 +01:00
|
|
|
contrib.append([7, util.ONE_DAY*recentDays])
|
2017-11-09 16:31:04 +01:00
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[8] and contribType == 'Event' and eventWeekCount>=1:
|
2017-11-09 13:43:02 +01:00
|
|
|
contrib.append([8, util.ONE_DAY*regularlyDays])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[9] and contribType == 'Event' and isLabeled:
|
|
|
|
contrib.append([9, util.ONE_DAY*regularlyDays])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[10] and contribType == 'Sighting' and sightingWeekCount>heavilyCount:
|
|
|
|
contrib.append([10, util.ONE_DAY*regularlyDays])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[11] and (contribType == 'Attribute' or contribType == 'Object') and attributeWeekCount>heavilyCount:
|
|
|
|
contrib.append([11, util.ONE_DAY*regularlyDays])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[12] and contribType == 'Proposal' and proposalWeekCount>heavilyCount:
|
|
|
|
contrib.append([12, util.ONE_DAY*regularlyDays])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[13] and contribType == 'Event' and eventWeekCount>heavilyCount:
|
|
|
|
contrib.append([13, util.ONE_DAY*regularlyDays])
|
|
|
|
if totOrgPnts >= self.org_rank_requirement_pnts[14] and contribType == 'Event' and eventWeekCount>heavilyCount and isLabeled:
|
|
|
|
contrib.append([14, util.ONE_DAY*regularlyDays])
|
|
|
|
|
2017-11-08 17:37:03 +01:00
|
|
|
for rankReq, ttl in contrib:
|
|
|
|
self.serv_redis_db.set(keyname.format(org=orgName, orgCateg='CONTRIB_REQ_'+str(rankReq)), 1)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Set: keyname={}'.format(keyname.format(org=orgName, orgCateg='CONTRIB_REQ_'+str(rankReq))))
|
2017-11-09 13:43:02 +01:00
|
|
|
self.serv_redis_db.expire(keyname.format(org=orgName, orgCateg='CONTRIB_REQ_'+str(rankReq)), ttl)
|
2017-11-08 17:37:03 +01:00
|
|
|
|
2017-11-14 14:05:54 +01:00
|
|
|
ContributionStatus = self.getCurrentContributionStatus(orgName)
|
2017-11-14 09:50:20 +01:00
|
|
|
newContributionStatus = ContributionStatus['status']
|
2017-11-14 14:05:54 +01:00
|
|
|
newHonorBadges = self.getOrgHonorBadges(orgName)
|
2017-11-14 14:56:15 +01:00
|
|
|
newTrophy = self.getOrgTrophies(orgName)
|
2017-11-14 14:05:54 +01:00
|
|
|
|
2017-11-14 14:56:15 +01:00
|
|
|
# awards to publish
|
2017-11-14 09:50:20 +01:00
|
|
|
awards_given = []
|
2017-11-14 14:05:54 +01:00
|
|
|
for i in newContributionStatus.keys():
|
|
|
|
if oldContributionStatus[i] < newContributionStatus[i] and i != ContributionStatus['rank']:
|
2017-11-14 09:50:20 +01:00
|
|
|
awards_given.append(['contribution_status', i])
|
|
|
|
|
2017-11-14 14:05:54 +01:00
|
|
|
for badgeNum in newHonorBadges:
|
|
|
|
if badgeNum not in oldHonorBadges:
|
|
|
|
awards_given.append(['badge', badgeNum])
|
|
|
|
|
2017-11-14 14:56:15 +01:00
|
|
|
temp = {}
|
|
|
|
for item in oldTrophy:
|
|
|
|
categ = item['categ']
|
|
|
|
rank = item['trophy_true_rank']
|
|
|
|
temp[categ] = rank
|
|
|
|
for item in newTrophy:
|
|
|
|
categ = item['categ']
|
|
|
|
rank = item['trophy_true_rank']
|
2017-11-14 17:02:51 +01:00
|
|
|
try:
|
|
|
|
oldCategRank = temp[categ]
|
|
|
|
except KeyError:
|
|
|
|
oldCategRank = 0
|
|
|
|
if rank > oldCategRank:
|
2017-11-14 14:56:15 +01:00
|
|
|
awards_given.append(['trophy', [categ, rank]])
|
|
|
|
|
2017-11-14 09:50:20 +01:00
|
|
|
return awards_given
|
|
|
|
|
2017-11-08 15:34:02 +01:00
|
|
|
''' HONOR BADGES '''
|
|
|
|
def getOrgHonorBadges(self, org):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{org}:{orgCateg}'
|
2017-11-08 15:34:02 +01:00
|
|
|
honorBadge = []
|
|
|
|
for i in range(1, self.honorBadgeNum+1):
|
2017-11-29 16:09:39 +01:00
|
|
|
key = keyname.format(mainKey=self.keyContribReq, org=org, orgCateg='BADGE_'+str(i))
|
2017-11-08 15:34:02 +01:00
|
|
|
if self.serv_redis_db.get(key) is not None: #existing
|
|
|
|
honorBadge.append(i)
|
|
|
|
return honorBadge
|
|
|
|
|
|
|
|
def giveBadgeToOrg(self, org, badgeNum):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{org}:{orgCateg}'
|
|
|
|
self.serv_redis_db.set(keyname.format(mainKey=self.keyContribReq, org=org, orgCateg='BADGE_'+str(badgeNum)), 1)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Giving badge {} to org {}'.format(org, badgeNum))
|
2017-11-08 15:34:02 +01:00
|
|
|
|
|
|
|
def removeBadgeFromOrg(self, org, badgeNum):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{org}:{orgCateg}'
|
|
|
|
self.serv_redis_db.delete(keyname.format(mainKey=self.keyContribReq, org=org, orgCateg='BADGE_'+str(badgeNum)))
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Removing badge {} from org {}'.format(org, badgeNum))
|
2017-11-08 15:34:02 +01:00
|
|
|
|
2017-11-13 12:57:05 +01:00
|
|
|
''' TROPHIES '''
|
|
|
|
def getOrgTrophies(self, org):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{orgCateg}'
|
2017-11-13 12:57:05 +01:00
|
|
|
trophy = []
|
2017-11-14 09:50:20 +01:00
|
|
|
for categ in self.categories_in_trophy:
|
2017-11-29 16:09:39 +01:00
|
|
|
key = keyname.format(mainKey=self.keyTrophy, orgCateg=categ)
|
2017-11-24 15:09:34 +01:00
|
|
|
totNum = self.serv_redis_db.zcard(key)
|
|
|
|
if totNum == 0:
|
|
|
|
continue
|
2018-09-25 14:48:13 +02:00
|
|
|
pos = self.serv_redis_db.zrevrank(key, org)
|
2017-11-24 15:09:34 +01:00
|
|
|
if pos is None:
|
|
|
|
continue
|
2018-09-25 14:48:13 +02:00
|
|
|
|
2017-11-24 15:09:34 +01:00
|
|
|
trophy_rank = self.posToRankMapping(pos, totNum)
|
|
|
|
trophy_Pnts = self.serv_redis_db.zscore(key, org)
|
2018-09-25 14:48:13 +02:00
|
|
|
trophy.append({ 'categ': categ, 'trophy_points': trophy_Pnts, 'trophy_rank': trophy_rank, 'trophy_true_rank': self.trophyNum-trophy_rank, 'trophy_title': self.trophy_title[trophy_rank]})
|
2017-11-13 12:57:05 +01:00
|
|
|
return trophy
|
|
|
|
|
2017-11-24 15:09:34 +01:00
|
|
|
def getOrgsTrophyRanking(self, categ):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{orgCateg}'
|
|
|
|
res = self.serv_redis_db.zrange(keyname.format(mainKey=self.keyTrophy, orgCateg=categ), 0, -1, withscores=True, desc=True)
|
2017-11-24 15:09:34 +01:00
|
|
|
res = [[org.decode('utf8'), score] for org, score in res]
|
|
|
|
return res
|
|
|
|
|
2018-09-25 14:48:13 +02:00
|
|
|
def getAllOrgsTrophyRanking(self, category=None):
|
|
|
|
concerned_categ = self.categories_in_trophy if category is None else category
|
2017-11-24 15:09:34 +01:00
|
|
|
dico_categ = {}
|
2018-09-25 14:48:13 +02:00
|
|
|
for categ in [concerned_categ]:
|
2017-11-24 15:09:34 +01:00
|
|
|
res = self.getOrgsTrophyRanking(categ)
|
2018-09-25 14:48:13 +02:00
|
|
|
# add ranking info
|
|
|
|
tot = len(res)
|
|
|
|
for pos in range(tot):
|
|
|
|
res[pos].append(self.trophyNum-self.posToRankMapping(pos, tot))
|
2017-11-24 15:09:34 +01:00
|
|
|
dico_categ[categ] = res
|
2018-09-25 14:48:13 +02:00
|
|
|
toret = dico_categ if category is None else dico_categ.get(category, [])
|
|
|
|
return toret
|
2017-11-24 15:09:34 +01:00
|
|
|
|
|
|
|
def posToRankMapping(self, pos, totNum):
|
2018-09-25 14:48:13 +02:00
|
|
|
ratio = pos/totNum*100
|
|
|
|
rank = 0
|
|
|
|
if pos == totNum:
|
|
|
|
return 0
|
2017-11-24 15:09:34 +01:00
|
|
|
else:
|
2018-09-25 14:48:13 +02:00
|
|
|
for i in range(len(self.trophyMappingIncremental)):
|
|
|
|
if self.trophyMappingIncremental[i] < ratio <= self.trophyMappingIncremental[i+1]:
|
|
|
|
rank = i+1
|
|
|
|
return rank
|
2017-11-24 15:09:34 +01:00
|
|
|
|
2017-11-14 09:50:20 +01:00
|
|
|
def giveTrophyPointsToOrg(self, org, categ, points):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{orgCateg}'
|
|
|
|
self.serv_redis_db.zincrby(keyname.format(mainKey=self.keyTrophy, orgCateg=categ), org, points)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Giving {} trophy points to {} in {}'.format(points, org, categ))
|
2017-11-14 09:50:20 +01:00
|
|
|
|
|
|
|
def removeTrophyPointsFromOrg(self, org, categ, points):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = '{mainKey}:{orgCateg}'
|
|
|
|
self.serv_redis_db.zincrby(keyname.format(mainKey=self.keyTrophy, orgCateg=categ), org, -points)
|
2017-12-05 10:23:40 +01:00
|
|
|
self.logger.debug('Removing {} trophy points from {} in {}'.format(points, org, categ))
|
2017-11-14 09:50:20 +01:00
|
|
|
|
|
|
|
''' AWARDS HELPER '''
|
|
|
|
def getLastAwardsFromRedis(self):
|
|
|
|
date = datetime.datetime.now()
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = self.keyLastAward
|
2017-11-14 09:50:20 +01:00
|
|
|
prev_days = 7
|
|
|
|
topNum = self.MAX_NUMBER_OF_LAST_CONTRIBUTOR # default Num
|
|
|
|
addedOrg = []
|
|
|
|
data = []
|
|
|
|
for curDate in util.getXPrevDaysSpan(date, prev_days):
|
2017-11-29 16:09:39 +01:00
|
|
|
last_awards = getZrange(self.serv_redis_db, keyname, curDate, topNum)
|
2017-11-14 09:50:20 +01:00
|
|
|
for dico_award, sec in last_awards:
|
|
|
|
dico_award = json.loads(dico_award)
|
|
|
|
org = dico_award['org']
|
|
|
|
dic = {}
|
|
|
|
dic['orgRank'] = self.getOrgContributionRank(org)['final_rank']
|
|
|
|
dic['logo_path'] = self.getOrgLogoFromMISP(org)
|
|
|
|
dic['org'] = org
|
|
|
|
dic['epoch'] = sec
|
|
|
|
dic['award'] = dico_award['award']
|
|
|
|
data.append(dic)
|
|
|
|
return data
|
|
|
|
|
2017-11-08 15:34:02 +01:00
|
|
|
''' MONTHLY CONTRIBUTION '''
|
2017-11-06 10:42:51 +01:00
|
|
|
def getOrgPntFromRedis(self, org, date):
|
|
|
|
scoreSum = 0
|
|
|
|
for curDate in util.getMonthSpan(date):
|
|
|
|
date_str = util.getDateStrFormat(curDate)
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = "{}:{}".format(self.keyDay, date_str)
|
2017-11-06 10:42:51 +01:00
|
|
|
data = self.serv_redis_db.zscore(keyname, org)
|
|
|
|
if data is None:
|
|
|
|
data = 0
|
|
|
|
scoreSum += data
|
|
|
|
return scoreSum
|
|
|
|
|
|
|
|
def getOrgRankFromRedis(self, org, date):
|
|
|
|
ptns = self.getOrgPntFromRedis(org, date)
|
|
|
|
return self.getTrueRank(ptns)
|
|
|
|
|
|
|
|
|
2017-11-06 13:43:55 +01:00
|
|
|
def getLastContributorsFromRedis(self):
|
2017-11-06 10:42:51 +01:00
|
|
|
date = datetime.datetime.now()
|
2017-11-10 09:28:59 +01:00
|
|
|
prev_days = 7
|
2017-11-06 10:42:51 +01:00
|
|
|
topNum = self.MAX_NUMBER_OF_LAST_CONTRIBUTOR # default Num
|
2017-11-10 14:10:48 +01:00
|
|
|
addedOrg = []
|
2017-11-06 10:42:51 +01:00
|
|
|
data = []
|
2017-11-10 09:28:59 +01:00
|
|
|
for curDate in util.getXPrevDaysSpan(date, prev_days):
|
2017-11-29 16:09:39 +01:00
|
|
|
last_contrib_org = getZrange(self.serv_redis_db, self.keyLastContrib, curDate, topNum)
|
2017-11-10 09:28:59 +01:00
|
|
|
for org, sec in last_contrib_org:
|
2017-11-10 14:10:48 +01:00
|
|
|
if org in addedOrg:
|
|
|
|
continue
|
2017-11-10 09:28:59 +01:00
|
|
|
dic = {}
|
|
|
|
dic['rank'] = self.getOrgRankFromRedis(org, date)
|
|
|
|
dic['orgRank'] = self.getOrgContributionRank(org)['final_rank']
|
|
|
|
dic['honorBadge'] = self.getOrgHonorBadges(org)
|
|
|
|
dic['logo_path'] = self.getOrgLogoFromMISP(org)
|
|
|
|
dic['org'] = org
|
|
|
|
dic['pnts'] = self.getOrgPntFromRedis(org, date)
|
|
|
|
dic['epoch'] = sec
|
|
|
|
data.append(dic)
|
2017-11-10 14:10:48 +01:00
|
|
|
addedOrg.append(org)
|
2017-11-06 10:42:51 +01:00
|
|
|
return data
|
|
|
|
|
2017-11-10 09:28:59 +01:00
|
|
|
|
2017-11-06 13:43:55 +01:00
|
|
|
def getContributorFromRedis(self, org):
|
|
|
|
date = datetime.datetime.now()
|
2017-11-29 16:09:39 +01:00
|
|
|
epoch = self.serv_redis_db.zscore(self.keyLastContrib, org)
|
2017-11-06 13:43:55 +01:00
|
|
|
dic = {}
|
|
|
|
dic['rank'] = self.getOrgRankFromRedis(org, date)
|
2017-11-09 10:58:18 +01:00
|
|
|
dic['orgRank'] = self.getOrgContributionRank(org)['final_rank']
|
|
|
|
dic['honorBadge'] = self.getOrgHonorBadges(org)
|
|
|
|
dic['logo_path'] = self.getOrgLogoFromMISP(org)
|
2017-11-06 13:43:55 +01:00
|
|
|
dic['org'] = org
|
|
|
|
dic['pnts'] = self.getOrgPntFromRedis(org, date)
|
|
|
|
dic['epoch'] = epoch
|
|
|
|
return dic
|
|
|
|
|
2017-12-01 16:02:28 +01:00
|
|
|
def getTopContributorFromRedis(self, date, maxNum=100):
|
2017-11-06 10:42:51 +01:00
|
|
|
orgDicoPnts = {}
|
|
|
|
for curDate in util.getMonthSpan(date):
|
|
|
|
topNum = 0 # all
|
2017-11-29 16:09:39 +01:00
|
|
|
contrib_org = getZrange(self.serv_redis_db, self.keyDay, curDate, topNum)
|
2017-11-06 10:42:51 +01:00
|
|
|
for org, pnts in contrib_org:
|
|
|
|
if org not in orgDicoPnts:
|
|
|
|
orgDicoPnts[org] = 0
|
|
|
|
orgDicoPnts[org] += pnts
|
|
|
|
|
|
|
|
data = []
|
|
|
|
for org, pnts in orgDicoPnts.items():
|
|
|
|
dic = {}
|
|
|
|
dic['rank'] = self.getTrueRank(pnts)
|
2017-11-09 10:58:18 +01:00
|
|
|
dic['orgRank'] = self.getOrgContributionRank(org)['final_rank']
|
|
|
|
dic['honorBadge'] = self.getOrgHonorBadges(org)
|
|
|
|
dic['logo_path'] = self.getOrgLogoFromMISP(org)
|
2017-11-06 10:42:51 +01:00
|
|
|
dic['org'] = org
|
|
|
|
dic['pnts'] = pnts
|
|
|
|
data.append(dic)
|
|
|
|
data.sort(key=lambda x: x['pnts'], reverse=True)
|
|
|
|
|
2017-12-01 16:02:28 +01:00
|
|
|
return data[:maxNum]
|
2017-11-06 10:42:51 +01:00
|
|
|
|
|
|
|
def getTop5OvertimeFromRedis(self):
|
|
|
|
data = []
|
|
|
|
today = datetime.datetime.now()
|
|
|
|
topSortedOrg = self.getTopContributorFromRedis(today) #Get current top
|
|
|
|
# show current top 5 org points overtime (last 5 days)
|
|
|
|
for dic in topSortedOrg[0:5]:
|
|
|
|
org = dic['org']
|
2017-11-09 10:58:18 +01:00
|
|
|
to_append = self.getOrgOvertime(org)
|
2017-11-06 10:42:51 +01:00
|
|
|
data.append(to_append)
|
|
|
|
return data
|
|
|
|
|
2017-11-09 10:58:18 +01:00
|
|
|
def getOrgOvertime(self, org):
|
|
|
|
overtime = []
|
2017-11-10 11:15:31 +01:00
|
|
|
today = datetime.datetime.today()
|
|
|
|
today = today.replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
|
for curDate in util.getXPrevDaysSpan(today, 7):
|
|
|
|
timestamp = util.getTimestamp(curDate)
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = "{}:{}".format(self.keyDay, util.getDateStrFormat(curDate))
|
2017-11-09 10:58:18 +01:00
|
|
|
org_score = self.serv_redis_db.zscore(keyname, org)
|
|
|
|
if org_score is None:
|
|
|
|
org_score = 0
|
2017-11-10 11:15:31 +01:00
|
|
|
overtime.append([timestamp, org_score])
|
2017-11-09 10:58:18 +01:00
|
|
|
to_return = {'label': org, 'data': overtime}
|
|
|
|
return to_return
|
|
|
|
|
2017-11-06 10:42:51 +01:00
|
|
|
def getCategPerContribFromRedis(self, date):
|
|
|
|
topNum = 0 # all
|
2017-11-10 09:00:50 +01:00
|
|
|
contrib_org = self.getTopContributorFromRedis(date)
|
|
|
|
for dic in contrib_org:
|
|
|
|
org = dic['org']
|
2017-11-06 10:42:51 +01:00
|
|
|
for categ in self.categories_in_datatable:
|
|
|
|
categ_score = 0
|
2017-11-10 09:00:50 +01:00
|
|
|
for curDate in util.getMonthSpan(date):
|
2017-11-29 16:09:39 +01:00
|
|
|
keyname = "{}:{}:{}".format(self.keyCateg, util.getDateStrFormat(curDate), categ)
|
2017-11-10 09:00:50 +01:00
|
|
|
temp = self.serv_redis_db.zscore(keyname, org)
|
|
|
|
if temp is None:
|
|
|
|
temp = 0
|
|
|
|
categ_score += temp
|
|
|
|
dic[categ] = categ_score
|
|
|
|
return contrib_org
|
|
|
|
|
2017-11-06 10:42:51 +01:00
|
|
|
|
|
|
|
def getAllOrgFromRedis(self):
|
2017-11-29 16:09:39 +01:00
|
|
|
data = self.serv_redis_db.smembers(self.keyAllOrg)
|
2017-11-06 10:42:51 +01:00
|
|
|
data = [x.decode('utf8') for x in data]
|
|
|
|
return data
|
|
|
|
|
|
|
|
def getCurrentOrgRankFromRedis(self, org):
|
|
|
|
date = datetime.datetime.now()
|
|
|
|
points = self.getOrgPntFromRedis(org, date)
|
|
|
|
remainingPts = self.getRemainingPoints(points)
|
|
|
|
data = {
|
|
|
|
'org': org,
|
|
|
|
'points': points,
|
|
|
|
'rank': self.getRankLevel(points),
|
2017-11-09 10:58:18 +01:00
|
|
|
'orgRank': self.getOrgContributionRank(org)['final_rank'],
|
|
|
|
'honorBadge': self.getOrgHonorBadges(org),
|
2017-11-06 10:42:51 +01:00
|
|
|
'remainingPts': remainingPts['remainingPts'],
|
|
|
|
'stepPts': remainingPts['stepPts'],
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
|
|
|
|
def getRankLevel(self, points):
|
|
|
|
if points == 0:
|
|
|
|
return 0
|
|
|
|
elif points == 1:
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return float("{:.2f}".format(math.log(points, self.rankMultiplier)))
|
|
|
|
|
|
|
|
def getTrueRank(self, ptns):
|
|
|
|
return int(self.getRankLevel(ptns))
|
|
|
|
|
|
|
|
def getRemainingPoints(self, points):
|
|
|
|
prev = 0
|
|
|
|
for i in [math.floor(self.rankMultiplier**x) for x in range(1,self.levelMax+1)]:
|
|
|
|
if prev <= points < i:
|
|
|
|
return { 'remainingPts': i-points, 'stepPts': prev }
|
|
|
|
prev = i
|
|
|
|
return { 'remainingPts': 0, 'stepPts': self.rankMultiplier**self.levelMax }
|
|
|
|
|