diff --git a/modules/chankeep.py b/modules/chankeep.py index 0f18e9b..0ed89ac 100644 --- a/modules/chankeep.py +++ b/modules/chankeep.py @@ -1,45 +1,82 @@ import main from utils.logging.log import * +from utils.logging.debug import * +from copy import deepcopy +from twisted.internet.threads import deferToThread -# make this into a threaded function -def initialList(net, num, listinfo, chanLimit): +def provisionInstances(net, relaysNeeded): + #num, alias = + pass + +def purgeRecords(net): + base = "list.%s" % net + p = main.g.pipeline() + existingChans = main.g.smembers("list."+net) + for i in existingChans: + p.delete(base+"."+i.decode("utf-8")) + p.execute() + +def _nukeNetwork(net): + purgeRecords(net) + p = main.g.pipeline() + p.delete("analytics.list."+net) + p.delete("list."+net) + p.execute() + +def nukeNetwork(net): + deferToThread(_nukeNetwork, net) + +def _initialList(net, num, listinfo, chanlimit): #listinfo = sorted(listinfo, key=lambda x: xdd[0]) - listLength = len(listinfo) + listlength = len(listinfo) 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 - mean = cumul/listLength - sigLength = 0 - insigLength = 0 - sigCumul = 0 - insigCumul = 0 + mean = round(cumul/listlength, 2) + siglength = 0 + insiglength = 0 + sigcumul = 0 + insigcumul = 0 for i in listinfo: if int(i[1]) > mean: - sigLength += 1 - sigCumul += int(i[1]) + siglength += 1 + sigcumul += int(i[1]) elif int(i[1]) < mean: - insigLength += 1 - insigCumul += int(i[1]) + insiglength += 1 + insigcumul += int(i[1]) if not net in main.network.keys(): warn("Cannot write list info - no network entry for %s" % net) return - main.network[net].list["mean"] = mean - main.network[net].list["total"] = listLength - main.network[net].list["sigtotal"] = sigLength - main.network[net].list["insigtotal"] = insigLength - main.network[net].list["sigperc"] = sigLength/listLength*100 - main.network[net].list["insigperc"] = insigLength/listLength*100 - main.network[net].list["cumul"] = cumul - main.network[net].list["sigcumul"] = sigCumul - main.network[net].list["insigcumul"] = insigCumul - main.network[net].list["relay"] = listLength/chanLimit - main.network[net].list["sigrelay"] = sigLength/chanLimit - main.network[net].list["insigrelay"] = insigLength/chanLimit + sigrelay = round(siglength/chanlimit, 2) + 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) + p.hset(abase, "relay", round(listlength/chanlimit, 2)) + p.hset(abase, "sigrelay", sigrelay) + p.hset(abase, "insigrelay", round(insiglength/chanlimit, 2)) + # Purge existing records before writing + 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]) + + p.execute() + debug("List parsing completed on %s" % net) + +def initialList(net, num, listinfo, chanlimit): + deferToThread(_initialList, net, num, deepcopy(listinfo), chanlimit) - # Rounding - main.network[net].list = {x: round(y, 4) for x, y in main.network[net].list.items()} - main.saveConf("network") diff --git a/modules/network.py b/modules/network.py index a958cc1..3758460 100644 --- a/modules/network.py +++ b/modules/network.py @@ -2,6 +2,7 @@ from twisted.internet.ssl import DefaultOpenSSLContextFactory import json from modules import alias +from modules.chankeep import nukeNetwork from twisted.internet import reactor from core.bot import IRCBot, IRCBotFactory import main @@ -19,7 +20,6 @@ class Network: self.last = 0 self.relays = {} self.aliases = {} - self.list = {} def add_relay(self, num=None): if not num: @@ -60,6 +60,7 @@ class Network: def seppuku(self): # Removes all bots in preperation for deletion self.killAliases(self.relays.keys()) + nukeNetwork(self.net) def start_bot(self, num): # a single name is given to relays in the backend