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

dds stats #624

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions SPRINTLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,7 @@ _Nothing merged in CLI during this sprint_
- Bug: Catch UnicodeEncodeError during API request to avoid unclear error message upon usage of non-latin1 characters in username and password ([#617](https://github.com/ScilifelabDataCentre/dds_cli/pull/617))
- Workflow: Restructure and clarify PR template ([#618](https://github.com/ScilifelabDataCentre/dds_cli/pull/618))
- Workflow: Changelog changed to Sprintlog and CHANGELOG.rst created for version changes ([#621](https://github.com/ScilifelabDataCentre/dds_cli/pull/620))

# 2023-03-17 - 2023-03-31

- New command: `dds stats` to get project and data statistics ([#624](https://github.com/ScilifelabDataCentre/dds_cli/pull/624))
44 changes: 44 additions & 0 deletions dds_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging
import os
import sys
import typing

# Installed
import pathlib
Expand Down Expand Up @@ -2081,3 +2082,46 @@ def set_maintenance_mode(click_ctx, setting):
) as err:
LOG.error(err)
sys.exit(1)


# Stats


@dds_main.command(name="stats", no_args_is_help=False)
@click.argument(
"stat_type", nargs=1, type=click.Choice(["active", "all", "size"], case_sensitive=True)
)
@click.pass_obj
def get_stats(click_ctx, stat_type):
"""Get statistics in the DDS."""
try:
# Num projects
with dds_cli.data_lister.DataLister(
show_usage=True,
no_prompt=click_ctx.get("NO_PROMPT", False),
json=True,
token_path=click_ctx.get("TOKEN_PATH"),
) as lister:
# Get projects, only active by default
projects: typing.List = lister.list_projects(show_all=(stat_type == "all"))

if stat_type == "size":
# Calculate total amount of saved data in active projects
title_bold_part: str = "Bytes"
title_rest: str = "currently stored in DDS"
value: int = sum([x["Size"] for x in projects])
else:
# Get number of projects
title_bold_part: str = "Active" if stat_type == "active" else "Total"
title_rest: str = "projects"
value: int = len(projects)

LOG.info(f"[bold]{title_bold_part}[/bold] {title_rest}: {value}")
except (
dds_cli.exceptions.APIError,
dds_cli.exceptions.AuthenticationError,
dds_cli.exceptions.ApiResponseError,
dds_cli.exceptions.ApiRequestError,
) as err:
LOG.error(err)
sys.exit(1)