diff --git a/apps/em/views.py b/apps/em/views.py index 3985909f..1ae7ab13 100644 --- a/apps/em/views.py +++ b/apps/em/views.py @@ -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): """