Skip to content

Commit 550323d

Browse files
Slow query filter added.
1 parent bf27fb8 commit 550323d

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# DRF API Logger
2-
![version](https://img.shields.io/badge/version-1.0.7-blue.svg)
2+
![version](https://img.shields.io/badge/version-1.0.8-blue.svg)
33
[![Downloads](https://pepy.tech/badge/drf-api-logger)](http://pepy.tech/project/drf-api-logger)
44
[![Downloads](https://pepy.tech/badge/drf-api-logger/month)](https://pepy.tech/project/drf-api-logger)
55
[![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/)
@@ -178,6 +178,16 @@ You can specify an endpoint of API should have absolute URI or not by setting th
178178
DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE' # Default to ABSOLUTE if not specified
179179
# Possible values are ABSOLUTE, FULL_PATH or RAW_URI
180180
```
181+
182+
### Want to identify slow APIs? (Optional)
183+
You can also identify slow APIs by specifying `DRF_API_LOGGER_SLOW_API_ABOVE` in settings.py.
184+
185+
A new filter (By API Performance) will be visible, and you can choose slow or fast API.
186+
```python
187+
DRF_API_LOGGER_SLOW_API_ABOVE = 200 # Default to None
188+
# Specify in milli-seconds.
189+
```
190+
181191
Considering we are accessing the following URL: http://127.0.0.1:8000/api/v1/?page=123
182192
DRF_API_LOGGER_PATH_TYPE possible values are:
183193
1. ABSOLUTE (Default) :

drf_api_logger/admin.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,67 @@
1+
from django.conf import settings
12
from django.contrib import admin
23
from django.db.models import Count
3-
44
from drf_api_logger.utils import database_log_enabled
55

66
if database_log_enabled():
77
from drf_api_logger.models import APILogsModel
8+
from django.utils.translation import gettext_lazy as _
9+
10+
class SlowAPIsFilter(admin.SimpleListFilter):
11+
title = _('API Performance')
12+
13+
# Parameter for the filter that will be used in the URL query.
14+
parameter_name = 'api_performance'
15+
16+
def __init__(self, request, params, model, model_admin):
17+
super().__init__(request, params, model, model_admin)
18+
if hasattr(settings, 'DRF_API_LOGGER_SLOW_API_ABOVE'):
19+
if type(settings.DRF_API_LOGGER_SLOW_API_ABOVE) == int: # Making sure for integer value.
20+
self.DRF_API_LOGGER_SLOW_API_ABOVE = settings.DRF_API_LOGGER_SLOW_API_ABOVE / 1000 # Converting to seconds.
21+
22+
def lookups(self, request, model_admin):
23+
"""
24+
Returns a list of tuples. The first element in each
25+
tuple is the coded value for the option that will
26+
appear in the URL query. The second element is the
27+
human-readable name for the option that will appear
28+
in the right sidebar.
29+
"""
30+
slow = 'Slow'
31+
fast = 'Fast'
32+
if hasattr(settings, 'DRF_API_LOGGER_SLOW_API_ABOVE'):
33+
slow += ', >={}ms'.format(settings.DRF_API_LOGGER_SLOW_API_ABOVE)
34+
fast += ', <{}ms'.format(settings.DRF_API_LOGGER_SLOW_API_ABOVE)
835

36+
return (
37+
('slow', _(slow)),
38+
('fast', _(fast)),
39+
)
40+
41+
def queryset(self, request, queryset):
42+
"""
43+
Returns the filtered queryset based on the value
44+
provided in the query string and retrievable via
45+
`self.value()`.
46+
"""
47+
# to decide how to filter the queryset.
48+
if self.value() == 'slow':
49+
return queryset.filter(execution_time__gte=self.DRF_API_LOGGER_SLOW_API_ABOVE)
50+
if self.value() == 'fast':
51+
return queryset.filter(execution_time__lt=self.DRF_API_LOGGER_SLOW_API_ABOVE)
52+
53+
return queryset
954

1055
class APILogsAdmin(admin.ModelAdmin):
1156

57+
def __init__(self, model, admin_site):
58+
super().__init__(model, admin_site)
59+
self.DRF_API_LOGGER_SLOW_API_ABOVE = None
60+
if hasattr(settings, 'DRF_API_LOGGER_SLOW_API_ABOVE'):
61+
if type(settings.DRF_API_LOGGER_SLOW_API_ABOVE) == int: # Making sure for integer value.
62+
self.DRF_API_LOGGER_SLOW_API_ABOVE = settings.DRF_API_LOGGER_SLOW_API_ABOVE / 1000 # Converting to seconds.
63+
self.list_filter += (SlowAPIsFilter,)
64+
1265
def added_on_time(self, obj):
1366
return obj.added_on.strftime("%d %b %Y %H:%M:%S")
1467

@@ -47,6 +100,9 @@ def changelist_view(self, request, extra_context=None):
47100
response.context_data.update(extra_context)
48101
return response
49102

103+
def get_queryset(self, request):
104+
return super(APILogsAdmin, self).get_queryset(request)
105+
50106
def has_add_permission(self, request, obj=None):
51107
return False
52108

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def get_long_desc():
2121

2222
setuptools.setup(
2323
name="drf_api_logger",
24-
version="1.0.7",
24+
version="1.0.8",
2525
author="Vishal Anand",
2626
author_email="[email protected]",
2727
description="An API Logger for your Django Rest Framework project.",

0 commit comments

Comments
 (0)