Skip to content

Commit

Permalink
feat(API): new /stats endpoint for TotalStats (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 28, 2024
1 parent 034025b commit 4f8384f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
Empty file.
10 changes: 10 additions & 0 deletions open_prices/api/stats/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework import serializers

from open_prices.stats.models import TotalStats


class TotalStatsSerializer(serializers.ModelSerializer):
class Meta:
model = TotalStats
# fields = "__all__"
exclude = ["id", "created"]
15 changes: 15 additions & 0 deletions open_prices/api/stats/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.urls import reverse
from rest_framework.test import APITestCase

from open_prices.stats.models import TotalStats


class StatsTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.url = reverse("api:stats")

def test_get_total_stats(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json().keys()), len(TotalStats.COUNT_FIELDS) + 1)
17 changes: 17 additions & 0 deletions open_prices/api/stats/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from drf_spectacular.utils import extend_schema
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from open_prices.api.stats.serializers import TotalStatsSerializer
from open_prices.stats.models import TotalStats


class StatsView(APIView):
authentication_classes = []
permission_classes = []

@extend_schema(responses=TotalStatsSerializer, tags=["stats"])
def get(self, request: Request) -> Response:
serializer = TotalStatsSerializer(TotalStats.get_solo())
return Response(serializer.data, status=200)
3 changes: 3 additions & 0 deletions open_prices/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from open_prices.api.prices.views import PriceViewSet
from open_prices.api.products.views import ProductViewSet
from open_prices.api.proofs.views import ProofViewSet
from open_prices.api.stats.views import StatsView
from open_prices.api.users.views import UserViewSet
from open_prices.api.views import StatusView

Expand All @@ -27,6 +28,8 @@
# auth urls
path("v1/auth", LoginView.as_view(), name="login"),
path("v1/session", SessionView.as_view(), name="session"),
# stats urls
path("v1/stats", StatsView.as_view(), name="stats"),
# health check
path("v1/status", StatusView.as_view(), name="status"),
# Swagger / OpenAPI documentation
Expand Down
7 changes: 7 additions & 0 deletions open_prices/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ class TotalStats(SingletonModel):
LOCATION_COUNT_FIELDS = ["location_count", "location_with_price_count"]
PROOF_COUNT_FIELDS = ["proof_count", "proof_with_price_count"]
USER_COUNT_FIELDS = ["user_count", "user_with_price_count"]
COUNT_FIELDS = (
PRICE_COUNT_FIELDS
+ PRODUCT_COUNT_FIELDS
+ LOCATION_COUNT_FIELDS
+ PROOF_COUNT_FIELDS
+ USER_COUNT_FIELDS
)

price_count = models.PositiveIntegerField(default=0)
price_barcode_count = models.PositiveIntegerField(default=0)
Expand Down

0 comments on commit 4f8384f

Please sign in to comment.