Changeset - 9d3ee5959bc9
[Not reviewed]
Merge dev
0 1 0
Dennis Fink - 9 years ago 2016-05-16 17:32:56
dennis.fink@c3l.lu
Merge hotfix-ennstatus-cli-key-error
1 file changed with 32 insertions and 24 deletions:
0 comments (0 inline, 0 general)
ennstatus/cli/commands/stats.py
Show inline comments
 
@@ -89,136 +89,144 @@ def count(obj, by_type):
 
                    ),
 
                    click.style(
 
                        servertype,
 
                        fg='red'
 
                    )
 
                )
 
            )
 

	
 

	
 
@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'
 
                    )
 
                )
 
            )
 

	
 

	
 
@stats.command('exit_probability')
 
@click.option('--by-server', 'by_server', is_flag=True, default=False)
 
@click.pass_obj
 
def exit_probability(obj, by_server):
 

	
 
    app = create_app()
 
    with app.app_context():
 
        app.logger.disabled = True
 
        servers = split_all_servers_to_types()
 

	
 
    if not by_server:
 
        exit_probability = defaultdict(int)
 
        for server in servers['exit']:
 
            for subkey in ('1_week', '1_month', '3_months', '1_year', '5_years'):
 
                if server.mean_exit_probability[subkey] is not None:
 
                    exit_probability[subkey] += server.mean_exit_probability[subkey]
 

	
 
                try:
 
                    if server.mean_exit_probability[subkey] is not None:
 
                        exit_probability[subkey] += server.mean_exit_probability[subkey]
 
                except KeyError:
 
                    continue
 
        for subkey in ('1_week', '1_month', '3_months', '1_year', '5_years'):
 
            click.echo(
 
                'Mean exit probability over %s: %s' % (
 
                    click.style(
 
                        subkey,
 
                        fg='blue'
 
                    ),
 
                    click.style(
 
                        str(round(exit_probability[subkey], 2)) + '%',
 
                        fg='red'
 
            try:
 
                click.echo(
 
                    'Mean exit probability over %s: %s' % (
 
                        click.style(
 
                            subkey,
 
                            fg='blue'
 
                        ),
 
                        click.style(
 
                            str(round(exit_probability[subkey], 2)) + '%',
 
                            fg='red'
 
                        )
 
                    )
 
                )
 
            )
 
            except KeyError:
 
                continue
 
    else:
 
        for server in servers['exit']:
 
            click.echo(
 
                click.style(
 
                    server.name.capitalize(),
 
                    fg='red',
 
                    bold=True,
 
                    underline=True
 
                )
 
            )
 
            for subkey in ('1_week', '1_month', '3_months', '1_year', '5_years'):
 
                if server.mean_exit_probability[subkey] is not None:
 
                    click.echo(
 
                        'Mean exit probabilty over %s: %s' % (
 
                            click.style(
 
                                subkey,
 
                                fg='blue'
 
                            ),
 
                            click.style(
 
                                str(round(server.mean_exit_probability[subkey], 2)) + "%",
 
                                fg='red'
 
                try:
 
                    if server.mean_exit_probability[subkey] is not None:
 
                        click.echo(
 
                            'Mean exit probabilty over %s: %s' % (
 
                                click.style(
 
                                    subkey,
 
                                    fg='blue'
 
                                ),
 
                                click.style(
 
                                    str(round(server.mean_exit_probability[subkey], 2)) + "%",
 
                                    fg='red'
 
                                )
 
                            )
 
                        )
 
                    )
 
                except KeyError:
 
                    continue
0 comments (0 inline, 0 general)