2019-10-06 20:10:44 +00:00
|
|
|
import main
|
2019-10-08 17:15:23 +00:00
|
|
|
from utils.logging.log import *
|
2019-10-08 20:10:42 +00:00
|
|
|
from utils.logging.debug import *
|
|
|
|
from copy import deepcopy
|
2019-10-11 12:07:57 +00:00
|
|
|
from math import ceil
|
|
|
|
from modules.provision import provisionMultipleRelays
|
2019-10-08 20:10:42 +00:00
|
|
|
from twisted.internet.threads import deferToThread
|
2019-10-08 17:15:23 +00:00
|
|
|
|
2019-10-11 12:07:57 +00:00
|
|
|
def allRelaysActive(net):
|
|
|
|
relayNum = len(main.network[net].relays.keys())
|
|
|
|
existNum = 0
|
|
|
|
for i in main.network[net].relays.keys():
|
|
|
|
name = net+str(i)
|
|
|
|
if name in main.IRCPool.keys():
|
2020-05-31 20:52:56 +00:00
|
|
|
if main.IRCPool[name].authenticated and main.network[net].relays[i]["registered"]:
|
2019-10-12 20:05:55 +00:00
|
|
|
existNum += 1
|
2019-10-11 12:07:57 +00:00
|
|
|
if existNum == relayNum:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-10-12 20:40:50 +00:00
|
|
|
def getChanFree(net, new):
|
2019-10-12 20:05:55 +00:00
|
|
|
chanfree = {}
|
2019-10-12 20:40:50 +00:00
|
|
|
chanlimits = set()
|
2019-10-12 20:05:55 +00:00
|
|
|
for i in main.network[net].relays.keys():
|
2019-10-12 20:40:50 +00:00
|
|
|
if i in new:
|
|
|
|
continue
|
2019-10-12 20:05:55 +00:00
|
|
|
name = net+str(i)
|
|
|
|
chanfree[i] = main.IRCPool[name].chanlimit-len(main.IRCPool[name].channels)
|
2019-10-12 20:40:50 +00:00
|
|
|
chanlimits.add(main.IRCPool[name].chanlimit)
|
|
|
|
if not len(chanlimits) == 1:
|
2019-10-31 15:44:59 +00:00
|
|
|
error("Network %s has servers with different CHANLIMIT values" % net)
|
2019-10-12 20:40:50 +00:00
|
|
|
return False
|
|
|
|
return (chanfree, chanlimits.pop())
|
|
|
|
|
|
|
|
def emptyChanAllocate(net, flist, relay, new):
|
|
|
|
chanfree = getChanFree(net, new)
|
|
|
|
if not chanfree:
|
|
|
|
return
|
|
|
|
for i in new:
|
|
|
|
chanfree[0][i] = chanfree[1]
|
2019-10-12 20:05:55 +00:00
|
|
|
allocated = {}
|
|
|
|
toalloc = len(flist)
|
2019-10-12 20:40:50 +00:00
|
|
|
if toalloc > sum(chanfree[0].values()):
|
2019-10-12 20:05:55 +00:00
|
|
|
error("Too many channels to allocate for %s - this is probably a bug" % net)
|
|
|
|
return False
|
2019-10-12 20:40:50 +00:00
|
|
|
for i in chanfree[0].keys():
|
|
|
|
for x in range(chanfree[0][i]):
|
2019-10-12 20:05:55 +00:00
|
|
|
if not len(flist):
|
|
|
|
break
|
|
|
|
if i in allocated.keys():
|
|
|
|
allocated[i].append(flist.pop())
|
|
|
|
else:
|
|
|
|
allocated[i] = [flist.pop()]
|
|
|
|
return allocated
|
|
|
|
|
2019-10-12 20:40:50 +00:00
|
|
|
def populateChans(net, clist, relay, new):
|
2019-10-12 20:05:55 +00:00
|
|
|
#divided = array_split(clist, relay)
|
2019-10-12 20:40:50 +00:00
|
|
|
allocated = emptyChanAllocate(net, clist, relay, new)
|
2019-10-12 20:05:55 +00:00
|
|
|
if not allocated:
|
|
|
|
return
|
|
|
|
for i in allocated.keys():
|
2019-10-11 12:07:57 +00:00
|
|
|
if net in main.TempChan.keys():
|
2019-10-12 20:05:55 +00:00
|
|
|
main.TempChan[net][i] = allocated[i]
|
2019-10-11 12:07:57 +00:00
|
|
|
else:
|
2019-10-12 20:05:55 +00:00
|
|
|
main.TempChan[net] = {i: allocated[i]}
|
2019-10-11 12:07:57 +00:00
|
|
|
|
|
|
|
def notifyJoin(net):
|
|
|
|
for i in main.network[net].relays.keys():
|
|
|
|
name = net+str(i)
|
|
|
|
if name in main.IRCPool.keys():
|
|
|
|
main.IRCPool[name].checkChannels()
|
|
|
|
|
2019-10-12 20:05:55 +00:00
|
|
|
def minifyChans(net, listinfo):
|
|
|
|
if not allRelaysActive(net):
|
2019-10-31 15:44:59 +00:00
|
|
|
error("All relays for %s are not active, cannot minify list" % net)
|
2019-10-12 20:05:55 +00:00
|
|
|
return False
|
|
|
|
for i in main.network[net].relays.keys():
|
|
|
|
name = net+str(i)
|
|
|
|
for x in main.IRCPool[name].channels:
|
|
|
|
for y in listinfo:
|
|
|
|
if y[0] == x:
|
|
|
|
listinfo.remove(y)
|
|
|
|
if not listinfo:
|
|
|
|
log("We're on all the channels we want to be on, dropping LIST")
|
|
|
|
return False
|
|
|
|
return listinfo
|
|
|
|
|
2019-10-11 12:07:57 +00:00
|
|
|
def keepChannels(net, listinfo, mean, sigrelay, relay):
|
2019-10-12 20:05:55 +00:00
|
|
|
listinfo = minifyChans(net, listinfo)
|
|
|
|
if not listinfo:
|
|
|
|
return
|
2019-10-11 12:07:57 +00:00
|
|
|
if relay <= main.config["ChanKeep"]["SigSwitch"]: # we can cover all of the channels
|
|
|
|
coverAll = True
|
|
|
|
elif relay > main.config["ChanKeep"]["SigSwitch"]: # we cannot cover all of the channels
|
|
|
|
coverAll = False
|
|
|
|
if not sigrelay <= main.config["ChanKeep"]["MaxRelay"]:
|
|
|
|
error("Network %s is too big to cover: %i relays required" % (net, sigrelay))
|
|
|
|
return
|
|
|
|
if coverAll:
|
|
|
|
needed = relay-len(main.network[net].relays.keys())
|
2019-10-12 20:40:50 +00:00
|
|
|
newNums = provisionMultipleRelays(net, needed)
|
2019-10-11 12:07:57 +00:00
|
|
|
flist = [i[0] for i in listinfo]
|
2019-10-12 20:40:50 +00:00
|
|
|
populateChans(net, flist, relay, newNums)
|
2019-10-11 12:07:57 +00:00
|
|
|
else:
|
|
|
|
needed = sigrelay-len(main.network[net].relays.keys())
|
2019-10-12 20:40:50 +00:00
|
|
|
newNums = provisionMultipleRelays(net, needed)
|
2019-10-11 12:07:57 +00:00
|
|
|
siglist = [i[0] for i in listinfo if int(i[1]) > mean]
|
2019-10-12 20:40:50 +00:00
|
|
|
populateChans(net, siglist, sigrelay, newNums)
|
2019-10-11 12:07:57 +00:00
|
|
|
notifyJoin(net)
|
|
|
|
|
2019-10-08 20:10:42 +00:00
|
|
|
def nukeNetwork(net):
|
2019-10-12 20:05:55 +00:00
|
|
|
#purgeRecords(net)
|
|
|
|
#p = main.g.pipeline()
|
|
|
|
main.g.delete("analytics.list."+net)
|
|
|
|
#p.delete("list."+net)
|
|
|
|
#p.execute()
|
|
|
|
|
|
|
|
#def nukeNetwork(net):
|
|
|
|
# deferToThread(_nukeNetwork, net)
|
|
|
|
|
2019-10-08 20:10:42 +00:00
|
|
|
|
|
|
|
def _initialList(net, num, listinfo, chanlimit):
|
|
|
|
listlength = len(listinfo)
|
2019-10-08 17:15:23 +00:00
|
|
|
cumul = 0
|
|
|
|
try:
|
|
|
|
cumul += sum(int(i[1]) for i in listinfo)
|
|
|
|
except TypeError:
|
|
|
|
warn("Bad LIST data received from %s - %i" % (net, num))
|
|
|
|
return
|
2019-10-08 20:10:42 +00:00
|
|
|
mean = round(cumul/listlength, 2)
|
|
|
|
siglength = 0
|
|
|
|
insiglength = 0
|
|
|
|
sigcumul = 0
|
|
|
|
insigcumul = 0
|
2019-10-08 17:15:23 +00:00
|
|
|
for i in listinfo:
|
|
|
|
if int(i[1]) > mean:
|
2019-10-08 20:10:42 +00:00
|
|
|
siglength += 1
|
|
|
|
sigcumul += int(i[1])
|
2019-10-08 17:15:23 +00:00
|
|
|
elif int(i[1]) < mean:
|
2019-10-08 20:10:42 +00:00
|
|
|
insiglength += 1
|
|
|
|
insigcumul += int(i[1])
|
2019-10-08 17:15:23 +00:00
|
|
|
|
2019-10-11 12:07:57 +00:00
|
|
|
sigrelay = ceil(siglength/chanlimit)
|
|
|
|
relay = ceil(listlength/chanlimit)
|
2019-10-08 20:10:42 +00:00
|
|
|
netbase = "list.%s" % net
|
|
|
|
abase = "analytics.list.%s" % net
|
|
|
|
p = main.g.pipeline()
|
|
|
|
p.hset(abase, "mean", mean)
|
|
|
|
p.hset(abase, "total", listlength)
|
|
|
|
p.hset(abase, "sigtotal", siglength)
|
|
|
|
p.hset(abase, "insigtotal", insiglength)
|
|
|
|
p.hset(abase, "sigperc", round(siglength/listlength*100, 2))
|
|
|
|
p.hset(abase, "insigperc", round(insiglength/listlength*100, 2))
|
|
|
|
p.hset(abase, "cumul", cumul)
|
|
|
|
p.hset(abase, "sigcumul", sigcumul)
|
|
|
|
p.hset(abase, "insigcumul", insigcumul)
|
2019-10-11 12:07:57 +00:00
|
|
|
p.hset(abase, "relay", relay)
|
2019-10-08 20:10:42 +00:00
|
|
|
p.hset(abase, "sigrelay", sigrelay)
|
2019-10-11 12:07:57 +00:00
|
|
|
p.hset(abase, "insigrelay", ceil(insiglength/chanlimit))
|
2019-10-12 20:05:55 +00:00
|
|
|
|
2019-10-08 20:10:42 +00:00
|
|
|
# Purge existing records before writing
|
2019-10-12 20:05:55 +00:00
|
|
|
#purgeRecords(net)
|
|
|
|
#for i in listinfo:
|
|
|
|
# p.rpush(netbase+"."+i[0], i[1])
|
|
|
|
# p.rpush(netbase+"."+i[0], i[2])
|
|
|
|
# p.sadd(netbase, i[0])
|
2019-10-08 20:10:42 +00:00
|
|
|
|
|
|
|
p.execute()
|
|
|
|
debug("List parsing completed on %s" % net)
|
2019-10-11 12:07:57 +00:00
|
|
|
keepChannels(net, listinfo, mean, sigrelay, relay)
|
2019-10-08 20:10:42 +00:00
|
|
|
|
|
|
|
def initialList(net, num, listinfo, chanlimit):
|
|
|
|
deferToThread(_initialList, net, num, deepcopy(listinfo), chanlimit)
|
|
|
|
|