mirror of https://github.com/MISP/misp-dashboard
fix: Fixed tons of bugs related to migration of handle_contribution to
controbutor_helperpull/15/head
parent
17315f6c74
commit
849d8cc8eb
|
@ -1,18 +1,30 @@
|
|||
import util
|
||||
from util import getZrange
|
||||
import math, random
|
||||
import time
|
||||
import os
|
||||
import configparser
|
||||
import json
|
||||
import datetime
|
||||
import redis
|
||||
|
||||
import util
|
||||
import users_helper
|
||||
KEYDAY = "CONTRIB_DAY" # To be used by other module
|
||||
|
||||
class Contributor_helper:
|
||||
def __init__(self, serv_redis_db, cfg):
|
||||
self.serv_redis_db = serv_redis_db
|
||||
self.serv_log = redis.StrictRedis(
|
||||
host=cfg.get('RedisGlobal', 'host'),
|
||||
port=cfg.getint('RedisGlobal', 'port'),
|
||||
db=cfg.getint('RedisLog', 'db'))
|
||||
self.cfg = cfg
|
||||
self.cfg_org_rank = configparser.ConfigParser()
|
||||
self.cfg_org_rank.read(os.path.join(os.environ['DASH_CONFIG'], 'ranking.cfg'))
|
||||
self.CHANNEL_LASTAWARDS = cfg.get('RedisLog', 'channelLastAwards')
|
||||
self.CHANNEL_LASTCONTRIB = cfg.get('RedisLog', 'channelLastContributor')
|
||||
self.users_helper = users_helper.Users_helper(serv_redis_db, cfg)
|
||||
|
||||
#honorBadge
|
||||
self.honorBadgeNum = len(self.cfg_org_rank.options('HonorBadge'))
|
||||
|
@ -66,7 +78,7 @@ class Contributor_helper:
|
|||
self.levelMax = self.cfg_org_rank.getfloat('monthlyRanking' ,'levelMax')
|
||||
|
||||
# REDIS KEYS
|
||||
self.keyDay = "CONTRIB_DAY"
|
||||
self.keyDay = KEYDAY
|
||||
self.keyCateg = "CONTRIB_CATEG"
|
||||
self.keyLastContrib = "CONTRIB_LAST"
|
||||
self.keyAllOrg = "CONTRIB_ALL_ORG"
|
||||
|
@ -86,7 +98,7 @@ class Contributor_helper:
|
|||
|
||||
def publish_log(self, zmq_name, name, content, channel=""):
|
||||
to_send = { 'name': name, 'log': json.dumps(content), 'zmqName': zmq_name }
|
||||
serv_log.publish(channel, json.dumps(to_send))
|
||||
self.serv_log.publish(channel, json.dumps(to_send))
|
||||
|
||||
''' HANDLER '''
|
||||
#pntMultiplier if one contribution rewards more than others. (e.g. shighting may gives more points than editing)
|
||||
|
@ -100,36 +112,36 @@ class Contributor_helper:
|
|||
pnts_to_add = self.default_pnts_per_contribution
|
||||
|
||||
# if there is a contribution, there is a login (even if ti comes from the API)
|
||||
users_helper.add_user_login(nowSec, org)
|
||||
self.users_helper.add_user_login(nowSec, org)
|
||||
|
||||
# is a valid contribution
|
||||
if categ is not None:
|
||||
try:
|
||||
pnts_to_add = self.DICO_PNTS_REWARD[noSpaceLower(categ)]
|
||||
pnts_to_add = self.DICO_PNTS_REWARD[util.noSpaceLower(categ)]
|
||||
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
|
||||
util.push_to_redis_zset(self.serv_redis_db, self.keyCateg, org, count=1, endSubkey=':'+noSpaceLower(categ))
|
||||
self.publish_log(zmq_name, 'CONTRIBUTION', {'org': org, 'categ': categ, 'action': action, 'epoch': nowSec }, channel=CHANNEL_LASTCONTRIB)
|
||||
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)
|
||||
else:
|
||||
categ = ""
|
||||
|
||||
serv_redis_db.sadd(self.keyAllOrg, org)
|
||||
self.serv_redis_db.sadd(self.keyAllOrg, org)
|
||||
|
||||
keyname = "{}:{}".format(self.keyLastContrib, util.getDateStrFormat(now))
|
||||
serv_redis_db.zadd(keyname, nowSec, org)
|
||||
serv_redis_db.expire(keyname, ONE_DAY*7) #expire after 7 day
|
||||
self.serv_redis_db.zadd(keyname, nowSec, org)
|
||||
self.serv_redis_db.expire(keyname, util.ONE_DAY*7) #expire after 7 day
|
||||
|
||||
awards_given = self.updateOrgContributionRank(org, pnts_to_add, action, contribType, eventTime=datetime.datetime.now(), isLabeled=isLabeled, categ=noSpaceLower(categ))
|
||||
awards_given = self.updateOrgContributionRank(org, pnts_to_add, action, contribType, eventTime=datetime.datetime.now(), isLabeled=isLabeled, categ=util.noSpaceLower(categ))
|
||||
|
||||
for award in awards_given:
|
||||
# update awards given
|
||||
keyname = "{}:{}".format(self.keyLastAward, util.getDateStrFormat(now))
|
||||
serv_redis_db.zadd(keyname, nowSec, json.dumps({'org': org, 'award': award, 'epoch': nowSec }))
|
||||
serv_redis_db.expire(keyname, ONE_DAY*7) #expire after 7 day
|
||||
self.serv_redis_db.zadd(keyname, nowSec, json.dumps({'org': org, 'award': award, 'epoch': nowSec }))
|
||||
self.serv_redis_db.expire(keyname, util.ONE_DAY*7) #expire after 7 day
|
||||
# publish
|
||||
self.publish_log(zmq_name, 'CONTRIBUTION', {'org': org, 'award': award, 'epoch': nowSec }, channel=self.CHANNEL_LASTAWARDS)
|
||||
|
||||
|
|
|
@ -14,8 +14,7 @@ class Users_helper:
|
|||
self.keyTimestamp = "LOGIN_TIMESTAMP"
|
||||
self.keyTimestampSet = "LOGIN_TIMESTAMPSET"
|
||||
self.keyOrgLog = "LOGIN_ORG"
|
||||
contrib_helper = contributor_helper.Contributor_helper(serv_redis_db, cfg)
|
||||
self.keyContribDay = contrib_helper.keyDay # Key to get monthly contribution
|
||||
self.keyContribDay = contributor_helper.KEYDAY # Key to get monthly contribution
|
||||
|
||||
def addTemporary(self, org, timestamp):
|
||||
timestampDate = datetime.datetime.fromtimestamp(float(timestamp))
|
||||
|
|
5
util.py
5
util.py
|
@ -9,9 +9,12 @@ def getZrange(serv_redis_db, keyCateg, date, topNum, endSubkey=""):
|
|||
data = [ [record[0].decode('utf8'), record[1]] for record in data ]
|
||||
return data
|
||||
|
||||
def noSpaceLower(text):
|
||||
return text.lower().replace(' ', '_')
|
||||
|
||||
def push_to_redis_zset(serv_redis_db, mainKey, toAdd, endSubkey="", count=1):
|
||||
now = datetime.datetime.now()
|
||||
today_str = util.getDateStrFormat(now)
|
||||
today_str = getDateStrFormat(now)
|
||||
keyname = "{}:{}{}".format(mainKey, today_str, endSubkey)
|
||||
serv_redis_db.zincrby(keyname, toAdd, count)
|
||||
|
||||
|
|
|
@ -64,9 +64,6 @@ def getFields(obj, fields):
|
|||
except KeyError as e:
|
||||
return ""
|
||||
|
||||
def noSpaceLower(text):
|
||||
return text.lower().replace(' ', '_')
|
||||
|
||||
##############
|
||||
## HANDLERS ##
|
||||
##############
|
||||
|
|
Loading…
Reference in New Issue