Skip to content

Commit

Permalink
new cli command: fsck
Browse files Browse the repository at this point in the history
  • Loading branch information
jokester committed Nov 29, 2024
1 parent ce2383b commit fffceac
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
Empty file added app/scripts/__init__.py
Empty file.
68 changes: 68 additions & 0 deletions app/scripts/fsck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from dataclasses import dataclass
from typing import Literal
import io
import click
from app.models.file import File
from app.models.team import Team
from app.models.project import ProjectSet, Project
import csv
import logging

Check warning on line 9 in app/scripts/fsck.py

View check run for this annotation

Codecov / codecov/patch

app/scripts/fsck.py#L1-L9

Added lines #L1 - L9 were not covered by tests

logger = logging.getLogger(__name__)

Check warning on line 11 in app/scripts/fsck.py

View check run for this annotation

Codecov / codecov/patch

app/scripts/fsck.py#L11

Added line #L11 was not covered by tests


@dataclass(frozen=True, kw_only=True)
class SizeEntry:
team_id: str
team_name: str
project_set_id: str
project_set_name: str
project_id: str
project_name: str
num_files: int
total_size_kb: int

Check warning on line 23 in app/scripts/fsck.py

View check run for this annotation

Codecov / codecov/patch

app/scripts/fsck.py#L14-L23

Added lines #L14 - L23 were not covered by tests


@click.command("fsck")
@click.option("--export-csv", type=click.File("w"), help="export statistics to csv")
@click.option(

Check warning on line 28 in app/scripts/fsck.py

View check run for this annotation

Codecov / codecov/patch

app/scripts/fsck.py#L26-L28

Added lines #L26 - L28 were not covered by tests
"--sum-by",
type=click.Choice(["team", "project_set", "project"]),
default="team",
help="export statistics to csv",
)
def fsck(

Check warning on line 34 in app/scripts/fsck.py

View check run for this annotation

Codecov / codecov/patch

app/scripts/fsck.py#L34

Added line #L34 was not covered by tests
*, export_csv: io.FileIO | None, sum_by: Literal["team", "project_set", "project"]
):
logger.info("Running fsck")
size_entries: list[SizeEntry] = [

Check warning on line 38 in app/scripts/fsck.py

View check run for this annotation

Codecov / codecov/patch

app/scripts/fsck.py#L37-L38

Added lines #L37 - L38 were not covered by tests
_sum_by_project(t, ps, p)
for t in Team.objects()
for ps in ProjectSet.objects(team=t)
for p in Project.objects(project_set=ps)
]
if export_csv:
writer = csv.DictWriter(export_csv, fieldnames=SizeEntry.__dataclass_fields__)
writer.writeheader()
for entry in size_entries:
writer.writerow(entry.__dict__)
export_csv.close()

Check warning on line 49 in app/scripts/fsck.py

View check run for this annotation

Codecov / codecov/patch

app/scripts/fsck.py#L44-L49

Added lines #L44 - L49 were not covered by tests
else:
for entry in size_entries:
logger.info(entry)

Check warning on line 52 in app/scripts/fsck.py

View check run for this annotation

Codecov / codecov/patch

app/scripts/fsck.py#L51-L52

Added lines #L51 - L52 were not covered by tests


def _sum_by_project(t: Team, ps: ProjectSet, p: Project) -> SizeEntry:
files = File.objects(project=p)
num_files = len(files)
size_kb = sum(f.file_size for f in files)
return SizeEntry(

Check warning on line 59 in app/scripts/fsck.py

View check run for this annotation

Codecov / codecov/patch

app/scripts/fsck.py#L55-L59

Added lines #L55 - L59 were not covered by tests
team_id=t.id,
team_name=t.name,
project_set_id=ps.id,
project_set_name=ps.name,
project_id=p.id,
project_name=p.name,
total_size_kb=size_kb,
num_files=num_files,
)
3 changes: 3 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
from app import flask_app
from app.factory import init_db
from app.scripts.fsck import fsck

logging.basicConfig(
level=logging.INFO,
Expand Down Expand Up @@ -81,6 +82,8 @@ def mit_preprocess_dir(dir: str):
main.add_command(list_translations)
main.add_command(mit_preprocess_file)
main.add_command(mit_preprocess_dir)
main.add_command(fsck)


if __name__ == "__main__":
main()

0 comments on commit fffceac

Please sign in to comment.