-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21a43e4
commit 3831390
Showing
8 changed files
with
323 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import datetime, timezone | ||
|
||
import click | ||
import sqlalchemy as sa | ||
|
||
import ckan.plugins.toolkit as tk | ||
from ckan import model | ||
|
||
from ckanext.files import shared, utils | ||
|
||
|
||
def _now(): | ||
return datetime.now(timezone.utc) | ||
|
||
|
||
@click.group() | ||
def group(): | ||
"""Storage maintenance.""" | ||
|
||
|
||
storage_option = click.option( | ||
"-s", | ||
"--storage-name", | ||
help="Name of the configured storage", | ||
) | ||
|
||
|
||
@group.command() | ||
@storage_option | ||
@click.option("--remove", is_flag=True, help="Remove files") | ||
def empty_owner(storage_name: str | None, remove: bool): | ||
"""Manage files that have no owner.""" | ||
storage_name = storage_name or shared.config.default_storage() | ||
try: | ||
storage = shared.get_storage(storage_name) | ||
except shared.exc.UnknownStorageError as err: | ||
tk.error_shout(err) | ||
raise click.Abort from err | ||
|
||
if remove and not storage.supports(shared.Capability.REMOVE): | ||
tk.error_shout(f"Storage {storage_name} does not support file removal") | ||
raise click.Abort | ||
|
||
stmt = ( | ||
sa.select(shared.File) | ||
.outerjoin(shared.File.owner_info) | ||
.where(shared.File.storage == storage_name, shared.Owner.owner_id.is_(None)) | ||
) | ||
|
||
total = model.Session.scalar(sa.select(sa.func.count()).select_from(stmt)) | ||
if not total: | ||
click.echo(f"Every file in storage {storage_name} has owner reference") | ||
return | ||
click.echo("Following files do not have owner reference") | ||
|
||
for file in model.Session.scalars(stmt): | ||
size = utils.humanize_filesize(file.size) | ||
click.echo(f"\t{file.id}: {file.name} [{file.content_type}, {size}]") | ||
|
||
if remove and click.confirm("Do you want to delete these files?"): | ||
action = tk.get_action("files_file_delete") | ||
|
||
with click.progressbar(model.Session.scalars(stmt), length=total) as bar: | ||
for file in bar: | ||
action({"ignore_auth": True}, {"id": file.id}) | ||
|
||
|
||
@group.command() | ||
@storage_option | ||
@click.option("--remove", is_flag=True, help="Remove files") | ||
def invalid_owner(storage_name: str | None, remove: bool): | ||
"""Manage files that has suspicious owner reference.""" | ||
storage_name = storage_name or shared.config.default_storage() | ||
try: | ||
storage = shared.get_storage(storage_name) | ||
except shared.exc.UnknownStorageError as err: | ||
tk.error_shout(err) | ||
raise click.Abort from err | ||
|
||
if remove and not storage.supports(shared.Capability.REMOVE): | ||
tk.error_shout(f"Storage {storage_name} does not support file removal") | ||
raise click.Abort | ||
|
||
stmt = ( | ||
sa.select(shared.File) | ||
.join(shared.File.owner_info) | ||
.where(shared.File.storage == storage_name) | ||
) | ||
|
||
files = [f for f in model.Session.scalars(stmt) if f.owner is None] | ||
|
||
if not files: | ||
click.echo( | ||
f"Every owned file in storage {storage_name} has valid owner reference", | ||
) | ||
return | ||
|
||
click.echo("Following files have dangling owner reference") | ||
for file in files: | ||
size = utils.humanize_filesize(file.size) | ||
click.echo( | ||
"\t{}: {} [{}, {}]. Owner: {} {}".format( | ||
file.id, | ||
file.name, | ||
file.content_type, | ||
size, | ||
file.owner_info.owner_type, | ||
file.owner_info.owner_id, | ||
), | ||
) | ||
|
||
if remove and click.confirm("Do you want to delete these files?"): | ||
action = tk.get_action("files_file_delete") | ||
|
||
with click.progressbar(files) as bar: | ||
for file in bar: | ||
action({"ignore_auth": True}, {"id": file.id}) | ||
|
||
|
||
@group.command() | ||
@storage_option | ||
@click.option("--remove", is_flag=True, help="Remove files") | ||
def missing_files(storage_name: str | None, remove: bool): | ||
"""Manage files do not exist in storage.""" | ||
storage_name = storage_name or shared.config.default_storage() | ||
try: | ||
storage = shared.get_storage(storage_name) | ||
except shared.exc.UnknownStorageError as err: | ||
tk.error_shout(err) | ||
raise click.Abort from err | ||
|
||
if not storage.supports(shared.Capability.EXISTS): | ||
tk.error_shout( | ||
f"Storage {storage_name} does not support file availability checks", | ||
) | ||
raise click.Abort | ||
|
||
if remove and not storage.supports(shared.Capability.REMOVE): | ||
tk.error_shout(f"Storage {storage_name} does not support file removal") | ||
raise click.Abort | ||
|
||
stmt = sa.select(shared.File).where(shared.File.storage == storage_name) | ||
total = model.Session.scalar(sa.select(sa.func.count()).select_from(stmt)) | ||
missing: list[shared.File] = [] | ||
with click.progressbar(model.Session.scalars(stmt), length=total) as bar: | ||
for file in bar: | ||
data = shared.FileData.from_model(file) | ||
if not storage.exists(data): | ||
missing.append(file) | ||
|
||
if not missing: | ||
click.echo( | ||
f"No missing files located in storage {storage_name}", | ||
) | ||
return | ||
|
||
click.echo("Following files are not found in storage") | ||
for file in missing: | ||
size = utils.humanize_filesize(file.size) | ||
click.echo( | ||
f"\t{file.id}: {file.name} [{file.content_type}, {size}]", | ||
) | ||
|
||
if remove and click.confirm("Do you want to delete these files?"): | ||
action = tk.get_action("files_file_delete") | ||
|
||
with click.progressbar(missing) as bar: | ||
for file in bar: | ||
action({"ignore_auth": True}, {"id": file.id}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import datetime, timezone | ||
|
||
import click | ||
import sqlalchemy as sa | ||
from babel.dates import format_datetime, format_timedelta | ||
|
||
import ckan.plugins.toolkit as tk | ||
from ckan import model | ||
|
||
from ckanext.files import shared, utils | ||
|
||
|
||
def _now(): | ||
return datetime.now(timezone.utc) | ||
|
||
|
||
@click.group() | ||
def group(): | ||
"""Storage statistics.""" | ||
|
||
|
||
storage_option = click.option( | ||
"-s", | ||
"--storage-name", | ||
help="Name of the configured storage", | ||
) | ||
|
||
|
||
@group.command() | ||
@storage_option | ||
def overview(storage_name: str | None): | ||
"""General information about storage usage.""" | ||
|
||
storage_name = storage_name or shared.config.default_storage() | ||
stmt = sa.select( | ||
sa.func.sum(shared.File.size), | ||
sa.func.count(shared.File.id), | ||
sa.func.max(shared.File.ctime), | ||
sa.func.min(shared.File.ctime), | ||
).where(shared.File.storage == storage_name) | ||
row = model.Session.execute(stmt).fetchone() | ||
size, count, newest, oldest = row if row else (0, 0, _now(), _now()) | ||
|
||
if not count: | ||
tk.error_shout("Storage is not configured or empty") | ||
raise click.Abort | ||
|
||
click.secho(f"Number of files: {click.style(count, bold=True)}") | ||
click.secho( | ||
f"Used space: {click.style(utils.humanize_filesize(size), bold=True)}", | ||
) | ||
click.secho( | ||
"Newest file created at: {} ({})".format( | ||
click.style(format_datetime(newest), bold=True), | ||
format_timedelta(newest - _now(), add_direction=True), | ||
), | ||
) | ||
click.secho( | ||
"Oldest file created at: {} ({})".format( | ||
click.style(format_datetime(oldest), bold=True), | ||
format_timedelta(oldest - _now(), add_direction=True), | ||
), | ||
) | ||
|
||
|
||
@group.command() | ||
@storage_option | ||
def types(storage_name: str | None): | ||
"""Files distribution by MIMEtype.""" | ||
|
||
storage_name = storage_name or shared.config.default_storage() | ||
stmt = ( | ||
sa.select( | ||
shared.File.content_type, | ||
sa.func.count(shared.File.content_type).label("count"), | ||
) | ||
.where(shared.File.storage == storage_name) | ||
.group_by(shared.File.content_type) | ||
.order_by(shared.File.content_type) | ||
) | ||
|
||
total = model.Session.scalar(sa.select(sa.func.sum(stmt.c.count))) | ||
click.secho( | ||
"Storage {} contains {} files".format( | ||
click.style(storage_name, bold=True), | ||
click.style(total, bold=True), | ||
), | ||
) | ||
for content_type, count in model.Session.execute(stmt): | ||
click.secho(f"\t{content_type}: {click.style(count, bold=True)}") | ||
|
||
|
||
@group.command() | ||
@storage_option | ||
@click.option( | ||
"-v", | ||
"--verbose", | ||
is_flag=True, | ||
help="Show distribution for every owner ID", | ||
) | ||
def owner(storage_name: str | None, verbose: bool): | ||
"""Files distribution by owner.""" | ||
|
||
storage_name = storage_name or shared.config.default_storage() | ||
owner_col = ( | ||
sa.func.concat(shared.Owner.owner_type, " ", shared.Owner.owner_id) | ||
if verbose | ||
else sa.func.concat(shared.Owner.owner_type, "") | ||
) | ||
|
||
stmt = ( | ||
sa.select( | ||
owner_col.label("owner"), | ||
sa.func.count(shared.File.id), | ||
) | ||
.where(shared.File.storage == storage_name) | ||
.outerjoin( | ||
shared.Owner, | ||
sa.and_( | ||
shared.Owner.item_id == shared.File.id, | ||
shared.Owner.item_type == "file", | ||
), | ||
) | ||
.group_by(owner_col) | ||
).order_by(owner_col) | ||
|
||
total = model.Session.scalar(sa.select(sa.func.sum(stmt.c.count))) | ||
click.secho( | ||
"Storage {} contains {} files".format( | ||
click.style(storage_name, bold=True), | ||
click.style(total, bold=True), | ||
), | ||
) | ||
for owner, count in model.Session.execute(stmt): | ||
click.secho( | ||
"\t{}: {}".format( | ||
owner.strip() or click.style("has no owner", underline=True, bold=True), | ||
click.style(count, bold=True), | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters