Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(Stats): new PriceTag stats #652

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions open_prices/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
Original file line number Diff line number Diff line change
@@ -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),
),
]
20 changes: 20 additions & 0 deletions open_prices/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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

Expand Down
27 changes: 22 additions & 5 deletions open_prices/stats/tests.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading