-
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.
[Dépôt de besoin] Stats supplémentaires pour Metabase (#939)
* Tender: new stat field siae_count * Tender update_tender_count_fields command * Add other count fields * Add command to cron (daily)
- Loading branch information
Showing
7 changed files
with
189 additions
and
11 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
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 tender count fields | ||
|
||
# Do not run if this env var is not set: | ||
if [[ -z "$CRON_UPDATE_TENDER_COUNT_FIELDS_ENABLED" ]]; then | ||
echo "CRON_UPDATE_TENDER_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_tender_count_fields | ||
django-admin update_tender_count_fields |
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
70 changes: 70 additions & 0 deletions
70
lemarche/tenders/management/commands/update_tender_count_fields.py
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,70 @@ | ||
from lemarche.tenders.models import Tender | ||
from lemarche.utils.commands import BaseCommand | ||
|
||
|
||
TENDER_COUNT_FIELDS = [ | ||
"siae_count", | ||
"siae_email_send_count", | ||
"siae_email_link_click_count", | ||
"siae_detail_display_count", | ||
"siae_email_link_click_or_detail_display_count", | ||
"siae_detail_contact_click_count", | ||
] | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Goal: update the '_count' fields of each Tender | ||
Usage: | ||
python manage.py update_tender_count_fields | ||
python manage.py update_tender_count_fields --id 1 | ||
python manage.py update_tender_count_fields --id 1 --fields siae_count | ||
python manage.py update_tender_count_fields --id 1 --fields siae_count --fields siae_detail_contact_click_count | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("--id", type=int, default=None, help="Indiquer l'ID d'un besoin") | ||
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 Tender count fields...") | ||
|
||
# Step 1a: build the queryset | ||
tender_queryset = Tender.objects.with_siae_stats().all() | ||
if options["id"]: | ||
tender_queryset = tender_queryset.filter(id=options["id"]) | ||
self.stdout_messages_info(f"Found {tender_queryset.count()} tenders") | ||
|
||
# Step 1b: init fields to update | ||
update_fields = options["fields"] if options["fields"] else TENDER_COUNT_FIELDS | ||
self.stdout_messages_info(f"Fields to update: {update_fields}") | ||
|
||
# Step 2: loop on each Tender | ||
progress = 0 | ||
for index, tender in enumerate(tender_queryset): | ||
# M2M | ||
tender.siae_count = tender.siae_count_annotated | ||
tender.siae_email_send_count = tender.siae_email_send_count_annotated | ||
tender.siae_email_link_click_count = tender.siae_email_link_click_count_annotated | ||
tender.siae_detail_display_count = tender.siae_detail_display_count_annotated | ||
tender.siae_email_link_click_or_detail_display_count = ( | ||
tender.siae_email_link_click_or_detail_display_count_annotated | ||
) | ||
tender.siae_detail_contact_click_count = tender.siae_detail_contact_click_count_annotated | ||
|
||
# Step 3: update count fields | ||
tender.save(update_fields=update_fields) | ||
|
||
progress += 1 | ||
if (progress % 500) == 0: | ||
self.stdout_info(f"{progress}...") | ||
|
||
msg_success = [ | ||
"----- Tender count fields -----", | ||
f"Done! Processed {tender_queryset.count()} tenders", | ||
f"Fields updated: {update_fields}", | ||
] | ||
self.stdout_messages_success(msg_success) |
66 changes: 66 additions & 0 deletions
66
lemarche/tenders/migrations/0059_tender_siae_count_updated.py
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,66 @@ | ||
# Generated by Django 4.2.2 on 2023-10-25 11:48 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("tenders", "0058_tenderstepsdata"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="tender", | ||
name="siae_count", | ||
field=models.IntegerField( | ||
default=0, | ||
help_text="Champ recalculé à intervalles réguliers", | ||
verbose_name="Nombre de structures concernées", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="tender", | ||
name="siae_detail_contact_click_count", | ||
field=models.IntegerField( | ||
default=0, | ||
help_text="Champ recalculé à intervalles réguliers", | ||
verbose_name="Nombre de structures intéressées", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="tender", | ||
name="siae_detail_display_count", | ||
field=models.IntegerField( | ||
default=0, | ||
help_text="Champ recalculé à intervalles réguliers", | ||
verbose_name="Nombre de structures vues", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="tender", | ||
name="siae_email_link_click_count", | ||
field=models.IntegerField( | ||
default=0, | ||
help_text="Champ recalculé à intervalles réguliers", | ||
verbose_name="Nombre de structures cliquées", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="tender", | ||
name="siae_email_link_click_or_detail_display_count", | ||
field=models.IntegerField( | ||
default=0, | ||
help_text="Champ recalculé à intervalles réguliers", | ||
verbose_name="Nombre de structures cliquées ou vues", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="tender", | ||
name="siae_email_send_count", | ||
field=models.IntegerField( | ||
default=0, | ||
help_text="Champ recalculé à intervalles réguliers", | ||
verbose_name="Nombre de structures contactées", | ||
), | ||
), | ||
] |
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