cleanup api

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Jens Langhammer 2024-01-13 21:00:34 +01:00
parent 1033b114c7
commit 7ed2babd22
No known key found for this signature in database
3 changed files with 83 additions and 44 deletions

View File

@ -4,7 +4,7 @@ from importlib import import_module
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schema
from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework.decorators import action
from rest_framework.fields import (
CharField,
@ -28,30 +28,40 @@ LOGGER = get_logger()
class SystemTaskSerializer(ModelSerializer):
"""Serialize TaskInfo and TaskResult"""
task_name = CharField(source="name")
task_description = CharField(source="description")
task_start_timestamp = DateTimeField(source="start_timestamp")
task_finish_timestamp = DateTimeField(source="finish_timestamp")
task_duration = SerializerMethodField()
name = CharField()
full_name = SerializerMethodField()
uid = CharField(required=False)
description = CharField()
start_timestamp = DateTimeField()
finish_timestamp = DateTimeField()
duration = SerializerMethodField()
status = ChoiceField(
# source="status",
choices=[(x.name, x.name) for x in TaskStatus],
)
messages = ListField(child=CharField())
def get_task_duration(self, instance: SystemTask) -> int:
def get_full_name(self, instance: SystemTask) -> str:
"""Get full name with UID"""
if instance.uid:
return f"{instance.name}:{instance.uid}"
return instance.name
def get_duration(self, instance: SystemTask) -> int:
"""Get the duration a task took to run"""
return max(instance.finish_timestamp.timestamp() - instance.start_timestamp.timestamp(), 0)
class Meta:
model = SystemTask
fields = [
"task_name",
"task_description",
"task_start_timestamp",
"task_finish_timestamp",
"task_duration",
"uuid",
"name",
"full_name",
"uid",
"description",
"start_timestamp",
"finish_timestamp",
"duration",
"status",
"messages",
]
@ -62,6 +72,9 @@ class SystemTaskViewSet(ReadOnlyModelViewSet):
queryset = SystemTask.objects.all()
serializer_class = SystemTaskSerializer
filterset_fields = ["name", "uid", "status"]
ordering = ["name", "uid", "status"]
search_fields = ["name", "description", "uid", "status"]
@permission_required(None, ["authentik_events.rerun_task"])
@extend_schema(
@ -71,14 +84,6 @@ class SystemTaskViewSet(ReadOnlyModelViewSet):
404: OpenApiResponse(description="Task not found"),
500: OpenApiResponse(description="Failed to retry task"),
},
parameters=[
OpenApiParameter(
"id",
type=OpenApiTypes.STR,
location=OpenApiParameter.PATH,
required=True,
),
],
)
@action(detail=True, methods=["post"])
def retry(self, request: Request, pk=None) -> Response:

View File

@ -3237,25 +3237,30 @@
"model_authentik_events.systemtask": {
"type": "object",
"properties": {
"task_name": {
"name": {
"type": "string",
"minLength": 1,
"title": "Task name"
"title": "Name"
},
"task_description": {
"uid": {
"type": "string",
"minLength": 1,
"title": "Task description"
"title": "Uid"
},
"task_start_timestamp": {
"description": {
"type": "string",
"minLength": 1,
"title": "Description"
},
"start_timestamp": {
"type": "string",
"format": "date-time",
"title": "Task start timestamp"
"title": "Start timestamp"
},
"task_finish_timestamp": {
"finish_timestamp": {
"type": "string",
"format": "date-time",
"title": "Task finish timestamp"
"title": "Finish timestamp"
},
"status": {
"type": "string",

View File

@ -6840,6 +6840,10 @@ paths:
operationId: events_system_tasks_list
description: Read-only view set that returns all background tasks
parameters:
- in: query
name: name
schema:
type: string
- name: ordering
required: false
in: query
@ -6864,6 +6868,24 @@ paths:
description: A search term.
schema:
type: string
- in: query
name: status
schema:
type: integer
enum:
- 1
- 2
- 4
- 8
description: |-
* `1` - Successful
* `2` - Warning
* `4` - Error
* `8` - Unknown
- in: query
name: uid
schema:
type: string
tags:
- events
security:
@ -6927,11 +6949,6 @@ paths:
operationId: events_system_tasks_retry_create
description: Retry task
parameters:
- in: path
name: id
schema:
type: string
required: true
- in: path
name: uuid
schema:
@ -42685,17 +42702,27 @@ components:
type: object
description: Serialize TaskInfo and TaskResult
properties:
task_name:
uuid:
type: string
task_description:
format: uuid
readOnly: true
name:
type: string
task_start_timestamp:
full_name:
type: string
description: Get full name with UID
readOnly: true
uid:
type: string
description:
type: string
start_timestamp:
type: string
format: date-time
task_finish_timestamp:
finish_timestamp:
type: string
format: date-time
task_duration:
duration:
type: integer
description: Get the duration a task took to run
readOnly: true
@ -42706,13 +42733,15 @@ components:
items:
type: string
required:
- description
- duration
- finish_timestamp
- full_name
- messages
- name
- start_timestamp
- status
- task_description
- task_duration
- task_finish_timestamp
- task_name
- task_start_timestamp
- uuid
SystemTaskStatusEnum:
enum:
- SUCCESSFUL