Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ NEW: Add verdi database summary #4737

Merged
merged 10 commits into from
Feb 10, 2021
45 changes: 45 additions & 0 deletions aiida/cmdline/commands/cmd_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,48 @@ def detect_invalid_nodes():
echo.echo_success('no integrity violations detected')
else:
echo.echo_critical('one or more integrity violations detected')


@verdi_database.command('summary')
@options.VERBOSE()
def database_summary(verbose):
"""Summarise the entities in the database."""
from aiida.orm import QueryBuilder, Node, Group, Computer, Comment, Log, User
data = {}

# User
query_user = QueryBuilder().append(User, project=['email'])
data['Users'] = {'count': query_user.count()}
if verbose:
data['Users']['emails'] = query_user.distinct().all(flat=True)

# Computer
query_comp = QueryBuilder().append(Computer, project=['name'])
data['Computers'] = {'count': query_comp.count()}
if verbose:
data['Computers']['names'] = query_comp.distinct().all(flat=True)

# Node
count = QueryBuilder().append(Node).count()
data['Nodes'] = {'count': count}
if verbose:
node_types = QueryBuilder().append(Node, project=['node_type']).distinct().all(flat=True)
data['Nodes']['node_types'] = node_types
process_types = QueryBuilder().append(Node, project=['process_type']).distinct().all(flat=True)
data['Nodes']['process_types'] = [p for p in process_types if p]

# Group
query_group = QueryBuilder().append(Group, project=['type_string'])
data['Groups'] = {'count': query_group.count()}
if verbose:
data['Groups']['type_strings'] = query_group.distinct().all(flat=True)
chrisjsewell marked this conversation as resolved.
Show resolved Hide resolved

# Comment
count = QueryBuilder().append(Comment).count()
data['Comments'] = {'count': count}

# Log
count = QueryBuilder().append(Log).count()
data['Logs'] = {'count': count}

echo.echo_dictionary(data, sort_keys=False, fmt='yaml')
24 changes: 18 additions & 6 deletions aiida/cmdline/utils/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def echo_formatted_list(collection, attributes, sort=None, highlight=None, hide=
click.secho(template.format(symbol=' ', *values))


def _format_dictionary_json_date(dictionary):
def _format_dictionary_json_date(dictionary, sort_keys=True):
"""Return a dictionary formatted as a string using the json format and converting dates to strings."""
from aiida.common import json

Expand All @@ -201,27 +201,39 @@ def default_jsondump(data):

raise TypeError(f'{repr(data)} is not JSON serializable')

return json.dumps(dictionary, indent=4, sort_keys=True, default=default_jsondump)
return json.dumps(dictionary, indent=4, sort_keys=sort_keys, default=default_jsondump)


VALID_DICT_FORMATS_MAPPING = OrderedDict((('json+date', _format_dictionary_json_date), ('yaml', yaml.dump),
('yaml_expanded', lambda d: yaml.dump(d, default_flow_style=False))))
def _format_yaml(dictionary, sort_keys=True):
"""Return a dictionary formatted as a string using the YAML format."""
return yaml.dump(dictionary, sort_keys=sort_keys)


def echo_dictionary(dictionary, fmt='json+date'):
def _format_yaml_expanded(dictionary, sort_keys=True):
"""Return a dictionary formatted as a string using the expanded YAML format."""
return yaml.dump(dictionary, sort_keys=sort_keys, default_flow_style=False)


VALID_DICT_FORMATS_MAPPING = OrderedDict(
(('json+date', _format_dictionary_json_date), ('yaml', _format_yaml), ('yaml_expanded', _format_yaml_expanded))
)


def echo_dictionary(dictionary, fmt='json+date', sort_keys=True):
"""
Print the given dictionary to stdout in the given format

:param dictionary: the dictionary
:param fmt: the format to use for printing
:param sort_keys: Whether to automatically sort keys
"""
try:
format_function = VALID_DICT_FORMATS_MAPPING[fmt]
except KeyError:
formats = ', '.join(VALID_DICT_FORMATS_MAPPING.keys())
raise ValueError(f'Unrecognised printing format. Valid formats are: {formats}')

echo(format_function(dictionary))
echo(format_function(dictionary, sort_keys=sort_keys))


def is_stdout_redirected():
Expand Down
1 change: 1 addition & 0 deletions docs/source/reference/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ Below is a list with all available subcommands.
Commands:
integrity Check the integrity of the database and fix potential issues.
migrate Migrate the database to the latest schema version.
summary Summarise the entities in the database.
version Show the version of the database.


Expand Down
10 changes: 10 additions & 0 deletions tests/cmdline/commands/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,13 @@ def tests_database_version(run_cli_command, manager):
result = run_cli_command(cmd_database.database_version)
assert result.output_lines[0].endswith(backend_manager.get_schema_generation_database())
assert result.output_lines[1].endswith(backend_manager.get_schema_version_database())


@pytest.mark.usefixtures('clear_database_before_test')
def tests_database_summary(aiida_localhost, run_cli_command):
"""Test the ``verdi database summary -v`` command."""
from aiida import orm
node = orm.Dict().store()
result = run_cli_command(cmd_database.database_summary, ['--verbose'])
assert aiida_localhost.label in result.output
assert node.node_type in result.output