Skip to content

Commit

Permalink
feat(rnu-package): add celery tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisig committed Jul 15, 2024
1 parent 36811ad commit 4314d44
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 24 deletions.
22 changes: 22 additions & 0 deletions project/migrations/0084_rnupackage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.13 on 2024-07-14 15:54

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("project", "0083_alter_historicalrequest_organism_and_more"),
]

operations = [
migrations.CreateModel(
name="RNUPackage",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("file", models.FileField(upload_to="rnu_packages/")),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("departement_official_id", models.CharField(max_length=10)),
],
),
]
15 changes: 9 additions & 6 deletions project/tasks/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,21 +894,24 @@ def alert_on_blocked_diagnostic(self) -> None:
@shared_task(max_retries=5)
def create_zip_departement_rnu_package_one_off(departement_id: str) -> None:
departement = Departement.objects.get(source_id=departement_id)
commune_in_departement_ids_as_string = [
str(commune_id) for commune_id in departement.commune_set.values_list("id", flat=True)
]
requests_created_by_the_rnu_package_service_account = Request.objects.filter(
email="[email protected]",
project__land_id=departement.pk,
project__land_id__in=commune_in_departement_ids_as_string,
)

file_name = f"rnu_package_departement_{departement_id}.zip"

with zipfile.ZipFile(file_name, "a", compression=zipfile.ZIP_DEFLATED) as zipf:
for request in requests_created_by_the_rnu_package_service_account:
if not request.sent_file:
raise ValueError(f"Request {request.id} has no sent file")

file_name_in_zip = f"{departement_id}_COMM_{request.project.land.official_id}.docx"
zipf.write(
filename=request.sent_file,
arcname=file_name_in_zip,
)
zipf.writestr(file_name_in_zip, request.sent_file.read())
RNUPackage.objects.create(
departement_official_id=departement.source_id,
file_name=file_name,
file=file_name,
)
39 changes: 26 additions & 13 deletions project/views/RNUPackagesProgressView.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
from rest_framework.views import APIView

from project.models import Project
from public_data.models import Commune
from public_data.models import Commune, Departement
from public_data.models.sudocuh import DocumentUrbanismeChoices, Sudocuh
from users.models import User


class RNUPackagesProgressView(APIView):
def get(self, request):
diagnostic_to_create = Sudocuh.objects.filter(du_opposable=DocumentUrbanismeChoices.RNU)
of_those_with_ocsge = Commune.objects.filter(
from_commune_table = Commune.objects.filter(
insee__in=diagnostic_to_create.values("code_insee"),
)
of_those_with_ocsge = from_commune_table.filter(
ocsge_available=True,
)
of_those_with_ocsge_count = of_those_with_ocsge.count()
Expand All @@ -37,8 +39,6 @@ def get(self, request):

async_fields_with_ocsge = [
"async_theme_map_understand_artif_done",
"async_theme_map_gpu_done",
"async_theme_map_fill_gpu_done",
]

aggregate_results = []
Expand Down Expand Up @@ -67,13 +67,26 @@ def get(self, request):
minutes = (time_diff.seconds % 3600) // 60
seconds = time_diff.seconds % 60

return Response(
{
"elapsed_time": f"{hours}h {minutes}m {seconds}s",
"diagnostic_to_create_count": diagnostic_to_create_count,
"of_those_with_ocsge_count": of_those_with_ocsge_count,
"diagnostic_created_count": diagnostic_created_count,
"diangostic_created_percentage": f"{diagnostic_created_count / diagnostic_to_create_count * 100}%",
"async_operations_progress": aggregate_results,
response_data = {
"elapsed_time": f"{hours}h {minutes}m {seconds}s",
"diagnostic_to_create_count": diagnostic_to_create_count,
"of_those_with_ocsge_count": of_those_with_ocsge_count,
"diagnostic_created_count": diagnostic_created_count,
"diangostic_created_percentage": f"{diagnostic_created_count / diagnostic_to_create_count * 100}%",
"async_operations_progress": aggregate_results,
}

for departement in Departement.objects.all():
response_data[f"department_{departement.source_id}"] = {
"diagnostic_to_create_count": diagnostic_to_create.filter(
code_insee__startswith=departement.source_id
).count(),
"of_those_with_ocsge_count": of_those_with_ocsge.filter(departement=departement).count(),
"diagnostic_created_count": diagnostic_created.annotate(
land_id_as_int=Cast("land_id", output_field=IntegerField())
)
.filter(land_id_as_int__in=from_commune_table.filter(departement=departement).values("id"))
.count(),
}
)

return Response(response_data)
16 changes: 13 additions & 3 deletions public_data/management/commands/create_rnu_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
class Command(BaseCommand):
help = "create_rnu_diagnostics"

def add_arguments(self, parser):
parser.add_argument("--departement", type=str, required=True)

def handle(self, *args, **options):
mondiagartif_user, _ = User.objects.get_or_create(
email="[email protected]",
Expand All @@ -26,8 +29,11 @@ def handle(self, *args, **options):
defaults={"email_checked": timezone.now()},
)

projects = []

for commune in Commune.objects.filter(
insee__in=[Sudocuh.objects.filter(du_opposable=DocumentUrbanismeChoices.RNU).values("code_insee")]
departement__source_id=options["departement"],
insee__in=[Sudocuh.objects.filter(du_opposable=DocumentUrbanismeChoices.RNU).values("code_insee")],
):
land = Land(public_key=f"COMM_{commune.pk}")
project = Project.objects.create(
Expand Down Expand Up @@ -58,6 +64,8 @@ def handle(self, *args, **options):
async_add_comparison_lands_done=True,
)

project.cities.add(commune)

Emprise.objects.create(
mpoly=fix_poly(commune.mpoly),
srid_source=commune.srid_source,
Expand All @@ -70,7 +78,9 @@ def handle(self, *args, **options):

project.refresh_from_db()

project = project.add_look_a_like(public_key=similar_lands_public_keys, many=True)
project.add_look_a_like(public_key=similar_lands_public_keys, many=True)

projects.append(project)

for project in Project.objects.filter(user=mondiagartif_user):
for project in projects:
trigger_async_tasks_rnu_pakage_one_off(project)
12 changes: 10 additions & 2 deletions public_data/management/commands/create_rnu_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@


class Command(BaseCommand):
help = "create_rnu_diagnostics"
help = "create_rnu_packages"

def add_arguments(self, parser):
parser.add_argument("--departement", type=str, required=False)

def handle(self, *args, **options):
tasks = []

for departement in Departement.objects.all():
departements = Departement.objects.all()

if options["departement"]:
departements = Departement.objects.filter(source_id=options["departement"])

for departement in departements:
tasks.append(create_zip_departement_rnu_package_one_off.si(departement.source_id))

celery.group(*tasks).apply_async(queue="long")

0 comments on commit 4314d44

Please sign in to comment.