fix: equipment evdata add_info_for_list

This commit is contained in:
caoqianming 2024-04-11 16:32:52 +08:00
parent 0061285355
commit d191a18f7b
1 changed files with 48 additions and 67 deletions

View File

@ -27,16 +27,11 @@ class EcateViewSet(CustomModelViewSet):
设备分类
"""
queryset = Ecate.objects.all()
serializer_class = EcateSerializer
ordering = ['id', 'type', 'code', 'create_time']
filterset_fields = {
"type": ['exact', 'in'],
"code": ['exact', 'in', 'contains'],
"is_for_safe": ['exact'],
"is_for_enp": ['exact'],
"is_car": ['exact']
}
ordering = ["id", "type", "code", "create_time"]
filterset_fields = {"type": ["exact", "in"], "code": ["exact", "in", "contains"], "is_for_safe": ["exact"], "is_for_enp": ["exact"], "is_car": ["exact"]}
class EquipmentViewSet(CustomModelViewSet):
@ -45,10 +40,11 @@ class EquipmentViewSet(CustomModelViewSet):
设备列表
"""
queryset = Equipment.objects.all()
serializer_class = EquipmentSerializer
select_related_fields = ['create_by', 'belong_dept', 'keeper', 'mgroup']
search_fields = ['number', 'name']
select_related_fields = ["create_by", "belong_dept", "keeper", "mgroup"]
search_fields = ["number", "name"]
filterset_class = EquipFilterSet
# def filter_queryset(self, queryset):
@ -56,51 +52,45 @@ class EquipmentViewSet(CustomModelViewSet):
# raise ParseError('请指定设备类型')
# return super().filter_queryset(queryset)
@action(methods=['post'], detail=False, perms_map={'post': 'equipment.create'}, serializer_class=Serializer)
@action(methods=["post"], detail=False, perms_map={"post": "equipment.create"}, serializer_class=Serializer)
@transaction.atomic
def daoru(self, request, *args, **kwargs):
"""导入
导入
"""
daoru_equipment(settings.BASE_DIR + request.data.get('path', ''))
daoru_equipment(settings.BASE_DIR + request.data.get("path", ""))
return Response()
@swagger_auto_schema(manual_parameters=[
openapi.Parameter(name="has_envdata", in_=openapi.IN_QUERY, description="Include envdata in the response",
type=openapi.TYPE_STRING, enum=["yes", "no"], required=False),
openapi.Parameter(name="query", in_=openapi.IN_QUERY, description="定制返回数据",
type=openapi.TYPE_STRING, required=False),
])
@swagger_auto_schema(
manual_parameters=[
openapi.Parameter(name="has_envdata", in_=openapi.IN_QUERY, description="Include envdata in the response", type=openapi.TYPE_STRING, enum=["yes", "no"], required=False),
openapi.Parameter(name="query", in_=openapi.IN_QUERY, description="定制返回数据", type=openapi.TYPE_STRING, required=False),
]
)
def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)
def add_info_for_list(self, data):
if self.request.query_params.get('has_envdata', 'no') == 'yes':
if self.request.query_params.get("has_envdata", "no") == "yes":
now = timezone.localtime()
now_10_before = now - timezone.timedelta(minutes=10)
data_ids = [item['id'] for item in data]
data_ids = [item["id"] for item in data]
from apps.enp.models import EnvData
from apps.enp.serializers import EnvDataSerializer
# 子查询获取每个 equipment_id 对应的最大时间戳
last_time_subquery = EnvData.objects.filter(
equipment_id=OuterRef('equipment_id'),
time__gte=now_10_before,
time__lte=now
).order_by('-time').values('time')[:1]
last_time_subquery = EnvData.objects.filter(equipment_id=OuterRef("equipment_id"), time__gte=now_10_before, time__lte=now).order_by("-time").values("time")[:1]
# 主查询,获取每个 equipment_id 对应的完整记录
last_envdata_qs = EnvData.objects.filter(
equipment_id__in=data_ids,
time=Subquery(last_time_subquery)
)
last_envdata_qs = EnvData.objects.filter(equipment_id__in=data_ids, timex=Subquery(last_time_subquery))
envdata = EnvDataSerializer(last_envdata_qs, many=True).data
envdata_dict = {item['equipment']: item for item in envdata}
envdata_dict = {item["equipment"]: item for item in envdata}
for item in data:
item['envdata'] = envdata_dict.get(item['id'], {})
item["envdata"] = envdata_dict.get(item["id"], {})
return data
@action(methods=['get'], detail=False, perms_map={'get': '*'})
@action(methods=["get"], detail=False, perms_map={"get": "*"})
def count_running_state(self, request, *args, **kwargs):
"""当前运行状态统计
@ -108,31 +98,24 @@ class EquipmentViewSet(CustomModelViewSet):
"""
queryset = self.filter_queryset(self.get_queryset())
result = queryset.aggregate(
count=Count('id'),
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()))
count=Count("id"),
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': result['count'],
'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']
"count": result["count"],
"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)
@ -144,31 +127,28 @@ class EcheckRecordViewSet(ListModelMixin, CreateModelMixin, DestroyModelMixin, C
校准/检定记录
"""
perms_map = {'get': '*', 'post': 'echeckrecord.create',
'put': 'echeckrecord.update', 'delete': 'echeckrecord.delete'}
perms_map = {"get": "*", "post": "echeckrecord.create", "put": "echeckrecord.update", "delete": "echeckrecord.delete"}
queryset = EcheckRecord.objects.all()
serializer_class = EcheckRecordSerializer
select_related_fields = ['equipment', 'create_by']
filterset_fields = ['equipment', 'result']
select_related_fields = ["equipment", "create_by"]
filterset_fields = ["equipment", "result"]
def perform_create(self, serializer):
instance = serializer.save()
equipment = instance.equipment
if equipment.cycle:
equipment.check_date = instance.check_date
equipment.next_check_date = instance.check_date + \
relativedelta(months=equipment.cycle)
equipment.next_check_date = instance.check_date + relativedelta(months=equipment.cycle)
equipment.save()
def perform_destroy(self, instance):
instance.delete()
equipment = instance.equipment
er = EcheckRecord.objects.filter(
equipment=equipment).order_by('check_date').last()
er = EcheckRecord.objects.filter(equipment=equipment).order_by("check_date").last()
if er:
equipment.check_date = instance.check_date
equipment.next_check_date = instance.check_date + \
relativedelta(months=equipment.cycle)
equipment.next_check_date = instance.check_date + relativedelta(months=equipment.cycle)
equipment.save()
else:
equipment.check_date = None
@ -182,7 +162,8 @@ class EInspectViewSet(CustomModelViewSet):
巡检记录
"""
queryset = EInspect.objects.all()
serializer_class = EInspectSerializer
select_related_fields = ['equipment', 'inspect_user']
filterset_fields = ['equipment']
select_related_fields = ["equipment", "inspect_user"]
filterset_fields = ["equipment"]