Improve nucypher status locked-tokens: shows bars with min & max

pull/1259/head
David Núñez 2019-09-04 23:15:52 +02:00
parent beb0e04d86
commit b416b74381
2 changed files with 27 additions and 8 deletions

View File

@ -17,6 +17,7 @@ along with nucypher. If not, see <https://www.gnu.org/licenses/>.
import time
import webbrowser
from collections import Counter
from decimal import Decimal
from typing import List
@ -535,11 +536,11 @@ def paint_stakers(emitter, stakers: List[str], agent) -> None:
emitter.echo(f"{tab} {'Worker:':10} {worker}\n")
def paint_locked_tokens_status(emitter, agent) -> None:
emitter.echo("\n| Locked Tokens |\n")
def paint_locked_tokens_status(emitter, agent, periods) -> None:
from collections import Counter
token_counter = Counter({day: agent.get_all_locked_tokens(day) for day in range(1, 61)})
MAX_ROWS = 30
period_range = list(range(1, periods + 1))
token_counter = Counter({day: agent.get_all_locked_tokens(day) for day in period_range})
width = 60 # Adjust to desired width
longest_key = max(len(str(key)) for key in token_counter)
@ -547,5 +548,22 @@ def paint_locked_tokens_status(emitter, agent) -> None:
widest = token_counter.most_common(1)[0][1]
scale = graph_width / float(widest)
for key, size in sorted(token_counter.items()):
emitter.echo(f"{key:2}: {int(size * scale) * '*'} {NU.from_nunits(size)}")
bucket_size = periods // MAX_ROWS if periods > MAX_ROWS else 1
emitter.echo(f"\n| Locked Tokens for next {periods} periods |\n")
buckets = [period_range[i:i + bucket_size] for i in range(0, len(period_range), bucket_size)]
for bucket in buckets:
bucket_start = bucket[0]
bucket_end = bucket[-1]
bucket_max = max([token_counter[period] for period in bucket])
bucket_min = min([token_counter[period] for period in bucket])
delta = bucket_max - bucket_min
bucket_range = f"{bucket_start} - {bucket_end}"
box_plot = f"{int(bucket_min * scale) * ''}{int(delta * scale) * ''}"
emitter.echo(f"{bucket_range:>9}: {box_plot:60}"
f"Min: {NU.from_nunits(bucket_min)} - Max: {NU.from_nunits(bucket_max)}")

View File

@ -34,8 +34,9 @@ from nucypher.config.characters import UrsulaConfiguration
@click.option('--sync/--no-sync', default=False)
@click.option('--geth', '-G', help="Run using the built-in geth node", is_flag=True)
@click.option('--provider', 'provider_uri', help="Blockchain provider's URI", type=click.STRING, default="auto://")
@click.option('--periods', help="Number of periods", type=click.INT, default=90)
@nucypher_click_config
def status(click_config, action, provider_uri, sync, geth, poa):
def status(click_config, action, provider_uri, sync, geth, poa, periods):
"""
Echo a snapshot of live network metadata.
"""
@ -78,7 +79,7 @@ def status(click_config, action, provider_uri, sync, geth, poa):
return # Exit
elif action == 'locked-tokens':
paint_locked_tokens_status(emitter=emitter, agent=staking_agent)
paint_locked_tokens_status(emitter=emitter, agent=staking_agent, periods=periods)
return # Exit
else: