From 411222be6d2da05ec3acb7a7948a6268c29b65d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Odini?= Date: Fri, 18 Oct 2024 10:24:57 +0200 Subject: [PATCH] refactor(Stats): extra Proof type stats. new Location type stats (#524) --- open_prices/locations/models.py | 6 ++++ open_prices/locations/tests.py | 13 ++++++-- open_prices/proofs/models.py | 6 ++++ ...tra_proof_type_and_location_type_counts.py | 32 +++++++++++++++++++ open_prices/stats/models.py | 19 ++++++++++- open_prices/stats/tests.py | 4 +++ 6 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 open_prices/stats/migrations/0003_add_extra_proof_type_and_location_type_counts.py diff --git a/open_prices/locations/models.py b/open_prices/locations/models.py index b23793e8..5a628cbd 100644 --- a/open_prices/locations/models.py +++ b/open_prices/locations/models.py @@ -12,6 +12,12 @@ class LocationQuerySet(models.QuerySet): + def has_type_osm(self): + return self.filter(type=location_constants.TYPE_OSM) + + def has_type_online(self): + return self.filter(type=location_constants.TYPE_ONLINE) + def has_prices(self): return self.filter(price_count__gt=0) diff --git a/open_prices/locations/tests.py b/open_prices/locations/tests.py index 0b677ad4..9a302031 100644 --- a/open_prices/locations/tests.py +++ b/open_prices/locations/tests.py @@ -17,6 +17,10 @@ "osm_type": "NODE", "osm_name": "Monoprix", } +LOCATION_ONLINE_DECATHLON = { + "type": location_constants.TYPE_ONLINE, + "website_url": "https://www.decathlon.fr/", +} class LocationModelSaveTest(TestCase): @@ -121,15 +125,20 @@ def test_location_online_validation(self): class LocationQuerySetTest(TestCase): @classmethod def setUpTestData(cls): - cls.location_without_price = LocationFactory() - cls.location_with_price = LocationFactory() + cls.location_with_price = LocationFactory(**LOCATION_OSM_NODE_652825274) + cls.location_without_price = LocationFactory(**LOCATION_ONLINE_DECATHLON) PriceFactory( location_osm_id=cls.location_with_price.osm_id, location_osm_type=cls.location_with_price.osm_type, price=1.0, ) + def test_has_type_osm(self): + self.assertEqual(Location.objects.count(), 2) + self.assertEqual(Location.objects.has_type_osm().count(), 1) + def test_has_prices(self): + self.assertEqual(Location.objects.count(), 2) self.assertEqual(Location.objects.has_prices().count(), 1) def test_with_stats(self): diff --git a/open_prices/proofs/models.py b/open_prices/proofs/models.py index 21e2c5bf..c3249e7c 100644 --- a/open_prices/proofs/models.py +++ b/open_prices/proofs/models.py @@ -17,6 +17,12 @@ def has_type_price_tag(self): def has_type_receipt(self): return self.filter(type=proof_constants.TYPE_RECEIPT) + def has_type_gdpr_request(self): + return self.filter(type=proof_constants.TYPE_GDPR_REQUEST) + + def has_type_shop_import(self): + return self.filter(type=proof_constants.TYPE_SHOP_IMPORT) + def has_type_single_shop(self): return self.filter(type__in=proof_constants.TYPE_SINGLE_SHOP_LIST) diff --git a/open_prices/stats/migrations/0003_add_extra_proof_type_and_location_type_counts.py b/open_prices/stats/migrations/0003_add_extra_proof_type_and_location_type_counts.py new file mode 100644 index 00000000..d348af38 --- /dev/null +++ b/open_prices/stats/migrations/0003_add_extra_proof_type_and_location_type_counts.py @@ -0,0 +1,32 @@ +# Generated by Django 5.1 on 2024-10-18 08:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("stats", "0002_add_proof_type_counts"), + ] + + operations = [ + migrations.AddField( + model_name="totalstats", + name="location_type_online_count", + field=models.PositiveIntegerField(default=0), + ), + migrations.AddField( + model_name="totalstats", + name="location_type_osm_count", + field=models.PositiveIntegerField(default=0), + ), + migrations.AddField( + model_name="totalstats", + name="proof_type_gdpr_request_count", + field=models.PositiveIntegerField(default=0), + ), + migrations.AddField( + model_name="totalstats", + name="proof_type_shop_import_count", + field=models.PositiveIntegerField(default=0), + ), + ] diff --git a/open_prices/stats/models.py b/open_prices/stats/models.py index e9cee668..a6391323 100644 --- a/open_prices/stats/models.py +++ b/open_prices/stats/models.py @@ -10,12 +10,19 @@ class TotalStats(SingletonModel): "price_type_category_tag_count", ] PRODUCT_COUNT_FIELDS = ["product_count", "product_with_price_count"] - LOCATION_COUNT_FIELDS = ["location_count", "location_with_price_count"] + LOCATION_COUNT_FIELDS = [ + "location_count", + "location_with_price_count", + "location_type_osm_count", + "location_type_online_count", + ] PROOF_COUNT_FIELDS = [ "proof_count", "proof_with_price_count", "proof_type_price_tag_count", "proof_type_receipt_count", + "proof_type_gdpr_request_count", + "proof_type_shop_import_count", ] USER_COUNT_FIELDS = ["user_count", "user_with_price_count"] COUNT_FIELDS = ( @@ -33,10 +40,14 @@ class TotalStats(SingletonModel): product_with_price_count = models.PositiveIntegerField(default=0) location_count = models.PositiveIntegerField(default=0) location_with_price_count = models.PositiveIntegerField(default=0) + location_type_osm_count = models.PositiveIntegerField(default=0) + location_type_online_count = models.PositiveIntegerField(default=0) proof_count = models.PositiveIntegerField(default=0) proof_with_price_count = models.PositiveIntegerField(default=0) proof_type_price_tag_count = models.PositiveIntegerField(default=0) 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) user_count = models.PositiveIntegerField(default=0) user_with_price_count = models.PositiveIntegerField(default=0) @@ -72,6 +83,8 @@ def update_location_stats(self): self.location_count = Location.objects.count() self.location_with_price_count = Location.objects.has_prices().count() # self.location_with_price_count = User.objects.values_list("location_id", flat=True).distinct().count() # noqa + self.location_type_osm_count = Location.objects.has_type_osm().count() + self.location_type_online_count = Location.objects.has_type_online().count() self.save(update_fields=self.LOCATION_COUNT_FIELDS + ["updated"]) def update_proof_stats(self): @@ -82,6 +95,10 @@ def update_proof_stats(self): # self.proof_with_price_count = User.objects.values_list("proof_id", flat=True).distinct().count() # noqa self.proof_type_price_tag_count = Proof.objects.has_type_price_tag().count() self.proof_type_receipt_count = Proof.objects.has_type_receipt().count() + self.proof_type_gdpr_request_count = ( + Proof.objects.has_type_gdpr_request().count() + ) + self.proof_type_shop_import_count = Proof.objects.has_type_shop_import().count() self.save(update_fields=self.PROOF_COUNT_FIELDS + ["updated"]) def update_user_stats(self): diff --git a/open_prices/stats/tests.py b/open_prices/stats/tests.py index cfe9c033..69b831f1 100644 --- a/open_prices/stats/tests.py +++ b/open_prices/stats/tests.py @@ -98,6 +98,8 @@ def test_update_location_stats(self): self.total_stats.update_location_stats() self.assertEqual(self.total_stats.location_count, 2) self.assertEqual(self.total_stats.location_with_price_count, 1) + self.assertEqual(self.total_stats.location_type_osm_count, 2) + self.assertEqual(self.total_stats.location_type_online_count, 0) def test_update_proof_stats(self): self.assertEqual(self.total_stats.proof_count, 0) @@ -110,6 +112,8 @@ def test_update_proof_stats(self): self.assertEqual(self.total_stats.proof_with_price_count, 1) self.assertEqual(self.total_stats.proof_type_price_tag_count, 1) self.assertEqual(self.total_stats.proof_type_receipt_count, 1) + 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_user_stats(self): self.assertEqual(self.total_stats.user_count, 0)