From 46437066f0682bff9ef19df92d6e055f9445c594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Odini?= Date: Mon, 23 Dec 2024 11:47:23 +0100 Subject: [PATCH] refactor(Stats): new PriceTag stats (#652) --- open_prices/common/tasks.py | 1 + ...004_totalstats_price_tag_count_and_more.py | 27 +++++++++++++++++++ open_prices/stats/models.py | 20 ++++++++++++++ open_prices/stats/tests.py | 27 +++++++++++++++---- 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 open_prices/stats/migrations/0004_totalstats_price_tag_count_and_more.py diff --git a/open_prices/common/tasks.py b/open_prices/common/tasks.py index 907705ad..de44850b 100644 --- a/open_prices/common/tasks.py +++ b/open_prices/common/tasks.py @@ -55,6 +55,7 @@ def update_total_stats_task(): total_stats.update_product_stats() total_stats.update_location_stats() total_stats.update_proof_stats() + total_stats.update_price_tag_stats() total_stats.update_user_stats() diff --git a/open_prices/stats/migrations/0004_totalstats_price_tag_count_and_more.py b/open_prices/stats/migrations/0004_totalstats_price_tag_count_and_more.py new file mode 100644 index 00000000..ec802b72 --- /dev/null +++ b/open_prices/stats/migrations/0004_totalstats_price_tag_count_and_more.py @@ -0,0 +1,27 @@ +# Generated by Django 5.1.4 on 2024-12-23 10:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("stats", "0003_add_extra_proof_type_and_location_type_counts"), + ] + + operations = [ + migrations.AddField( + model_name="totalstats", + name="price_tag_count", + field=models.PositiveIntegerField(default=0), + ), + migrations.AddField( + model_name="totalstats", + name="price_tag_linked_to_price_count", + field=models.PositiveIntegerField(default=0), + ), + migrations.AddField( + model_name="totalstats", + name="price_tag_unknown_count", + field=models.PositiveIntegerField(default=0), + ), + ] diff --git a/open_prices/stats/models.py b/open_prices/stats/models.py index a6391323..e8eca917 100644 --- a/open_prices/stats/models.py +++ b/open_prices/stats/models.py @@ -2,6 +2,8 @@ from django.utils import timezone from solo.models import SingletonModel +from open_prices.common.constants import PriceTagStatus + class TotalStats(SingletonModel): PRICE_COUNT_FIELDS = [ @@ -24,6 +26,11 @@ class TotalStats(SingletonModel): "proof_type_gdpr_request_count", "proof_type_shop_import_count", ] + PRICE_TAG_COUNT_FIELDS = [ + "price_tag_count", + "price_tag_unknown_count", + "price_tag_linked_to_price_count", + ] USER_COUNT_FIELDS = ["user_count", "user_with_price_count"] COUNT_FIELDS = ( PRICE_COUNT_FIELDS @@ -48,6 +55,9 @@ class TotalStats(SingletonModel): proof_type_receipt_count = models.PositiveIntegerField(default=0) proof_type_gdpr_request_count = models.PositiveIntegerField(default=0) proof_type_shop_import_count = models.PositiveIntegerField(default=0) + price_tag_count = models.PositiveIntegerField(default=0) + price_tag_unknown_count = models.PositiveIntegerField(default=0) + price_tag_linked_to_price_count = models.PositiveIntegerField(default=0) user_count = models.PositiveIntegerField(default=0) user_with_price_count = models.PositiveIntegerField(default=0) @@ -101,6 +111,16 @@ def update_proof_stats(self): self.proof_type_shop_import_count = Proof.objects.has_type_shop_import().count() self.save(update_fields=self.PROOF_COUNT_FIELDS + ["updated"]) + def update_price_tag_stats(self): + from open_prices.proofs.models import PriceTag + + self.price_tag_count = PriceTag.objects.count() + self.price_tag_unknown_count = PriceTag.objects.filter(status=None).count() + self.price_tag_linked_to_price_count = PriceTag.objects.filter( + status=PriceTagStatus.linked_to_price.value + ).count() + self.save(update_fields=self.PRICE_TAG_COUNT_FIELDS + ["updated"]) + def update_user_stats(self): from open_prices.users.models import User diff --git a/open_prices/stats/tests.py b/open_prices/stats/tests.py index 0ed3f73d..4f85769b 100644 --- a/open_prices/stats/tests.py +++ b/open_prices/stats/tests.py @@ -1,11 +1,12 @@ from django.db import IntegrityError from django.test import TestCase +from open_prices.common.constants import PriceTagStatus from open_prices.locations.factories import LocationFactory from open_prices.prices import constants as price_constants from open_prices.prices.factories import PriceFactory from open_prices.proofs import constants as proof_constants -from open_prices.proofs.factories import ProofFactory +from open_prices.proofs.factories import PriceTagFactory, ProofFactory from open_prices.stats.models import TotalStats from open_prices.users.factories import UserFactory @@ -34,23 +35,23 @@ def setUpTestData(cls): cls.user_2 = UserFactory() cls.location = LocationFactory(**LOCATION_NODE_652825274) cls.location_2 = LocationFactory() - cls.proof = ProofFactory( + cls.proof_price_tag = ProofFactory( type=proof_constants.TYPE_PRICE_TAG, location_osm_id=cls.location.osm_id, location_osm_type=cls.location.osm_type, owner=cls.user.user_id, ) - cls.proof_2 = ProofFactory( + cls.proof_receipt = ProofFactory( type=proof_constants.TYPE_RECEIPT, location_osm_id=cls.location_2.osm_id, location_osm_type=cls.location_2.osm_type, owner=cls.user_2.user_id, ) - PriceFactory( + cls.price = PriceFactory( product_code="0123456789100", location_osm_id=cls.location.osm_id, location_osm_type=cls.location.osm_type, - proof_id=cls.proof.id, + proof_id=cls.proof_price_tag.id, price=1.0, owner=cls.user.user_id, ) @@ -72,6 +73,12 @@ def setUpTestData(cls): price=2.0, owner=cls.user_2.user_id, ) + PriceTagFactory(proof=cls.proof_price_tag, status=None) + PriceTagFactory( + proof=cls.proof_price_tag, + price=cls.price, + status=PriceTagStatus.linked_to_price.value, + ) def test_update_price_stats(self): self.assertEqual(self.total_stats.price_count, 0) @@ -115,6 +122,16 @@ def test_update_proof_stats(self): self.assertEqual(self.total_stats.proof_type_gdpr_request_count, 0) self.assertEqual(self.total_stats.proof_type_shop_import_count, 0) + def test_update_price_tag_stats(self): + self.assertEqual(self.total_stats.price_tag_count, 0) + self.assertEqual(self.total_stats.price_tag_unknown_count, 0) + self.assertEqual(self.total_stats.price_tag_linked_to_price_count, 0) + # update_price_tag_stats() will update price_tag_counts + self.total_stats.update_price_tag_stats() + self.assertEqual(self.total_stats.price_tag_count, 2) + self.assertEqual(self.total_stats.price_tag_unknown_count, 1) + self.assertEqual(self.total_stats.price_tag_linked_to_price_count, 1) + def test_update_user_stats(self): self.assertEqual(self.total_stats.user_count, 0) self.assertEqual(self.total_stats.user_with_price_count, 0)