-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt existing update_counts command. Add cron
- Loading branch information
Showing
7 changed files
with
129 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash -l | ||
|
||
# Update API ZRR fields for new siaes | ||
|
||
# Do not run if this env var is not set: | ||
if [[ -z "$CRON_UPDATE_COUNT_FIELDS_ENABLED" ]]; then | ||
echo "CRON_UPDATE_COUNT_FIELDS_ENABLED not set. Exiting..." | ||
exit 0 | ||
fi | ||
|
||
# About clever cloud cronjobs: | ||
# https://www.clever-cloud.com/doc/tools/crons/ | ||
|
||
if [[ "$INSTANCE_NUMBER" != "0" ]]; then | ||
echo "Instance number is ${INSTANCE_NUMBER}. Stop here." | ||
exit 0 | ||
fi | ||
|
||
# $APP_HOME is set by default by clever cloud. | ||
cd $APP_HOME | ||
|
||
# django-admin update_count_fields | ||
django-admin update_count_fields --fields etablissement_count |
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,84 @@ | ||
from lemarche.siaes import utils as siae_utils | ||
from lemarche.siaes.models import Siae | ||
from lemarche.utils.apis import api_slack | ||
from lemarche.utils.commands import BaseCommand | ||
|
||
|
||
SIAE_COUNT_FIELDS = [ | ||
"user_count", | ||
"sector_count", | ||
"network_count", | ||
"group_count", | ||
"offer_count", | ||
"client_reference_count", | ||
"label_count", | ||
"image_count", | ||
"etablissement_count", | ||
] | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Goal: update the '_count' fields of each Siae | ||
Note: these fields should be updated automatically on each Siae save() | ||
Usage: | ||
python manage.py update_count_fields | ||
python manage.py update_count_fields --id 1 | ||
python manage.py update_count_fields --id 1 --fields user_count | ||
python manage.py update_count_fields --id 1 --fields user_count --fields etablissement_count | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("--id", type=int, default=None, help="Indiquer l'ID d'une structure") | ||
parser.add_argument( | ||
"--fields", action="append", default=[], help="Filtrer sur les champs count à mettre à jour" | ||
) | ||
|
||
def handle(self, *args, **options): | ||
self.stdout_messages_info("Updating Siae count fields...") | ||
|
||
# Step 1a: build Siae queryset | ||
siae_queryset = Siae.objects.prefetch_related( | ||
"users", "sectors", "networks", "groups", "offers", "client_references", "labels", "images" | ||
).all() | ||
if options["id"]: | ||
siae_queryset = siae_queryset.filter(id=options["id"]) | ||
self.stdout_messages_info(f"Found {siae_queryset.count()} Siae") | ||
|
||
# Step 1b: init fields to update | ||
update_fields = options["fields"] if options["fields"] else SIAE_COUNT_FIELDS | ||
self.stdout_messages_info(f"Fields to update: {update_fields}") | ||
|
||
# Step 2: loop on each Siae | ||
progress = 0 | ||
for index, siae in enumerate(siae_queryset): | ||
# M2M | ||
siae.user_count = siae.users.count() | ||
siae.sector_count = siae.sectors.count() | ||
siae.network_count = siae.networks.count() | ||
siae.group_count = siae.groups.count() | ||
# FK | ||
siae.offer_count = siae.offers.count() | ||
siae.client_reference_count = siae.client_references.count() | ||
siae.label_count = siae.labels_old.count() | ||
siae.image_count = siae.images.count() | ||
# etablissement_count | ||
if siae.is_active and siae.siren: | ||
siae.etablissement_count = siae_utils.calculate_etablissement_count(siae) | ||
|
||
# Step 3: update count fields | ||
siae.save(update_fields=update_fields) | ||
|
||
progress += 1 | ||
if (progress % 500) == 0: | ||
self.stdout_messages_info(f"{progress}...") | ||
|
||
msg_success = [ | ||
"----- Siae count fields -----", | ||
f"Done! Processed {siae_queryset.count()} siaes", | ||
f"Fields updated: {update_fields}", | ||
] | ||
self.stdout_messages_success(msg_success) | ||
api_slack.send_message_to_channel("\n".join(msg_success)) |
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
from lemarche.siaes.models import Siae | ||
|
||
|
||
def calculate_etablissement_count(siae: Siae): | ||
if siae.siren: | ||
return Siae.objects.filter(is_active=True, siret__startswith=siae.siren).count() | ||
return 0 |
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