Changeset - 8c192f60f57f
[Not reviewed]
feature-cli
0 1 0
Dennis Fink - 9 years ago 2015-10-26 00:04:25
dennis.fink@c3l.lu
Implemented --by-type in count stats
1 file changed with 41 insertions and 5 deletions:
0 comments (0 inline, 0 general)
ennstatus/cli/commands/stats.py
Show inline comments
 
@@ -15,25 +15,29 @@ def stats():
 

	
 
@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':
 
    def calculate_host_number(config, type='all', servers=None):
 

	
 
            hosts = set()
 
        if type == 'all':
 

	
 
            for values in config['ENNSTATUS_SERVERS'].values():
 
                ips = frozenset(values['IPS'])
 
                hosts.add(ips)
 

	
 
            return len(hosts)
 

	
 
        else:
 
            for server in servers[servertype]:
 
                ips = frozenset(
 
                    config['ENNSTATUS_SERVERS'][server.name.lower()]['IPS']
 
                )
 
                hosts.add(ips)
 
        return len(hosts)
 

	
 
    app = create_app()
 

	
 
    with app.app_context():
 
        app.logger.disabled = True
 
        servers = split_all_servers_to_types()
 
@@ -54,12 +58,44 @@ def count(obj, by_type):
 
                click.style(
 
                    str(calculate_host_number(app.config)),
 
                    fg='blue'
 
                )
 
            )
 
        )
 
    else:
 
        for servertype, server in servers.items():
 
            click.echo(
 
                'We have %s %s servers!' % (
 
                    click.style(
 
                        str(len(server)),
 
                        fg='blue'
 
                    ),
 
                    click.style(
 
                        servertype,
 
                        fg='red'
 
                    )
 
                )
 
            )
 
            click.echo(
 
                'We have %s different %s hosts!' % (
 
                    click.style(
 
                        str(
 
                            calculate_host_number(
 
                                app.config,
 
                                type=servertype,
 
                                servers=servers
 
                            )
 
                        ),
 
                        fg='blue'
 
                    ),
 
                    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):
0 comments (0 inline, 0 general)