|
| 1 | +from django.conf import settings |
1 | 2 | from django.contrib import admin
|
2 | 3 | from django.db.models import Count
|
3 |
| - |
4 | 4 | from drf_api_logger.utils import database_log_enabled
|
5 | 5 |
|
6 | 6 | if database_log_enabled():
|
7 | 7 | 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) |
8 | 35 |
|
| 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 |
9 | 54 |
|
10 | 55 | class APILogsAdmin(admin.ModelAdmin):
|
11 | 56 |
|
| 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 | + |
12 | 65 | def added_on_time(self, obj):
|
13 | 66 | return obj.added_on.strftime("%d %b %Y %H:%M:%S")
|
14 | 67 |
|
@@ -47,6 +100,9 @@ def changelist_view(self, request, extra_context=None):
|
47 | 100 | response.context_data.update(extra_context)
|
48 | 101 | return response
|
49 | 102 |
|
| 103 | + def get_queryset(self, request): |
| 104 | + return super(APILogsAdmin, self).get_queryset(request) |
| 105 | + |
50 | 106 | def has_add_permission(self, request, obj=None):
|
51 | 107 | return False
|
52 | 108 |
|
|
0 commit comments