feat: 设备运行状态count统计接口

This commit is contained in:
caoqianming 2024-02-26 14:22:43 +08:00
parent 53193650f6
commit b71a8921c3
1 changed files with 40 additions and 4 deletions

View File

@ -12,6 +12,7 @@ from django.db import transaction
from apps.em.services import daoru_equipment
from rest_framework.response import Response
from django.conf import settings
from django.db.models import Count, Case, When, IntegerField
# Create your views here.
@ -46,10 +47,10 @@ class EquipmentViewSet(CustomModelViewSet):
search_fields = ['number', 'name']
filterset_class = EquipFilterSet
def filter_queryset(self, queryset):
if not self.detail and not self.request.query_params.get('type', None):
raise ParseError('请指定设备类型')
return super().filter_queryset(queryset)
# def filter_queryset(self, queryset):
# if not self.detail and not self.request.query_params.get('type', None):
# raise ParseError('请指定设备类型')
# return super().filter_queryset(queryset)
@action(methods=['post'], detail=False, perms_map={'post': 'equipment.create'}, serializer_class=Serializer)
@transaction.atomic
@ -61,6 +62,41 @@ class EquipmentViewSet(CustomModelViewSet):
daoru_equipment(settings.BASE_DIR + request.data.get('path', ''))
return Response()
@action(methods=['get'], detail=False, perms_map={'get': '*'})
def count_running_state(self, request, *args, **kwargs):
"""当前运行状态统计
当前运行状态统计
"""
queryset = self.filter_queryset(self.get_queryset())
result = queryset.aggregate(
count_online=Count(
Case(When(is_online=1, then=1), output_field=IntegerField())),
count_offline=Count(
Case(When(is_online=0, then=1), output_field=IntegerField())),
count_running=Count(
Case(When(running_state=10, then=1), output_field=IntegerField())),
count_standby=Count(
Case(When(running_state=20, then=1), output_field=IntegerField())),
count_stop=Count(
Case(When(running_state=30, then=1), output_field=IntegerField())),
count_fail=Count(
Case(When(running_state=40, then=1), output_field=IntegerField())),
count_unknown=Count(
Case(When(running_state=50, then=1), output_field=IntegerField()))
)
json_result = {
'count_online': result['count_online'],
'count_offline': result['count_offline'],
'count_running': result['count_running'],
'count_standby': result['count_standby'],
'count_stop': result['count_stop'],
'count_fail': result['count_fail'],
'count_unknown': result['count_unknown']
}
return Response(json_result)
class EcheckRecordViewSet(ListModelMixin, CreateModelMixin, DestroyModelMixin, CustomGenericViewSet):
"""