diff --git a/ennstatus/cli/commands/stats.py b/ennstatus/cli/commands/stats.py --- a/ennstatus/cli/commands/stats.py +++ b/ennstatus/cli/commands/stats.py @@ -13,39 +13,97 @@ def stats(): pass -@stats.command('countries') +@stats.command('count') +@click.option('--by-type', 'by_type', is_flag=True, default=False) @click.pass_obj -def countries(obj): +def count(obj, by_type): + + app = create_app() + + with app.app_context(): + app.logger.removeHandler(app.logger.handlers[1]) + servers = split_all_servers_to_types() + + if not by_type: + + click.echo( + 'We have %s servers in total!' % ( + click.style( + str(sum(len(x) for x in servers.values())), + fg='blue' + ) + ) + ) + + +@stats.command('countries') +@click.option('--by-type', 'by_type', is_flag=True, default=False) +@click.pass_obj +def countries(obj, by_type): app = create_app() with app.app_context(): app.logger.removeHandler(app.logger.handlers[1]) servers = split_all_servers_to_types() - countries = defaultdict(int) + if not by_type: + countries = defaultdict(int) - for key, value in servers.items(): - for server in value: - countries[server.country] += 1 + for key, value in servers.items(): + for server in value: + countries[server.country] += 1 - for key, value in sorted(countries.items(), key=lambda x: x[1]): - click.echo( - '%s: %s' % ( - click.style(key, fg='green'), - click.style(str(value), fg='blue') + for key, value in sorted(countries.items(), key=lambda x: x[1]): + click.echo( + '%s: %s' % ( + click.style(key, fg='green'), + click.style(str(value), fg='blue') + ) ) - ) + + click.echo( + 'We are hosted in %s different countries' % click.style( + str(len(countries.keys())), + fg='blue' + ) + ) + else: + type_countries = { + 'exit': defaultdict(int), + 'relay': defaultdict(int), + 'bridge': defaultdict(int) + } - click.echo( - 'Most servers are in: %s' % click.style( - str(max(countries)), - fg='green' - ) - ) + for key, value in servers.items(): + for server in value: + type_countries[key][server.country] += 1 + + type_countries = dict((k, v) for k, v in type_countries.items() if v) + + for key, value in sorted(type_countries.items(), key=lambda x: x[0]): + click.echo( + click.style( + key.capitalize(), + fg='red', + bold=True, + underline=True + ) + ) - click.echo( - 'We are hosted in %s different countries' % click.style( - str(len(countries.keys())), - fg='blue' - ) - ) + for country, count in sorted(type_countries[key].items(), key=lambda x: x[1]): + click.echo( + '%s: %s' % ( + click.style(country, fg='green'), + click.style(str(count), fg='blue') + ) + ) + + click.echo( + '%s are hosted in %s different countries' % ( + key, + click.style( + str(len(type_countries[key].keys())), + fg='blue' + ) + ) + )