2019-10-08 20:10:42 +00:00
|
|
|
from copy import deepcopy
|
2019-10-11 12:07:57 +00:00
|
|
|
from math import ceil
|
2022-07-21 12:40:09 +00:00
|
|
|
|
2022-07-21 12:40:01 +00:00
|
|
|
from twisted.internet.threads import deferToThread
|
2022-07-21 12:39:59 +00:00
|
|
|
|
2022-07-21 12:40:09 +00:00
|
|
|
import main
|
2022-08-16 21:01:35 +00:00
|
|
|
from modules import helpers
|
2022-08-11 20:44:19 +00:00
|
|
|
from utils.logging.debug import debug, trace
|
2022-07-21 12:40:09 +00:00
|
|
|
from utils.logging.log import error, log, warn
|
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
|
2022-08-16 21:01:35 +00:00
|
|
|
def getAllChannels(net=None):
|
|
|
|
"""
|
|
|
|
Get a list of all channels on all relays.
|
|
|
|
:return: list of channels
|
|
|
|
"""
|
|
|
|
channels = {}
|
|
|
|
if not net:
|
|
|
|
nets = main.network.keys()
|
|
|
|
else:
|
|
|
|
nets = [net]
|
|
|
|
for net in nets:
|
|
|
|
relays = helpers.get_connected_relays(net)
|
|
|
|
for relay in relays:
|
|
|
|
if net not in channels:
|
|
|
|
channels[net] = {}
|
|
|
|
if relay.num not in channels[net]:
|
|
|
|
channels[net][relay.num] = []
|
|
|
|
for channel in relay.channels:
|
|
|
|
channels[net][relay.num].append(channel)
|
|
|
|
# debug(f"getAllChannels(): {channels}")
|
|
|
|
return channels
|
|
|
|
|
|
|
|
|
|
|
|
def getDuplicateChannels(net=None, total=False):
|
|
|
|
"""
|
|
|
|
Get a list of duplicate channels.
|
|
|
|
:return: list of duplicate channels
|
|
|
|
"""
|
|
|
|
allChans = getAllChannels(net)
|
|
|
|
duplicates = {}
|
|
|
|
for net in allChans.keys():
|
|
|
|
net_chans = []
|
|
|
|
inst = {}
|
|
|
|
# add all the channels from this network to a list
|
|
|
|
for num in allChans[net].keys():
|
|
|
|
net_chans.extend(allChans[net][num])
|
|
|
|
for channel in net_chans:
|
|
|
|
count_chan = net_chans.count(channel)
|
|
|
|
# I don't know why but it works
|
|
|
|
# this is used in userinfo.delChannels
|
|
|
|
set_min = 1
|
|
|
|
if total:
|
|
|
|
set_min = 0
|
|
|
|
if count_chan > set_min:
|
|
|
|
inst[channel] = count_chan
|
|
|
|
if inst:
|
|
|
|
duplicates[net] = inst
|
|
|
|
|
|
|
|
if total:
|
|
|
|
return duplicates
|
|
|
|
|
|
|
|
to_part = {}
|
|
|
|
for net in allChans:
|
|
|
|
if net in duplicates:
|
|
|
|
for num in allChans[net].keys():
|
|
|
|
for channel in allChans[net][num]:
|
|
|
|
if channel in duplicates[net].keys():
|
|
|
|
if duplicates[net][channel] > 1:
|
|
|
|
if net not in to_part:
|
|
|
|
to_part[net] = {}
|
|
|
|
if num not in to_part[net]:
|
|
|
|
to_part[net][num] = []
|
|
|
|
to_part[net][num].append(channel)
|
|
|
|
duplicates[net][channel] -= 1
|
|
|
|
|
|
|
|
return to_part
|
|
|
|
|
|
|
|
|
|
|
|
def partChannels(data):
|
|
|
|
for net in data:
|
|
|
|
for num in data[net]:
|
|
|
|
name = f"{net}{num}"
|
|
|
|
if name in main.IRCPool.keys():
|
|
|
|
for channel in data[net][num]:
|
|
|
|
if channel in main.IRCPool[name].channels:
|
|
|
|
main.IRCPool[name].part(channel)
|
|
|
|
log(f"Parted {channel} on {net} - {num}")
|
|
|
|
|
|
|
|
|
2022-08-13 17:40:13 +00:00
|
|
|
def getEnabledRelays(net):
|
|
|
|
"""
|
|
|
|
Get a list of enabled relays for a network.
|
|
|
|
:param net: network
|
|
|
|
:rtype: list of int
|
|
|
|
:return: list of enabled relay numbers
|
|
|
|
"""
|
|
|
|
enabledRelays = [x for x in main.network[net].relays.keys() if main.network[net].relays[x]["enabled"]]
|
2022-08-14 10:41:29 +00:00
|
|
|
# debug(f"getEnabledRelays() {net}: {enabledRelays}")
|
2022-08-13 17:40:13 +00:00
|
|
|
return enabledRelays
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
|
2022-08-14 08:25:54 +00:00
|
|
|
def getConnectedRelays(net):
|
|
|
|
"""
|
|
|
|
Get a list of connected relays for a network.
|
|
|
|
:param net: network
|
|
|
|
:rtype: list of int
|
|
|
|
:return: list of relay numbers
|
|
|
|
"""
|
|
|
|
enabledRelays = getEnabledRelays(net)
|
|
|
|
connectedRelays = []
|
|
|
|
for i in enabledRelays:
|
|
|
|
name = net + str(i)
|
|
|
|
if name in main.IRCPool.keys():
|
|
|
|
if main.IRCPool[name].isconnected:
|
|
|
|
connectedRelays.append(i)
|
2022-08-14 10:41:29 +00:00
|
|
|
# debug(f"getConnectedRelays() {net}: {connectedRelays}")
|
2022-08-14 08:25:54 +00:00
|
|
|
return connectedRelays
|
|
|
|
|
|
|
|
|
2022-08-11 18:22:09 +00:00
|
|
|
def getActiveRelays(net):
|
2022-08-12 22:53:02 +00:00
|
|
|
"""
|
|
|
|
Get a list of active relays for a network.
|
|
|
|
:param net: network
|
|
|
|
:rtype: list of int
|
2022-08-14 08:25:54 +00:00
|
|
|
:return: list of relay numbers
|
2022-08-13 17:40:13 +00:00
|
|
|
"""
|
|
|
|
enabledRelays = getEnabledRelays(net)
|
|
|
|
activeRelays = []
|
|
|
|
for i in enabledRelays:
|
|
|
|
name = net + str(i)
|
|
|
|
if name in main.IRCPool.keys():
|
2022-08-14 10:41:29 +00:00
|
|
|
# debug(
|
|
|
|
# (
|
|
|
|
# f"getActiveRelays() {net}: {i} auth:{main.IRCPool[name].authenticated} "
|
|
|
|
# f"conn:{main.IRCPool[name].isconnected}"
|
|
|
|
# )
|
|
|
|
# )
|
2022-08-13 17:40:13 +00:00
|
|
|
if main.IRCPool[name].authenticated and main.IRCPool[name].isconnected:
|
|
|
|
activeRelays.append(i)
|
2022-08-26 21:17:12 +00:00
|
|
|
debug(f"getActiveRelays() {net}: {activeRelays}")
|
2022-08-11 18:22:09 +00:00
|
|
|
return activeRelays
|
|
|
|
|
|
|
|
|
2022-08-14 09:58:28 +00:00
|
|
|
def relayIsActive(net, num):
|
|
|
|
"""
|
|
|
|
Check if a relay is active.
|
|
|
|
:param net: network
|
|
|
|
:param num: relay number
|
|
|
|
:rtype: bool
|
|
|
|
:return: True if relay is active, False otherwise
|
|
|
|
"""
|
|
|
|
activeRelays = getActiveRelays(net)
|
|
|
|
return num in activeRelays
|
|
|
|
|
|
|
|
|
2019-10-11 12:07:57 +00:00
|
|
|
def allRelaysActive(net):
|
2022-08-11 18:22:09 +00:00
|
|
|
"""
|
|
|
|
Check if all enabled relays are active and authenticated.
|
2022-08-12 22:53:02 +00:00
|
|
|
:param net: network
|
|
|
|
:rtype: bool
|
|
|
|
:return: True if all relays are active and authenticated, False otherwise
|
2022-08-11 18:22:09 +00:00
|
|
|
"""
|
|
|
|
activeRelays = getActiveRelays(net)
|
2022-08-13 17:40:13 +00:00
|
|
|
enabledRelays = getEnabledRelays(net)
|
|
|
|
relaysActive = len(activeRelays) == len(enabledRelays)
|
2022-08-14 10:41:29 +00:00
|
|
|
# debug(f"allRelaysActive() {net}: {relaysActive} ({activeRelays}/{enabledRelays})")
|
2022-08-13 17:40:13 +00:00
|
|
|
return relaysActive
|
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
|
2022-08-13 17:40:13 +00:00
|
|
|
def getAverageChanlimit(net):
|
|
|
|
"""
|
|
|
|
Get the average channel limit for a network.
|
|
|
|
:param net: network
|
|
|
|
:rtype: int
|
|
|
|
:return: average channel limit
|
|
|
|
"""
|
|
|
|
total = 0
|
|
|
|
for i in getActiveRelays(net):
|
2022-07-21 12:39:41 +00:00
|
|
|
name = net + str(i)
|
2019-10-11 12:07:57 +00:00
|
|
|
if name in main.IRCPool.keys():
|
2022-08-13 17:40:13 +00:00
|
|
|
total += main.IRCPool[name].chanlimit
|
|
|
|
avg_chanlimit = total / len(getActiveRelays(net))
|
|
|
|
debug(f"getAverageChanlimit() {net}: {avg_chanlimit}")
|
|
|
|
return avg_chanlimit
|
2019-10-11 12:07:57 +00:00
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
|
2022-08-13 17:40:13 +00:00
|
|
|
def getSumChanlimit(net):
|
|
|
|
"""
|
|
|
|
Get the sum of all channel limits for a network.
|
|
|
|
:param net: network
|
|
|
|
:rtype: int
|
|
|
|
:return: sum of channel limits
|
|
|
|
"""
|
|
|
|
total = 0
|
|
|
|
for i in getActiveRelays(net):
|
|
|
|
name = net + str(i)
|
|
|
|
if name in main.IRCPool.keys():
|
|
|
|
total += main.IRCPool[name].chanlimit
|
|
|
|
return total
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
|
|
|
|
def getChanFree(net):
|
2022-07-27 21:03:42 +00:00
|
|
|
"""
|
|
|
|
Get a dictionary with the free channel spaces for
|
|
|
|
each relay, and a channel limit.
|
|
|
|
Example return:
|
|
|
|
({1: 99}, 100)
|
|
|
|
:param net: network
|
|
|
|
:return: ({relay: channel spaces}, channel limit)
|
|
|
|
"""
|
2019-10-12 20:05:55 +00:00
|
|
|
chanfree = {}
|
2022-08-11 18:22:09 +00:00
|
|
|
for i in getActiveRelays(net):
|
2022-07-21 12:39:41 +00:00
|
|
|
name = net + str(i)
|
2022-07-29 16:28:09 +00:00
|
|
|
if name not in main.IRCPool.keys():
|
|
|
|
continue
|
|
|
|
if not main.IRCPool[name].isconnected:
|
|
|
|
continue
|
2022-07-21 12:39:41 +00:00
|
|
|
chanfree[i] = main.IRCPool[name].chanlimit - len(main.IRCPool[name].channels)
|
2022-08-13 18:20:29 +00:00
|
|
|
|
|
|
|
return chanfree
|
2019-10-12 20:40:50 +00:00
|
|
|
|
2022-08-12 22:32:00 +00:00
|
|
|
|
2022-08-12 21:31:12 +00:00
|
|
|
def getTotalChans(net):
|
2022-08-12 22:53:02 +00:00
|
|
|
"""
|
|
|
|
Get the total number of channels on all relays for a network.
|
|
|
|
:param net: network
|
|
|
|
:rtype: int
|
|
|
|
:return: total number of channels
|
|
|
|
"""
|
2022-08-12 21:31:12 +00:00
|
|
|
total = 0
|
|
|
|
for i in getActiveRelays(net):
|
|
|
|
name = net + str(i)
|
|
|
|
if name in main.IRCPool.keys():
|
|
|
|
total += len(main.IRCPool[name].channels)
|
|
|
|
return total
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
def emptyChanAllocate(net, flist):
|
2022-08-12 22:53:02 +00:00
|
|
|
"""
|
|
|
|
Allocate channels to relays.
|
|
|
|
:param net: network
|
|
|
|
:param flist: list of channels to allocate
|
|
|
|
:param new: list of newly provisioned relays to account for
|
|
|
|
:rtype: dict
|
|
|
|
:return: dictionary of {relay: list of channels}"""
|
|
|
|
|
|
|
|
# Get the free channel spaces for each relay
|
2022-08-13 18:20:29 +00:00
|
|
|
chanfree = getChanFree(net)
|
2019-10-12 20:40:50 +00:00
|
|
|
if not chanfree:
|
|
|
|
return
|
2022-08-12 22:53:02 +00:00
|
|
|
|
|
|
|
# Pretend the newly provisioned relays are already on the network
|
2022-08-13 18:20:29 +00:00
|
|
|
# for i in new:
|
|
|
|
# chanfree[0][i] = chanfree[1]
|
2019-10-12 20:05:55 +00:00
|
|
|
allocated = {}
|
2022-08-12 22:53:02 +00:00
|
|
|
|
|
|
|
newlist = list(flist)
|
2022-08-13 20:54:14 +00:00
|
|
|
chan_slots_used = getTotalChans(net)
|
|
|
|
max_chans = getSumChanlimit(net) - chan_slots_used
|
|
|
|
trunc_list = newlist[:max_chans]
|
2022-08-13 18:20:29 +00:00
|
|
|
debug(f"emptyChanAllocate() {net}: newlist:{len(newlist)} trunc_list:{len(trunc_list)}")
|
|
|
|
|
|
|
|
for i in chanfree.keys():
|
|
|
|
for x in range(chanfree[i]):
|
|
|
|
if not len(trunc_list):
|
2019-10-12 20:05:55 +00:00
|
|
|
break
|
|
|
|
if i in allocated.keys():
|
2022-08-13 18:20:29 +00:00
|
|
|
allocated[i].append(trunc_list.pop())
|
2019-10-12 20:05:55 +00:00
|
|
|
else:
|
2022-08-13 18:20:29 +00:00
|
|
|
allocated[i] = [trunc_list.pop()]
|
2019-10-12 20:05:55 +00:00
|
|
|
return allocated
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
def populateChans(net, clist):
|
2022-08-12 22:53:02 +00:00
|
|
|
"""
|
|
|
|
Populate channels on relays.
|
|
|
|
Stores channels to join in a list in main.TempChan[net][num]
|
|
|
|
:param net: network
|
|
|
|
:param clist: list of channels to join
|
|
|
|
:param new: list of newly provisioned relays to account for"""
|
2022-07-21 12:39:41 +00:00
|
|
|
# divided = array_split(clist, relay)
|
2022-08-13 18:20:29 +00:00
|
|
|
allocated = emptyChanAllocate(net, clist)
|
2022-08-13 20:54:14 +00:00
|
|
|
trace(f"populateChans() allocated:{allocated}")
|
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]}
|
2022-08-13 20:54:14 +00:00
|
|
|
trace(f"populateChans() TempChan {net}{i}: {allocated[i]}")
|
2019-10-11 12:07:57 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2019-10-11 12:07:57 +00:00
|
|
|
def notifyJoin(net):
|
2022-08-12 22:53:02 +00:00
|
|
|
"""
|
|
|
|
Notify relays to join channels.
|
|
|
|
They will pull from main.TempChan and remove channels they join.
|
|
|
|
:param net: network
|
|
|
|
"""
|
2022-08-11 18:22:09 +00:00
|
|
|
for i in getActiveRelays(net):
|
2022-07-21 12:39:41 +00:00
|
|
|
name = net + str(i)
|
2019-10-11 12:07:57 +00:00
|
|
|
if name in main.IRCPool.keys():
|
2022-08-13 20:54:14 +00:00
|
|
|
trace(f"notifyJoin() {name}")
|
2019-10-11 12:07:57 +00:00
|
|
|
main.IRCPool[name].checkChannels()
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2022-08-13 20:54:14 +00:00
|
|
|
def minifyChans(net, listinfo, as_list=False):
|
2022-08-12 22:53:02 +00:00
|
|
|
"""
|
|
|
|
Remove channels from listinfo that are already covered by a relay.
|
|
|
|
:param net: network
|
|
|
|
:param listinfo: list of channels to check
|
|
|
|
:type listinfo: list of [channel, num_users]
|
|
|
|
:return: list of channels with joined channels removed
|
|
|
|
:rtype: list of [channel, num_users]
|
|
|
|
"""
|
2022-08-13 20:54:14 +00:00
|
|
|
# We want to make this reusable for joining a bunch of channels.
|
|
|
|
if as_list:
|
|
|
|
channel_list = listinfo
|
|
|
|
|
2019-10-12 20:05:55 +00:00
|
|
|
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
|
2022-08-16 21:01:35 +00:00
|
|
|
for i in getConnectedRelays(net):
|
2022-07-21 12:39:41 +00:00
|
|
|
name = net + str(i)
|
2019-10-12 20:05:55 +00:00
|
|
|
for x in main.IRCPool[name].channels:
|
2022-08-13 20:54:14 +00:00
|
|
|
if as_list:
|
|
|
|
for y in channel_list:
|
|
|
|
if y == x:
|
|
|
|
channel_list.remove(y)
|
|
|
|
else:
|
|
|
|
for y in listinfo:
|
|
|
|
if y[0] == x:
|
|
|
|
listinfo.remove(y)
|
|
|
|
if not as_list:
|
|
|
|
if not listinfo:
|
|
|
|
log("We're on all the channels we want to be on, dropping LIST")
|
|
|
|
return False
|
|
|
|
if as_list:
|
|
|
|
return channel_list
|
|
|
|
else:
|
|
|
|
return listinfo
|
2019-10-12 20:05:55 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
def keepChannels(net, listinfo, mean, sigrelay, relay):
|
2022-08-12 22:53:02 +00:00
|
|
|
"""
|
|
|
|
Minify channels, determine whether we can cover all the channels
|
|
|
|
on the network, or need to use 'significant' mode.
|
|
|
|
Truncate the channel list to available channel spaces.
|
|
|
|
Allocate these channels to relays.
|
|
|
|
Notify relays that they should pull from TempChan to join.
|
|
|
|
:param net: network
|
|
|
|
:param listinfo: list of [channel, num_users] lists
|
|
|
|
:param mean: mean of channel population
|
|
|
|
:param sigrelay: number of relays needed to cover significant channels
|
|
|
|
:param relay: number of relays needed to cover all channels
|
|
|
|
:param chanlimit: maximum number of channels to allocate to a relay
|
|
|
|
"""
|
2019-10-12 20:05:55 +00:00
|
|
|
listinfo = minifyChans(net, listinfo)
|
|
|
|
if not listinfo:
|
|
|
|
return
|
2022-07-21 12:40:01 +00:00
|
|
|
if relay <= main.config["ChanKeep"]["SigSwitch"]: # we can cover all of the channels
|
2019-10-11 12:07:57 +00:00
|
|
|
coverAll = True
|
2022-07-21 12:40:01 +00:00
|
|
|
elif relay > main.config["ChanKeep"]["SigSwitch"]: # we cannot cover all of the channels
|
2019-10-11 12:07:57 +00:00
|
|
|
coverAll = False
|
2022-08-13 18:20:29 +00:00
|
|
|
# if not sigrelay <= main.config["ChanKeep"]["MaxRelay"]:
|
|
|
|
# error("Network %s is too big to cover: %i relays required" % (net, sigrelay))
|
|
|
|
# return
|
2022-08-12 21:27:49 +00:00
|
|
|
num_instances = len(getActiveRelays(net))
|
2022-08-13 18:20:29 +00:00
|
|
|
debug(f"keepChannels() {net} instances:{num_instances}")
|
2022-08-12 21:31:12 +00:00
|
|
|
chan_slots_used = getTotalChans(net)
|
|
|
|
debug(f"keepChannels() slots_used:{chan_slots_used}")
|
2022-08-13 18:20:29 +00:00
|
|
|
# max_chans = (chanlimit * num_instances) - chan_slots_used
|
|
|
|
max_chans = getSumChanlimit(net) - chan_slots_used
|
2022-08-13 19:37:21 +00:00
|
|
|
if max_chans < 0:
|
|
|
|
max_chans = 0
|
2022-08-12 21:31:12 +00:00
|
|
|
debug(f"keepChannels() max_chans:{max_chans}")
|
2019-10-11 12:07:57 +00:00
|
|
|
if coverAll:
|
2022-08-13 18:20:29 +00:00
|
|
|
# needed = relay - len(getActiveRelays(net))
|
|
|
|
# if needed:
|
|
|
|
# debug(f"keepChannels() coverAll asking to provision {needed} relays for {net} relay:{relay}")
|
|
|
|
# newNums = modules.provision.provisionMultipleRelays(net, needed)
|
|
|
|
# else:
|
|
|
|
# newNums = []
|
2022-08-14 23:36:36 +00:00
|
|
|
listinfo_sort = sorted(listinfo, reverse=True, key=lambda x: x[1])
|
|
|
|
if len(listinfo_sort) > max_chans:
|
|
|
|
max_chans = len(listinfo_sort) - 1
|
|
|
|
|
|
|
|
flist = [i[0] for i in listinfo_sort]
|
|
|
|
|
|
|
|
flist = flist[:max_chans]
|
|
|
|
debug(f"keepChannels() {net}: joining {len(flist)}/{len(listinfo_sort)} channels")
|
|
|
|
trace(f"keepChannels() {net}: joining:{flist}")
|
|
|
|
populateChans(net, flist)
|
2019-10-11 12:07:57 +00:00
|
|
|
else:
|
2022-08-13 18:20:29 +00:00
|
|
|
# needed = sigrelay - len(getActiveRelays(net))
|
|
|
|
# if needed:
|
|
|
|
# debug(f"keepChannels() NOT coverAll asking to provision {needed} relays for {net} sigrelay:{sigrelay}")
|
|
|
|
# newNums = modules.provision.provisionMultipleRelays(net, needed)
|
|
|
|
# else:
|
|
|
|
# newNums = []
|
2022-08-14 23:36:36 +00:00
|
|
|
listinfo_sort = sorted(listinfo, reverse=True, key=lambda x: x[1])
|
2022-08-15 18:15:00 +00:00
|
|
|
trace(f"keepChannels() {net}: listinfo_sort:{listinfo_sort}")
|
2022-08-14 23:36:36 +00:00
|
|
|
if len(listinfo_sort) > max_chans:
|
|
|
|
max_chans = len(listinfo_sort) - 1
|
2022-08-14 23:39:22 +00:00
|
|
|
debug(f"keepChannels() {net}: new max_chans:{max_chans}")
|
2022-08-14 23:36:36 +00:00
|
|
|
|
|
|
|
siglist = [i[0] for i in listinfo if int(i[1]) > mean]
|
2022-08-15 18:15:00 +00:00
|
|
|
trace(f"keepChannels() {net}: new siglist:{siglist}")
|
2022-08-14 23:36:36 +00:00
|
|
|
|
|
|
|
siglist = siglist[:max_chans]
|
2022-08-15 18:15:00 +00:00
|
|
|
trace(f"keepChannels() {net}: truncated siglist:{siglist}")
|
2022-08-14 23:36:36 +00:00
|
|
|
|
2022-08-15 18:15:00 +00:00
|
|
|
trace(f"keepChannels() {net}: siglist:{siglist} max_chans:{max_chans} len_sig:{len(listinfo_sort)}")
|
2022-08-14 23:36:36 +00:00
|
|
|
debug(f"keepChannels() {net}: joining {len(siglist)}/{len(listinfo_sort)} channels")
|
|
|
|
trace(f"keepChannels() {net}: joining:{siglist}")
|
|
|
|
populateChans(net, siglist)
|
2019-10-11 12:07:57 +00:00
|
|
|
notifyJoin(net)
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2020-11-02 20:14:02 +00:00
|
|
|
def joinSingle(net, channel):
|
2022-08-12 22:53:02 +00:00
|
|
|
"""
|
|
|
|
Join a channel on a relay.
|
|
|
|
Use ECA to determine which relay to join on.
|
|
|
|
:param net: network
|
|
|
|
:param channel: channel to join
|
|
|
|
:return: relay number that joined the channel
|
|
|
|
:rtype: int
|
|
|
|
"""
|
2022-08-13 20:40:53 +00:00
|
|
|
if "," in channel:
|
|
|
|
channels = channel.split(",")
|
2022-08-13 20:54:14 +00:00
|
|
|
channels = minifyChans(net, channels, as_list=True)
|
|
|
|
|
2022-08-13 20:40:53 +00:00
|
|
|
else:
|
2022-08-13 20:54:14 +00:00
|
|
|
channels = [channel]
|
|
|
|
populateChans(net, channels)
|
|
|
|
notifyJoin(net)
|
|
|
|
return True
|
2020-11-02 20:14:02 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2022-07-27 21:03:42 +00:00
|
|
|
def partSingle(net, channel):
|
|
|
|
"""
|
|
|
|
Iterate over all the relays of net and part channels matching channel.
|
2022-08-12 22:53:02 +00:00
|
|
|
:param net: network
|
|
|
|
:param channel: channel to part
|
|
|
|
:return: list of relays that parted the channel
|
|
|
|
:rtype: list of str
|
2022-07-27 21:03:42 +00:00
|
|
|
"""
|
|
|
|
parted = []
|
2022-08-14 08:25:54 +00:00
|
|
|
for i in getConnectedRelays(net):
|
2022-07-27 21:03:42 +00:00
|
|
|
name = f"{net}{i}"
|
|
|
|
if name in main.IRCPool.keys():
|
|
|
|
if channel in main.IRCPool[name].channels:
|
|
|
|
main.IRCPool[name].part(channel)
|
|
|
|
parted.append(str(i))
|
|
|
|
return parted
|
|
|
|
|
|
|
|
|
2019-10-08 20:10:42 +00:00
|
|
|
def nukeNetwork(net):
|
2022-08-12 22:53:02 +00:00
|
|
|
"""
|
|
|
|
Remove network records.
|
|
|
|
:param net: network"""
|
2022-07-21 12:39:41 +00:00
|
|
|
# purgeRecords(net)
|
|
|
|
# p = main.g.pipeline()
|
|
|
|
main.g.delete("analytics.list." + net)
|
|
|
|
# p.delete("list."+net)
|
|
|
|
# p.execute()
|
2019-10-12 20:05:55 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
|
|
|
# def nukeNetwork(net):
|
2019-10-12 20:05:55 +00:00
|
|
|
# deferToThread(_nukeNetwork, net)
|
|
|
|
|
2019-10-08 20:10:42 +00:00
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
def _initialList(net, num, listinfo):
|
2022-08-12 22:53:02 +00:00
|
|
|
"""
|
|
|
|
Called when a relay receives a full LIST response.
|
|
|
|
Run statistics to determine how many channels are significant.
|
|
|
|
This is done by adding all the numbers of users on the channels together,
|
|
|
|
then dividing by the number of channels.
|
|
|
|
* cumul - cumulative sum of all channel membership
|
|
|
|
* siglength - number of significant channels
|
|
|
|
* listlength - number of channels in the list
|
|
|
|
* sigrelay - number of relays needed to cover siglength
|
|
|
|
* relay - number of relays needed to cover all channels
|
|
|
|
:param net: network
|
|
|
|
:param num: relay number
|
|
|
|
:param listinfo: list of [channel, num_users] lists
|
|
|
|
:param chanlimit: maximum number of channels the relay can join
|
|
|
|
"""
|
2019-10-08 20:10:42 +00:00
|
|
|
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
|
2022-07-21 12:39:41 +00:00
|
|
|
mean = round(cumul / listlength, 2)
|
2019-10-08 20:10:42 +00:00
|
|
|
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
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
avg_chanlimit = getAverageChanlimit(net)
|
|
|
|
sigrelay = ceil(siglength / avg_chanlimit)
|
|
|
|
relay = ceil(listlength / avg_chanlimit)
|
2022-08-12 22:53:02 +00:00
|
|
|
|
2022-08-13 19:37:21 +00:00
|
|
|
cur_relays = len(getActiveRelays(net))
|
|
|
|
sig_relays_missing = sigrelay - cur_relays
|
|
|
|
all_relays_missing = relay - cur_relays
|
|
|
|
|
2019-10-08 20:10:42 +00:00
|
|
|
abase = "analytics.list.%s" % net
|
2022-08-13 12:27:20 +00:00
|
|
|
main.g.delete(abase)
|
2019-10-08 20:10:42 +00:00
|
|
|
p = main.g.pipeline()
|
2022-08-12 22:53:02 +00:00
|
|
|
|
|
|
|
# See docstring for meanings
|
2019-10-08 20:10:42 +00:00
|
|
|
p.hset(abase, "mean", mean)
|
2022-08-13 12:27:20 +00:00
|
|
|
p.hset(abase, "total_chans", listlength)
|
|
|
|
p.hset(abase, "big_chans", siglength)
|
|
|
|
p.hset(abase, "small_chans", insiglength)
|
|
|
|
p.hset(abase, "big_chan_perc", round(siglength / listlength * 100, 2))
|
|
|
|
p.hset(abase, "small_chan_perc", round(insiglength / listlength * 100, 2))
|
|
|
|
p.hset(abase, "total_cumul_mem", cumul)
|
|
|
|
p.hset(abase, "big_chan_cumul_mem", sigcumul)
|
|
|
|
p.hset(abase, "small_chan_cumul_mem", insigcumul)
|
|
|
|
p.hset(abase, "relays_for_all_chans", relay)
|
|
|
|
p.hset(abase, "relays_for_big_chans", sigrelay)
|
2022-08-13 18:20:29 +00:00
|
|
|
p.hset(abase, "relays_for_small_chans", ceil(insiglength / avg_chanlimit))
|
2022-08-13 19:37:21 +00:00
|
|
|
p.hset(abase, "sig_relays_missing", sig_relays_missing)
|
|
|
|
p.hset(abase, "all_relays_missing", all_relays_missing)
|
2022-08-11 19:32:49 +00:00
|
|
|
debug(
|
|
|
|
(
|
|
|
|
f"_initialList() net:{net} num:{num} listlength:{listlength} "
|
|
|
|
f"mean:{mean} siglength:{siglength} insiglength:{insiglength} "
|
2022-08-13 18:20:29 +00:00
|
|
|
f"sigrelay:{sigrelay} relay:{relay} avg_chanlimit:{avg_chanlimit}"
|
2022-08-11 19:32:49 +00:00
|
|
|
)
|
|
|
|
)
|
2019-10-12 20:05:55 +00:00
|
|
|
|
2019-10-08 20:10:42 +00:00
|
|
|
# Purge existing records before writing
|
2022-07-21 12:39:41 +00:00
|
|
|
# purgeRecords(net)
|
|
|
|
# for i in listinfo:
|
2019-10-12 20:05:55 +00:00
|
|
|
# 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)
|
2022-08-13 18:20:29 +00:00
|
|
|
keepChannels(net, listinfo, mean, sigrelay, relay)
|
2022-08-12 21:27:49 +00:00
|
|
|
|
|
|
|
# return (listinfo, mean, sigrelay, relay)
|
2019-10-08 20:10:42 +00:00
|
|
|
|
2022-08-13 12:32:22 +00:00
|
|
|
|
2022-08-13 12:27:20 +00:00
|
|
|
def convert(data):
|
|
|
|
"""
|
|
|
|
Recursively convert a dictionary.
|
|
|
|
"""
|
|
|
|
if isinstance(data, bytes):
|
|
|
|
return data.decode("ascii")
|
|
|
|
if isinstance(data, dict):
|
|
|
|
return dict(map(convert, data.items()))
|
|
|
|
if isinstance(data, tuple):
|
|
|
|
return map(convert, data)
|
|
|
|
if isinstance(data, list):
|
|
|
|
return list(map(convert, data))
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def getListInfo(net):
|
|
|
|
abase = f"analytics.list.{net}"
|
|
|
|
info = main.g.hgetall(abase)
|
|
|
|
return convert(info)
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2022-08-13 18:20:29 +00:00
|
|
|
def initialList(net, num, listinfo):
|
2022-08-12 21:27:49 +00:00
|
|
|
"""
|
2022-08-12 22:53:02 +00:00
|
|
|
Run _initialList in a thread.
|
|
|
|
See above docstring.
|
2022-08-12 21:27:49 +00:00
|
|
|
"""
|
2022-08-13 18:20:29 +00:00
|
|
|
deferToThread(_initialList, net, num, deepcopy(listinfo))
|