Files @ 5c80ca07f00c
Branch filter:

Location: Morevna/src/stats.py

Laman
reformatted whitespace with more respect for PEP-8
class Stats:
	def __init__(self):
		self.received = 0
		self.sent = 0
		self.exchangedNodes = 0
		self.transferredBlocks = 0

stats = Stats()


def logReceived(data):
	stats.received += len(data)


def logSent(data):
	stats.sent += len(data)


def logExchangedNode(k=1):
	stats.exchangedNodes += k


def logTransferredBlock(k=1):
	stats.transferredBlocks += k


def reset():
	global stats
	stats = Stats()


def report():
	return """received {rf} ({r:,} B)
sent {sf} ({s:,} B)
exchanged {nodes:,} hash tree nodes
transferred {blocks:,} blocks""".format(
		rf=formatBytes(stats.received),
		r=stats.received,
		sf=formatBytes(stats.sent),
		s=stats.sent,
		nodes=stats.exchangedNodes,
		blocks=stats.transferredBlocks
	)


def formatBytes(x):
	exts = ["B", "kiB", "MiB", "GiB", "TiB", "PiB"]
	i = 0
	while x>1024:
		x /= 1024
		i += 1
	if x>=100: x = round(x)
	elif x>=10: x = round(x, 1)
	else: x = round(x, 2)
	return "{0} {1}".format(x, exts[i])