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

View File

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

View File

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