From 36822c128cc426a6416f362ddf9463d5919a1860 Mon Sep 17 00:00:00 2001 From: Jens L Date: Fri, 13 Jan 2023 13:21:49 +0100 Subject: [PATCH] admin: include task duration in API (#4428) include task duration in API Signed-off-by: Jens Langhammer Signed-off-by: Jens Langhammer --- authentik/admin/api/tasks.py | 15 +++++++++++++-- schema.yml | 4 ++++ web/src/admin/system-tasks/SystemTaskListPage.ts | 10 ++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/authentik/admin/api/tasks.py b/authentik/admin/api/tasks.py index c9ae28951..8ded6de49 100644 --- a/authentik/admin/api/tasks.py +++ b/authentik/admin/api/tasks.py @@ -7,7 +7,13 @@ from django.utils.translation import gettext_lazy as _ from drf_spectacular.types import OpenApiTypes from drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schema from rest_framework.decorators import action -from rest_framework.fields import CharField, ChoiceField, DateTimeField, ListField +from rest_framework.fields import ( + CharField, + ChoiceField, + DateTimeField, + ListField, + SerializerMethodField, +) from rest_framework.permissions import IsAdminUser from rest_framework.request import Request from rest_framework.response import Response @@ -26,6 +32,7 @@ class TaskSerializer(PassiveSerializer): task_name = CharField() task_description = CharField() task_finish_timestamp = DateTimeField(source="finish_time") + task_duration = SerializerMethodField() status = ChoiceField( source="result.status.name", @@ -33,7 +40,11 @@ class TaskSerializer(PassiveSerializer): ) messages = ListField(source="result.messages") - def to_representation(self, instance): + def get_task_duration(self, instance: TaskInfo) -> int: + """Get the duration a task took to run""" + return max(instance.finish_timestamp - instance.start_timestamp, 0) + + def to_representation(self, instance: TaskInfo): """When a new version of authentik adds fields to TaskInfo, the API will fail with an AttributeError, as the classes are pickled in cache. In that case, just delete the info""" diff --git a/schema.yml b/schema.yml index 8aba83abe..1cd8d2eb7 100644 --- a/schema.yml +++ b/schema.yml @@ -37157,6 +37157,9 @@ components: task_finish_timestamp: type: string format: date-time + task_duration: + type: integer + readOnly: true status: $ref: '#/components/schemas/TaskStatusEnum' messages: @@ -37166,6 +37169,7 @@ components: - messages - status - task_description + - task_duration - task_finish_timestamp - task_name TaskStatusEnum: diff --git a/web/src/admin/system-tasks/SystemTaskListPage.ts b/web/src/admin/system-tasks/SystemTaskListPage.ts index 3fafd1708..c2f18809a 100644 --- a/web/src/admin/system-tasks/SystemTaskListPage.ts +++ b/web/src/admin/system-tasks/SystemTaskListPage.ts @@ -82,6 +82,16 @@ export class SystemTaskListPage extends TablePage { return html`
+
+
+ ${t`Duration`} +
+
+
+ ${t`${Math.round(item.taskDuration)} seconds`} +
+
+
${t`Messages`}