Skip to content

Commit

Permalink
refactor(users): New weekly CRON to update User counts (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Sep 22, 2024
1 parent 4d66eee commit f7072b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
1 change: 1 addition & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
"import openfoodfacts",
"from open_prices.common import openfoodfacts as common_openfoodfacts",
"from open_prices.common import openstreetmap as common_openstreetmap",
"from open_prices.common import tasks as common_tasks",
]


Expand Down
48 changes: 31 additions & 17 deletions open_prices/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,30 @@
from open_prices.locations.models import Location
from open_prices.prices.models import Price
from open_prices.proofs.models import Proof
from open_prices.users.models import User


def import_product_db_task():
"""
Sync product database with Open Food Facts
"""
for flavor in [Flavor.off, Flavor.obf, Flavor.opff, Flavor.opf]:
import_product_db(flavor=flavor)


def update_user_counts_task():
"""
Update all user price_counts
"""
for user in User.objects.all():
for field in User.COUNT_FIELDS:
getattr(user, f"update_{field}")()


def dump_db_task():
"""
Dump the database as JSONL files to the data directory
"""
output_dir = Path(os.path.join(settings.BASE_DIR, "static/data"))

for table_name, model_class, schema_class in (
Expand All @@ -32,20 +48,18 @@ def dump_db_task():
export_model_to_jsonl_gz(table_name, model_class, schema_class, output_dir)


# sync product database with Open Food Facts daily at 15:00
# https://cron.help/#0_15_*_*_*
schedule(
"open_prices.common.tasks.import_product_db_task",
name="import_product_db_task",
schedule_type=Schedule.CRON,
cron="0 15 * * *",
)

# dump the database as JSONL files to the data directory daily at 23:00
# https://cron.help/#0_23_*_*_*
schedule(
"open_prices.common.tasks.dump_db_task",
name="dump_db_task",
schedule_type=Schedule.CRON,
cron="0 23 * * *",
)
CRON_SCHEDULES = {
"import_product_db_task": "0 15 * * *", # daily at 15:00
"update_user_counts_task": "0 2 * * 1", # every start of the week
"dump_db_task": "0 23 * * *", # daily at 23:00
}

for task_name, task_cron in CRON_SCHEDULES.items():
if not Schedule.objects.filter(name=task_name).exists():
schedule(
f"open_prices.common.tasks.{task_name}",
name=task_name,
schedule_type=Schedule.CRON,
cron=task_cron,
)
print(f"Task {task_name} scheduled with cron {task_cron}")
7 changes: 2 additions & 5 deletions open_prices/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ def has_prices(self):


class User(models.Model):
COUNT_FIELDS = ["price_count", "location_count", "product_count", "proof_count"]
SERIALIZED_FIELDS = [
"user_id",
"price_count",
"location_count",
"product_count",
"proof_count",
]
] + COUNT_FIELDS

user_id = models.CharField(primary_key=True)

Expand Down

0 comments on commit f7072b8

Please sign in to comment.