Files @ 870c5c6c334f
Branch filter:

Location: Morevna/src/stats.py

Laman
reacquiring old locks
class Stats:
	def __init__(self):
		self.received = 0
		self.sent = 0
		self.exchanged_nodes = 0
		self.transferred_blocks = 0


stats = Stats()


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


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


def log_exchanged_node(k=1):
	stats.exchanged_nodes += k


def log_transferred_block(k=1):
	stats.transferred_blocks += 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=format_bytes(stats.received),
		r=stats.received,
		sf=format_bytes(stats.sent),
		s=stats.sent,
		nodes=stats.exchanged_nodes,
		blocks=stats.transferred_blocks
	)


def format_bytes(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])