Skip to content

Commit

Permalink
refactor(Stats): extra Proof type stats. new Location type stats (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Oct 18, 2024
1 parent edea8eb commit 411222b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
6 changes: 6 additions & 0 deletions open_prices/locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 11 additions & 2 deletions open_prices/locations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions open_prices/proofs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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),
),
]
19 changes: 18 additions & 1 deletion open_prices/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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)

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions open_prices/stats/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 411222b

Please sign in to comment.