From 0d0b9b509f4b83178a8fbaecaac0f95d192721c8 Mon Sep 17 00:00:00 2001 From: Klaas van Schelven Date: Tue, 6 Jan 2026 20:41:43 +0100 Subject: [PATCH] Add modelcounts command; useful in the context of housekeeping when servers are down --- bsmain/management/commands/modelcounts.py | 42 +++++++++++++++++++++++ bugsink/views.py | 1 + 2 files changed, 43 insertions(+) create mode 100644 bsmain/management/commands/modelcounts.py diff --git a/bsmain/management/commands/modelcounts.py b/bsmain/management/commands/modelcounts.py new file mode 100644 index 0000000..d86685d --- /dev/null +++ b/bsmain/management/commands/modelcounts.py @@ -0,0 +1,42 @@ +from django.core.management.base import BaseCommand +from django import apps + +from bugsink.transaction import durable_atomic + + +class Command(BaseCommand): + + @durable_atomic + def handle(self, *args, **options): + # Copy/paste from bugsink/views's 'def counts' with removal of the limit_runtime / CachedModelCount parts. + + interesting_apps = [ + # "admin", + # "auth", + "bsmain", + # "contenttypes", + "events", + "files", + "ingest", + "issues", + # "phonehome", + "projects", + "releases", + # "sessions", + "snappea", + "tags", + "teams", + "users", + ] + + counts = {} + + for app_label in interesting_apps: + counts[app_label] = {} + app_config = apps.apps.get_app_config(app_label) + for model in app_config.get_models(): + if model.__name__ == "CachedModelCount": + continue # skip the CachedModelCount model itself + + model_name = f"{app_label}.{model.__name__}" + print(f"{model_name:<30}{model.objects.count():>12}") diff --git a/bugsink/views.py b/bugsink/views.py index 884b05a..97a9124 100644 --- a/bugsink/views.py +++ b/bugsink/views.py @@ -173,6 +173,7 @@ def settings_view(request): @user_passes_test(lambda u: u.is_superuser) @atomic_for_request_method # get a consistent view (barring cached, which are marked as such) def counts(request): + # See also: modelcounts management command. interesting_apps = [ # "admin", # "auth",