From 9a44088d2bf12ea2c30d030f0d59c57d7ca4dfe4 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 16 May 2021 17:49:37 +0200 Subject: [PATCH] admin: migrate metrics viewset to APIView Signed-off-by: Jens Langhammer --- authentik/admin/api/metrics.py | 6 +++--- authentik/api/v2/urls.py | 2 +- schema.yml | 6 ++---- web/src/api/Config.ts | 2 +- web/src/api/Users.ts | 7 ++++++- web/src/elements/buttons/TokenCopyButton.ts | 2 +- web/src/elements/charts/AdminLoginsChart.ts | 2 +- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/authentik/admin/api/metrics.py b/authentik/admin/api/metrics.py index 3bfd2dfdb..88b5b007c 100644 --- a/authentik/admin/api/metrics.py +++ b/authentik/admin/api/metrics.py @@ -12,7 +12,7 @@ from rest_framework.fields import IntegerField, SerializerMethodField from rest_framework.permissions import IsAdminUser from rest_framework.request import Request from rest_framework.response import Response -from rest_framework.viewsets import ViewSet +from rest_framework.views import APIView from authentik.core.api.utils import PassiveSerializer from authentik.events.models import Event, EventAction @@ -69,13 +69,13 @@ class LoginMetricsSerializer(PassiveSerializer): return get_events_per_1h(action=EventAction.LOGIN_FAILED) -class AdministrationMetricsViewSet(ViewSet): +class AdministrationMetricsViewSet(APIView): """Login Metrics per 1h""" permission_classes = [IsAdminUser] @extend_schema(responses={200: LoginMetricsSerializer(many=False)}) - def list(self, request: Request) -> Response: + def get(self, request: Request) -> Response: """Login Metrics per 1h""" serializer = LoginMetricsSerializer(True) return Response(serializer.data) diff --git a/authentik/api/v2/urls.py b/authentik/api/v2/urls.py index e63baaf85..d141d3f3d 100644 --- a/authentik/api/v2/urls.py +++ b/authentik/api/v2/urls.py @@ -100,7 +100,6 @@ router = routers.DefaultRouter() router.register("admin/version", VersionViewSet, basename="admin_version") router.register("admin/workers", WorkerViewSet, basename="admin_workers") -router.register("admin/metrics", AdministrationMetricsViewSet, basename="admin_metrics") router.register("admin/system_tasks", TaskViewSet, basename="admin_system_tasks") router.register("admin/apps", AppsViewSet, basename="apps") @@ -198,6 +197,7 @@ urlpatterns = ( ] + router.urls + [ + path("admin/metrics/", AdministrationMetricsViewSet.as_view(), name="admin_metrics"), path("root/config/", ConfigView.as_view(), name="config"), path( "flows/executor//", diff --git a/schema.yml b/schema.yml index 3a89297ca..1dff89e7f 100644 --- a/schema.yml +++ b/schema.yml @@ -29,7 +29,7 @@ paths: description: '' /api/v2beta/admin/metrics/: get: - operationId: admin_metrics_list + operationId: admin_metrics_retrieve description: Login Metrics per 1h tags: - admin @@ -41,9 +41,7 @@ paths: content: application/json: schema: - type: array - items: - $ref: '#/components/schemas/LoginMetrics' + $ref: '#/components/schemas/LoginMetrics' description: '' /api/v2beta/admin/system_tasks/: get: diff --git a/web/src/api/Config.ts b/web/src/api/Config.ts index 488e758bf..bf188659b 100644 --- a/web/src/api/Config.ts +++ b/web/src/api/Config.ts @@ -15,7 +15,7 @@ export class LoggingMiddleware implements Middleware { let globalConfigPromise: Promise; export function config(): Promise { if (!globalConfigPromise) { - globalConfigPromise = new RootApi(DEFAULT_CONFIG).rootConfigList(); + globalConfigPromise = new RootApi(DEFAULT_CONFIG).rootConfigRetrieve(); } return globalConfigPromise; } diff --git a/web/src/api/Users.ts b/web/src/api/Users.ts index a0201c7cc..5c74d035c 100644 --- a/web/src/api/Users.ts +++ b/web/src/api/Users.ts @@ -4,9 +4,14 @@ import { DEFAULT_CONFIG } from "./Config"; let globalMePromise: Promise; export function me(): Promise { if (!globalMePromise) { - globalMePromise = new CoreApi(DEFAULT_CONFIG).coreUsersMe().catch((ex) => { + globalMePromise = new CoreApi(DEFAULT_CONFIG).coreUsersMeRetrieve().catch((ex) => { const defaultUser: SessionUser = { user: { + pk: -1, + isSuperuser: false, + groups: [], + avatar: "", + uid: "", username: "", name: "" } diff --git a/web/src/elements/buttons/TokenCopyButton.ts b/web/src/elements/buttons/TokenCopyButton.ts index b04c327d8..9def97892 100644 --- a/web/src/elements/buttons/TokenCopyButton.ts +++ b/web/src/elements/buttons/TokenCopyButton.ts @@ -17,7 +17,7 @@ export class TokenCopyButton extends ActionButton { if (!this.identifier) { return Promise.reject(); } - return new CoreApi(DEFAULT_CONFIG).coreTokensViewKey({ + return new CoreApi(DEFAULT_CONFIG).coreTokensViewKeyRetrieve({ identifier: this.identifier }).then((token) => { if (!token.key) { diff --git a/web/src/elements/charts/AdminLoginsChart.ts b/web/src/elements/charts/AdminLoginsChart.ts index 6a89f6645..316cfc012 100644 --- a/web/src/elements/charts/AdminLoginsChart.ts +++ b/web/src/elements/charts/AdminLoginsChart.ts @@ -8,7 +8,7 @@ import { DEFAULT_CONFIG } from "../../api/Config"; export class AdminLoginsChart extends AKChart { apiRequest(): Promise { - return new AdminApi(DEFAULT_CONFIG).adminMetricsList(); + return new AdminApi(DEFAULT_CONFIG).adminMetricsRetrieve(); } getChartData(data: LoginMetrics): ChartData {