2021-03-26 11:09:34 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2022-02-28 11:28:58 +00:00
|
|
|
import datetime
|
2021-03-26 11:09:34 +00:00
|
|
|
import logging
|
2022-02-28 11:28:58 +00:00
|
|
|
from django.utils import timezone
|
2021-03-26 11:09:34 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from rest_framework import viewsets, generics
|
2022-02-17 12:13:31 +00:00
|
|
|
from rest_framework.views import Response
|
2021-03-26 11:09:34 +00:00
|
|
|
from rest_framework import status
|
|
|
|
|
2022-10-25 04:57:34 +00:00
|
|
|
from terminal.models import Terminal, Status, Session
|
|
|
|
from terminal import serializers
|
|
|
|
from terminal.utils import TypedComponentsStatusMetricsUtil
|
2021-03-26 11:09:34 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__file__)
|
|
|
|
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
__all__ = ['StatusViewSet', 'ComponentsMetricsAPIView']
|
2021-03-26 11:09:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StatusViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = Status.objects.all()
|
2022-11-04 03:40:16 +00:00
|
|
|
serializer_class = serializers.StatSerializer
|
2021-03-26 11:09:34 +00:00
|
|
|
session_serializer_class = serializers.SessionSerializer
|
|
|
|
task_serializer_class = serializers.TaskSerializer
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
self.handle_sessions()
|
|
|
|
self.perform_create(serializer)
|
2022-02-28 11:28:58 +00:00
|
|
|
task_serializer = self.get_task_serializer()
|
|
|
|
return Response(task_serializer.data, status=201)
|
2021-03-26 11:09:34 +00:00
|
|
|
|
|
|
|
def handle_sessions(self):
|
|
|
|
session_ids = self.request.data.get('sessions', [])
|
|
|
|
Session.set_sessions_active(session_ids)
|
|
|
|
|
2022-02-28 11:28:58 +00:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
serializer.validated_data.pop('sessions', None)
|
|
|
|
serializer.validated_data["terminal"] = self.request.user.terminal
|
|
|
|
return super().perform_create(serializer)
|
|
|
|
|
|
|
|
def get_task_serializer(self):
|
|
|
|
critical_time = timezone.now() - datetime.timedelta(minutes=10)
|
|
|
|
tasks = self.request.user.terminal.task_set.filter(is_finished=False, date_created__gte=critical_time)
|
|
|
|
serializer = self.task_serializer_class(tasks, many=True)
|
|
|
|
return serializer
|
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
terminal_id = self.kwargs.get("terminal", None)
|
|
|
|
if terminal_id:
|
|
|
|
terminal = get_object_or_404(Terminal, id=terminal_id)
|
2022-11-04 03:40:16 +00:00
|
|
|
return terminal.status_set.all()
|
2021-03-26 11:09:34 +00:00
|
|
|
return super().get_queryset()
|
|
|
|
|
|
|
|
|
|
|
|
class ComponentsMetricsAPIView(generics.GenericAPIView):
|
|
|
|
""" 返回汇总组件指标数据 """
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'GET': 'terminal.view_terminal'
|
|
|
|
}
|
2021-03-26 11:09:34 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
util = TypedComponentsStatusMetricsUtil()
|
|
|
|
metrics = util.get_metrics()
|
|
|
|
return Response(metrics, status=status.HTTP_200_OK)
|