Skip to content

Commit

Permalink
refactor(stats): new Proof type stats. Rename Price type stats (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Oct 3, 2024
1 parent a7f18e9 commit 6401f80
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
6 changes: 6 additions & 0 deletions open_prices/proofs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@


class ProofQuerySet(models.QuerySet):
def has_type_price_tag(self):
return self.filter(type=proof_constants.TYPE_PRICE_TAG)

def has_type_receipt(self):
return self.filter(type=proof_constants.TYPE_RECEIPT)

def has_type_single_shop(self):
return self.filter(type__in=proof_constants.TYPE_SINGLE_SHOP_LIST)

Expand Down
32 changes: 32 additions & 0 deletions open_prices/stats/migrations/0002_add_proof_type_counts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 5.1 on 2024-10-03 21:38

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("stats", "0001_initial"),
]

operations = [
migrations.RenameField(
model_name="totalstats",
old_name="price_barcode_count",
new_name="price_type_product_code_count",
),
migrations.RenameField(
model_name="totalstats",
old_name="price_category_count",
new_name="price_type_category_tag_count",
),
migrations.AddField(
model_name="totalstats",
name="proof_type_price_tag_count",
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name="totalstats",
name="proof_type_receipt_count",
field=models.PositiveIntegerField(default=0),
),
]
32 changes: 20 additions & 12 deletions open_prices/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@


class TotalStats(SingletonModel):
PRICE_COUNT_FIELDS = ["price_count", "price_barcode_count", "price_category_count"]
PRICE_COUNT_FIELDS = [
"price_count",
"price_type_product_code_count",
"price_type_category_tag_count",
]
PRODUCT_COUNT_FIELDS = ["product_count", "product_with_price_count"]
LOCATION_COUNT_FIELDS = ["location_count", "location_with_price_count"]
PROOF_COUNT_FIELDS = ["proof_count", "proof_with_price_count"]
PROOF_COUNT_FIELDS = [
"proof_count",
"proof_with_price_count",
"proof_type_price_tag_count",
"proof_type_receipt_count",
]
USER_COUNT_FIELDS = ["user_count", "user_with_price_count"]
COUNT_FIELDS = (
PRICE_COUNT_FIELDS
Expand All @@ -18,14 +27,16 @@ class TotalStats(SingletonModel):
)

price_count = models.PositiveIntegerField(default=0)
price_barcode_count = models.PositiveIntegerField(default=0)
price_category_count = models.PositiveIntegerField(default=0)
price_type_product_code_count = models.PositiveIntegerField(default=0)
price_type_category_tag_count = models.PositiveIntegerField(default=0)
product_count = models.PositiveIntegerField(default=0)
product_with_price_count = models.PositiveIntegerField(default=0)
location_count = models.PositiveIntegerField(default=0)
location_with_price_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)
user_count = models.PositiveIntegerField(default=0)
user_with_price_count = models.PositiveIntegerField(default=0)

Expand All @@ -39,18 +50,13 @@ def update_price_stats(self):
from open_prices.prices.models import Price

self.price_count = Price.objects.count()
self.price_barcode_count = Price.objects.filter(
self.price_type_product_code_count = Price.objects.filter(
product_code__isnull=False
).count()
self.price_category_count = Price.objects.filter(
self.price_type_category_tag_count = Price.objects.filter(
category_tag__isnull=False
).count()
self.save(
update_fields=self.PRICE_COUNT_FIELDS
+ [
"updated",
]
)
self.save(update_fields=self.PRICE_COUNT_FIELDS + ["updated"])

def update_product_stats(self):
from open_prices.products.models import Product
Expand All @@ -74,6 +80,8 @@ def update_proof_stats(self):
self.proof_count = Proof.objects.count()
self.proof_with_price_count = Proof.objects.has_prices().count()
# 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.save(update_fields=self.PROOF_COUNT_FIELDS + ["updated"])

def update_user_stats(self):
Expand Down
15 changes: 11 additions & 4 deletions open_prices/stats/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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.stats.models import TotalStats
from open_prices.users.factories import UserFactory
Expand Down Expand Up @@ -34,11 +35,13 @@ def setUpTestData(cls):
cls.location = LocationFactory(**LOCATION_NODE_652825274)
cls.location_2 = LocationFactory()
cls.proof = 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(
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,
Expand Down Expand Up @@ -72,13 +75,13 @@ def setUpTestData(cls):

def test_update_price_stats(self):
self.assertEqual(self.total_stats.price_count, 0)
self.assertEqual(self.total_stats.price_barcode_count, 0)
self.assertEqual(self.total_stats.price_category_count, 0)
self.assertEqual(self.total_stats.price_type_product_code_count, 0)
self.assertEqual(self.total_stats.price_type_category_tag_count, 0)
# update_price_stats() will update price_counts
self.total_stats.update_price_stats()
self.assertEqual(self.total_stats.price_count, 3)
self.assertEqual(self.total_stats.price_barcode_count, 2)
self.assertEqual(self.total_stats.price_category_count, 1)
self.assertEqual(self.total_stats.price_type_product_code_count, 2)
self.assertEqual(self.total_stats.price_type_category_tag_count, 1)

def test_update_product_stats(self):
self.assertEqual(self.total_stats.product_count, 0)
Expand All @@ -99,10 +102,14 @@ def test_update_location_stats(self):
def test_update_proof_stats(self):
self.assertEqual(self.total_stats.proof_count, 0)
self.assertEqual(self.total_stats.proof_with_price_count, 0)
self.assertEqual(self.total_stats.proof_type_price_tag_count, 0)
self.assertEqual(self.total_stats.proof_type_receipt_count, 0)
# update_proof_stats() will update proof_counts
self.total_stats.update_proof_stats()
self.assertEqual(self.total_stats.proof_count, 2)
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)

def test_update_user_stats(self):
self.assertEqual(self.total_stats.user_count, 0)
Expand Down

0 comments on commit 6401f80

Please sign in to comment.