Skip to content

Commit ffe0b1e

Browse files
committed
move stats views to django_rq.stats_views module
1 parent edd586a commit ffe0b1e

File tree

3 files changed

+60
-52
lines changed

3 files changed

+60
-52
lines changed

django_rq/stats_views.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from django.contrib import admin
2+
from django.contrib.admin.views.decorators import staff_member_required
3+
from django.http import Http404, HttpResponse, JsonResponse
4+
from django.shortcuts import render
5+
from django.views.decorators.cache import never_cache
6+
7+
from .settings import API_TOKEN
8+
from .utils import get_scheduler_statistics, get_statistics
9+
10+
try:
11+
import prometheus_client
12+
13+
from .metrics_collector import RQCollector
14+
except ImportError:
15+
prometheus_client = RQCollector = None # type: ignore[assignment, misc]
16+
17+
registry = None
18+
19+
20+
@never_cache
21+
@staff_member_required
22+
def prometheus_metrics(request):
23+
global registry
24+
25+
if not RQCollector: # type: ignore[truthy-function]
26+
raise Http404
27+
28+
if not registry:
29+
registry = prometheus_client.CollectorRegistry(auto_describe=True)
30+
registry.register(RQCollector())
31+
32+
encoder, content_type = prometheus_client.exposition.choose_encoder(request.META.get('HTTP_ACCEPT', ''))
33+
if 'name[]' in request.GET:
34+
registry = registry.restricted_registry(request.GET.getlist('name[]'))
35+
36+
return HttpResponse(encoder(registry), headers={'Content-Type': content_type})
37+
38+
39+
@never_cache
40+
@staff_member_required
41+
def stats(request):
42+
context_data = {
43+
**admin.site.each_context(request),
44+
**get_statistics(run_maintenance_tasks=True),
45+
**get_scheduler_statistics(),
46+
}
47+
return render(request, 'django_rq/stats.html', context_data)
48+
49+
50+
def stats_json(request, token=None):
51+
if request.user.is_staff or (token and token == API_TOKEN):
52+
return JsonResponse(get_statistics())
53+
54+
return JsonResponse(
55+
{"error": True, "description": "Please configure API_TOKEN in settings.py before accessing this view."}
56+
)

django_rq/urls.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from django.urls import re_path
22

3-
from . import views
3+
from . import stats_views, views
44
from .metrics_collector import RQCollector
55

66
metrics_view = [
7-
re_path(r'^metrics/?$', views.prometheus_metrics, name='rq_metrics'),
7+
re_path(r'^metrics/?$', stats_views.prometheus_metrics, name='rq_metrics'),
88
] if RQCollector else [] # type: ignore[truthy-function]
99

1010
urlpatterns = [
11-
re_path(r'^$', views.stats, name='rq_home'),
12-
re_path(r'^stats.json/(?P<token>[\w]+)?/?$', views.stats_json, name='rq_home_json'),
11+
re_path(r'^$', stats_views.stats, name='rq_home'),
12+
re_path(r'^stats.json/(?P<token>[\w]+)?/?$', stats_views.stats_json, name='rq_home_json'),
1313
*metrics_view,
1414
re_path(r'^queues/(?P<queue_index>[\d]+)/$', views.jobs, name='rq_jobs'),
1515
re_path(r'^workers/(?P<queue_index>[\d]+)/$', views.workers, name='rq_workers'),

django_rq/views.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,6 @@
2828
from .settings import API_TOKEN, QUEUES_MAP
2929
from .utils import get_executions, get_jobs, get_scheduler_statistics, get_statistics, stop_jobs
3030

31-
try:
32-
import prometheus_client
33-
34-
from .metrics_collector import RQCollector
35-
except ImportError:
36-
prometheus_client = RQCollector = None # type: ignore[assignment, misc]
37-
38-
registry = None
39-
40-
41-
@never_cache
42-
@staff_member_required
43-
def stats(request):
44-
context_data = {
45-
**admin.site.each_context(request),
46-
**get_statistics(run_maintenance_tasks=True),
47-
**get_scheduler_statistics(),
48-
}
49-
return render(request, 'django_rq/stats.html', context_data)
50-
51-
52-
def stats_json(request, token=None):
53-
if request.user.is_staff or (token and token == API_TOKEN):
54-
return JsonResponse(get_statistics())
55-
56-
return JsonResponse(
57-
{"error": True, "description": "Please configure API_TOKEN in settings.py before accessing this view."}
58-
)
59-
60-
61-
@never_cache
62-
@staff_member_required
63-
def prometheus_metrics(request):
64-
global registry
65-
66-
if not RQCollector: # type: ignore[truthy-function]
67-
raise Http404
68-
69-
if not registry:
70-
registry = prometheus_client.CollectorRegistry(auto_describe=True)
71-
registry.register(RQCollector())
72-
73-
encoder, content_type = prometheus_client.exposition.choose_encoder(request.META.get('HTTP_ACCEPT', ''))
74-
if 'name[]' in request.GET:
75-
registry = registry.restricted_registry(request.GET.getlist('name[]'))
76-
77-
return HttpResponse(encoder(registry), headers={'Content-Type': content_type})
78-
7931

8032
@never_cache
8133
@staff_member_required

0 commit comments

Comments
 (0)