From 3d8f685cbfbeb8d39342b457beb240f6d0c221f4 Mon Sep 17 00:00:00 2001 From: Jan David Mol <mol@astron.nl> Date: Wed, 11 Oct 2023 12:24:29 +0200 Subject: [PATCH] Add example django app with models tracked by metrics --- README.md | 7 ------- django/mysite/mysite/settings.py | 12 +++++++++++ django/mysite/mysite/urls.py | 5 +++++ django/mysite/polls/__init__.py | 0 django/mysite/polls/admin.py | 3 +++ django/mysite/polls/apps.py | 6 ++++++ django/mysite/polls/migrations/__init__.py | 0 django/mysite/polls/models.py | 13 ++++++++++++ django/mysite/polls/tests.py | 3 +++ django/mysite/polls/urls.py | 14 +++++++++++++ django/mysite/polls/views.py | 23 ++++++++++++++++++++++ django/requirements.txt | 7 +++++++ docker-compose.yml | 10 +++++----- 13 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 django/mysite/polls/__init__.py create mode 100644 django/mysite/polls/admin.py create mode 100644 django/mysite/polls/apps.py create mode 100644 django/mysite/polls/migrations/__init__.py create mode 100644 django/mysite/polls/models.py create mode 100644 django/mysite/polls/tests.py create mode 100644 django/mysite/polls/urls.py create mode 100644 django/mysite/polls/views.py diff --git a/README.md b/README.md index 3a7ebde..f12cbfe 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,6 @@ [](http://real-astron.nl/grafana) -TODO: - -* Fix django app (main URL 404s) -* (fixed) Metrics not exported to prometheus -* Provision dashboard found at https://grafana.com/grafana/dashboards/9528-django-prometheus/ -* Make all metrics work in dashboard - ## Welcome Welcome to the Grafana Deploy! This repository lets you set up your grafana instance in a few seconds. diff --git a/django/mysite/mysite/settings.py b/django/mysite/mysite/settings.py index 176b1c7..aab0f65 100644 --- a/django/mysite/mysite/settings.py +++ b/django/mysite/mysite/settings.py @@ -31,6 +31,8 @@ ALLOWED_HOSTS = ["*"] # Application definition INSTALLED_APPS = [ + "polls.apps.PollsConfig", + "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", @@ -38,6 +40,8 @@ INSTALLED_APPS = [ "django.contrib.messages", "django.contrib.staticfiles", + "rest_framework", + "django_prometheus", ] @@ -127,3 +131,11 @@ STATIC_URL = "static/" # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +# DRF + +REST_FRAMEWORK = { + 'DEFAULT_PERMISSION_CLASSES': [ + 'rest_framework.permissions.AllowAny' + ] +} diff --git a/django/mysite/mysite/urls.py b/django/mysite/mysite/urls.py index 2759024..f39aee9 100644 --- a/django/mysite/mysite/urls.py +++ b/django/mysite/mysite/urls.py @@ -16,9 +16,14 @@ Including another URLconf """ from django.contrib import admin from django.urls import path, include +from django.contrib.auth.models import User urlpatterns = [ path("admin/", admin.site.urls), + path("polls/", include("polls.urls")), + + path('api-auth/', include('rest_framework.urls')), + path('', include('django_prometheus.urls')), ] diff --git a/django/mysite/polls/__init__.py b/django/mysite/polls/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django/mysite/polls/admin.py b/django/mysite/polls/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/django/mysite/polls/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/django/mysite/polls/apps.py b/django/mysite/polls/apps.py new file mode 100644 index 0000000..5184937 --- /dev/null +++ b/django/mysite/polls/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class PollsConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "polls" diff --git a/django/mysite/polls/migrations/__init__.py b/django/mysite/polls/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django/mysite/polls/models.py b/django/mysite/polls/models.py new file mode 100644 index 0000000..c88accd --- /dev/null +++ b/django/mysite/polls/models.py @@ -0,0 +1,13 @@ +from django.db import models +from django_prometheus.models import ExportModelOperationsMixin + + +class Question(ExportModelOperationsMixin('question'), models.Model): + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField("date published") + + +class Choice(ExportModelOperationsMixin('choice'), models.Model): + question = models.ForeignKey(Question, on_delete=models.CASCADE) + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) diff --git a/django/mysite/polls/tests.py b/django/mysite/polls/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/django/mysite/polls/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/django/mysite/polls/urls.py b/django/mysite/polls/urls.py new file mode 100644 index 0000000..3357f02 --- /dev/null +++ b/django/mysite/polls/urls.py @@ -0,0 +1,14 @@ +from django.urls import path, include + +from . import views + +from rest_framework import routers + +# Routers provide an easy way of automatically determining the URL conf. +router = routers.DefaultRouter() +router.register(r'question', views.QuestionViewSet) +router.register(r'choice', views.ChoiceViewSet) + +urlpatterns = [ + path('', include(router.urls)), +] diff --git a/django/mysite/polls/views.py b/django/mysite/polls/views.py new file mode 100644 index 0000000..56fe19d --- /dev/null +++ b/django/mysite/polls/views.py @@ -0,0 +1,23 @@ +from django.http import HttpResponse +from rest_framework import serializers, viewsets +from .models import Question, Choice + +# Serializers define the API representation. +class QuestionSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Question + fields = '__all__' + +class ChoiceSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Choice + fields = '__all__' + +# ViewSets define the view behavior. +class QuestionViewSet(viewsets.ModelViewSet): + queryset = Question.objects.all() + serializer_class = QuestionSerializer + +class ChoiceViewSet(viewsets.ModelViewSet): + queryset = Choice.objects.all() + serializer_class = ChoiceSerializer diff --git a/django/requirements.txt b/django/requirements.txt index 5a4563a..108cd6d 100644 --- a/django/requirements.txt +++ b/django/requirements.txt @@ -1,4 +1,11 @@ +# Django django tzdata +# DRF +djangorestframework +markdown +django-filter + +# Metrics django-prometheus diff --git a/docker-compose.yml b/docker-compose.yml index f0ec36d..391302c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -69,11 +69,11 @@ services: container_name: django ports: - "8000:8000" - entrypoint: - - python3 - - /src/mysite/manage.py - - runserver - - 0.0.0.0:8000 + working_dir: /src/mysite + command: > + sh -c "python3 manage.py makemigrations + python3 manage.py migrate + python3 manage.py runserver 0.0.0.0:8000" networks: - monitoring -- GitLab