Skip to content

Commit

Permalink
Merge pull request #2297 from GSA-TTS/main
Browse files Browse the repository at this point in the history
  • Loading branch information
jadudm authored Sep 29, 2023
2 parents af72f4a + 7c79e72 commit 6bf96a7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
5 changes: 5 additions & 0 deletions backend/Apple_M1_Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ ENV PYTHONUNBUFFERED 1
RUN apt-get -y update
RUN apt-get -y install git

RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install --no-install-recommends --assume-yes \
postgresql-client

# RUN --mount=type=cache,target=/root/.cache \

WORKDIR ..
Expand Down
5 changes: 2 additions & 3 deletions backend/report_submission/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def get(self, request, *args, **kwargs):
+ "federal-awards-audit-findings/",
"no_findings_disclaimer": True,
"workbook_url": workbook_base_url
+ "federal-awards-audit-findings.xlsx",
+ "federal-awards-audit-findings-workbook.xlsx",
},
"audit-findings-text": {
"view_id": "audit-findings-text",
Expand All @@ -328,8 +328,7 @@ def get(self, request, *args, **kwargs):
"instructions_url": instructions_base_url
+ "federal-awards-audit-findings-text/",
"no_findings_disclaimer": True,
"workbook_url": workbook_base_url
+ "federal-awards-audit-findings-text-workbook.xlsx",
"workbook_url": workbook_base_url + "audit-findings-text-workbook.xlsx",
},
"cap": {
"view_id": "cap",
Expand Down
55 changes: 55 additions & 0 deletions backend/support/management/commands/collect_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from datetime import datetime
from django.core.management.base import BaseCommand
from config import settings
from django.db import connection, models
from django.apps import apps


class Command(BaseCommand):
help = """
Collect metrics about the database and media.
For the database, list table, row count and size
For media files, list file types, create date and size
"""

def handle(self, *args, **kwargs):
print(f"Metrics for {settings.ENVIRONMENT} collected at {datetime.now()}")
self.dump_db_metrics()
self.dump_media_metrics()

def dump_db_metrics(self):
def get_table_size(model):
cursor = connection.cursor()
cursor.execute(f"SELECT pg_total_relation_size('{model._meta.db_table}');")
table_size = cursor.fetchone()[0]
cursor.close()
return table_size

total_size = 0
for app in apps.get_app_configs():
print(f"App: {app.name}:")
for model in app.get_models():
row_count = model.objects.count()
table_size = get_table_size(model)
total_size += table_size
print(
f" * {model._meta.verbose_name}: {row_count} rows, {table_size} bytes"
)
print(f" ** Database size: {total_size} bytes **")

def dump_media_metrics(self):
total_size = 0
for app in apps.get_app_configs():
for model in app.get_models():
for field in model._meta.get_fields():
if isinstance(field, models.FileField):
print(
f"{app.name} : {model._meta.verbose_name} : {field.name}:"
)
count = size = 0
for row in model.objects.all():
count += 1
size += getattr(row, field.name).file.size
print(f"{count} instances, {size} bytes")
total_size += size
print(f" ** Media size: {total_size} bytes **")
29 changes: 29 additions & 0 deletions backend/support/management/commands/purge_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.core.management.base import BaseCommand
from django.apps import apps
from audit.models import SingleAuditChecklist
from config import settings


class Command(BaseCommand):
help = """
This is a testing utility that should only be used locally and in dev.
It deletes all data in the postgres database.
It was build to test backup and restore,
"""

def handle(self, *args, **kwargs):
if settings.ENVIRONMENT not in [
"DEVELOPMENT",
"LOCAL",
]:
print("This command works only in LOCAL or DEVELOPMENT environments")
else:
self.purge_tables()

def purge_tables(self):
# Delete SAC first to aboid FK protection issues
SingleAuditChecklist.objects.all().delete()

for app in apps.get_app_configs():
for model in app.get_models():
model.objects.all().delete()

0 comments on commit 6bf96a7

Please sign in to comment.