Files
@ 3d53c627707a
Branch filter:
Location: FVDE/ennstatus/ennstatus/cli/commands/stats.py
3d53c627707a
3.4 KiB
text/x-python
Disable logging when using the cli tool
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | 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('count')
@click.option('--by-type', 'by_type', is_flag=True, default=False)
@click.pass_obj
def count(obj, by_type):
def calculate_host_number(config, type='all'):
if type == 'all':
hosts = set()
for values in config['ENNSTATUS_SERVERS'].values():
ips = frozenset(values['IPS'])
hosts.add(ips)
return len(hosts)
app = create_app()
with app.app_context():
app.logger.disabled = True
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'
)
)
)
click.echo(
'We have %s different hosts!' % (
click.style(
str(calculate_host_number(app.config)),
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.disabled = True
servers = split_all_servers_to_types()
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 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)
}
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
)
)
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'
)
)
)
|