diff --git a/ennstatus/cli/commands/stats.py b/ennstatus/cli/commands/stats.py new file mode 100644 --- /dev/null +++ b/ennstatus/cli/commands/stats.py @@ -0,0 +1,37 @@ +import json + +from collections import defaultdict + +import click + +from ennstatus import create_app +from ...status.functions import split_all_servers_to_types + + +@click.group(short_help='Get statistics') +def stats(): + pass + + +@stats.command('countries') +@click.pass_obj +def countries(obj): + app = create_app() + + with app.app_context(): + app.logger.removeHandler(app.logger.handlers[1]) + servers = split_all_servers_to_types() + + countries = defaultdict(int) + + 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' % (key, value)) + + click.echo('Most servers are in: %s' % max(countries)) + click.echo( + 'We are hosted in %s different countries' % len(countries.keys()) + )