-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup api_metrics app and celery task to update them
- Loading branch information
Showing
14 changed files
with
177 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.contrib import admin | ||
from api_metrics.models import * | ||
|
||
|
||
@admin.register(Metric) | ||
class Metric(admin.ModelAdmin): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Generated by Django 3.2.25 on 2024-05-31 03:39 | ||
|
||
from django.db import migrations, models | ||
import django.utils.timezone | ||
import django_prometheus.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Metric", | ||
fields=[ | ||
("id", models.AutoField(primary_key=True, serialize=False)), | ||
( | ||
"creation_date", | ||
models.DateTimeField(default=django.utils.timezone.now), | ||
), | ||
("data", models.JSONField(verbose_name="Data")), | ||
( | ||
"name", | ||
models.CharField( | ||
choices=[ | ||
("member_count_total", "Member Count Total"), | ||
("subscription_count_total", "Subscription Count Total"), | ||
], | ||
default=None, | ||
max_length=250, | ||
verbose_name="Metric Name", | ||
), | ||
), | ||
], | ||
bases=( | ||
django_prometheus.models.ExportModelOperationsMixin("metric"), | ||
models.Model, | ||
), | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.db import models | ||
from django.utils import timezone | ||
import pytz | ||
from django_prometheus.models import ExportModelOperationsMixin | ||
|
||
utc = pytz.UTC | ||
|
||
|
||
class Metric(ExportModelOperationsMixin("metric"), models.Model): | ||
"""Stores a single instance of a metric value.""" | ||
|
||
class MetricName(models.TextChoices): | ||
MEMBER_COUNT_TOTAL = "member_count_total", "Member Count Total" | ||
SUBSCRIPTION_COUNT_TOTAL = ( | ||
"subscription_count_total", | ||
"Subscription Count Total", | ||
) | ||
|
||
id = models.AutoField(primary_key=True) | ||
creation_date = models.DateTimeField(default=timezone.now) | ||
data = models.JSONField("Data") | ||
name = models.CharField( | ||
"Metric Name", | ||
max_length=250, | ||
choices=MetricName.choices, | ||
default=None, | ||
) | ||
|
||
def __str__(self): | ||
return f"{self.name} - {self.creation_date}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.urls import path | ||
from rest_framework_simplejwt import views as jwt_views | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path( | ||
"api/statistics/", | ||
views.Statistics.as_view(), | ||
name="api_statistics", | ||
), | ||
path( | ||
"api/update-prom-metrics/", | ||
views.UpdatePromMetrics.as_view(), | ||
name="api_update_prom_metrics", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import logging | ||
import json | ||
|
||
from rest_framework import permissions | ||
|
||
import api_metrics.metrics | ||
from api_metrics.models import Metric | ||
from api_general.models import SiteSession | ||
|
||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
logger = logging.getLogger("metrics") | ||
|
||
|
||
class Statistics(APIView): | ||
""" | ||
get: gets site statistics. | ||
""" | ||
|
||
def get(self, request): | ||
members = SiteSession.objects.filter(signout_date=None).order_by("-signin_date") | ||
member_list = [] | ||
|
||
for member in members: | ||
member_list.append(member.user.profile.get_full_name()) | ||
|
||
statistics = {"onSite": {"members": member_list, "count": members.count()}} | ||
|
||
return Response(statistics) | ||
|
||
|
||
class UpdatePromMetrics(APIView): | ||
""" | ||
post: triggers Django to update the Prometheus site metrics from the database. | ||
""" | ||
|
||
permission_classes = (permissions.AllowAny,) | ||
|
||
def post(self, request): | ||
# get the latest distinct metric | ||
metrics = Metric.objects.order_by("creation_date").distinct("metric_name") | ||
|
||
for metric in metrics: | ||
if metric.metric_name in [ | ||
Metric.MetricName.MEMBER_COUNT_TOTAL, | ||
Metric.MetricName.SUBSCRIPTION_COUNT_TOTAL, | ||
]: | ||
prom_metric = getattr(api_metrics.metrics, metric.name) | ||
|
||
if not prom_metric: | ||
logger.error(f"Prometheus metric {metric.name} not found.") | ||
continue | ||
|
||
for state in metric.data: | ||
prom_metric.labels(state=state["state"]).set(state["total"]) | ||
|
||
return Response() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters