Skip to content

Commit

Permalink
Merge pull request #65 from dev-lymar/perf/dev-add-decorator-and-opti…
Browse files Browse the repository at this point in the history
…mize-views

Perf/dev add decorator and optimize views
  • Loading branch information
dev-lymar committed Jun 16, 2024
2 parents e03cd0e + 49b1e73 commit 0df7b9f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
4 changes: 3 additions & 1 deletion clients/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

# Базовый класс для получения данных по записям клиентов
class ClientAPIView(generics.ListCreateAPIView[Client]):
queryset = Client.objects.all()
queryset = Client.objects.select_related(
"director_position", "destination_city", "railway_station"
).all()
serializer_class = ClientSerializer
permission_classes = (IsAuthenticated,)

Expand Down
41 changes: 41 additions & 0 deletions decorators/query_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import functools
import time

from django.db import connection, reset_queries


def query_debugger(func):
"""
Decorator for tracking the number of database queries and the execution time of a function.
Applied to a function or class method to provide information on the number of executed queries
and the execution time of the function.
Usage example:
from decorators.query_decorator import query_debugger
from django.utils.decorators import method_decorator
# Applying the decorator to 'get', 'post' methods
or to all 'dispatch' methods of a view class:
@method_decorator(query_debugger, name='dispatch')
class MyView(View):
# class methods
"""

@functools.wraps(func)
def inner_func(*args, **kwargs):
reset_queries()

start_queries = len(connection.queries)

start = time.perf_counter()
result = func(*args, **kwargs)
end = time.perf_counter()

end_queries = len(connection.queries)

print(f"Function : {func.__name__}") # noqa
print(f"Number of Queries : {end_queries - start_queries}") # noqa
print(f"Finished in : {(end - start):.2f}s") # noqa
return result

return inner_func
4 changes: 2 additions & 2 deletions goods/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class GoodsViewSet(viewsets.ModelViewSet[Product]):
queryset = Product.objects.all()
queryset = Product.objects.select_related("flour_name", "brand", "package").all()
serializer_class = GoodsSerializer
permission_classes = (IsAuthenticated,)

Expand All @@ -16,6 +16,6 @@ def get_queryset(self):
if cached_goods:
return cached_goods
else:
goods = list(super().get_queryset())
goods = super().get_queryset()
cache.set("goods_list", goods, 1800)
return goods
6 changes: 1 addition & 5 deletions makedoc/data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ def get_products(validated_data):
results = []
cached_goods = cache.get("goods_list")
if cached_goods is None:
try:
cached_goods = list(Product.objects.all())
cache.set("goods_list", cached_goods, 1800)
except Exception:
cached_goods = list(Product.objects.all())
cached_goods = Product.objects.select_related("flour_name", "brand", "package").all()
for item in products_data:
product_id = item.get("product_id")
product_quantity = item.get("quantity")
Expand Down

0 comments on commit 0df7b9f

Please sign in to comment.