fix: shutdown_or_startup 优化

This commit is contained in:
caoqianming 2023-09-04 15:36:16 +08:00
parent 1ed7c846a3
commit f719702432
3 changed files with 23 additions and 12 deletions

View File

@ -569,3 +569,9 @@ class TestViewSet(CustomGenericViewSet):
TaskResult.objects.filter(task_name__in=['apps.vm.tasks.close_visit_by_leave_time',
'apps.rpm.tasks.close_rpj_by_leave_time']).delete()
return Response()
@action(methods=['post'], detail=False, serializer_class=Serializer, permission_classes=[])
def test_cal(self, request, pk=None):
from apps.wpm.tasks import cal_shut_hour
cal_shut_hour('3397169058570170368')
return Response()

View File

@ -81,17 +81,22 @@ def shutdown_or_startup(mplog: MpLog):
from apps.wpm.tasks import cal_shut_hour
mpoint = mplog.mpoint
mgroup = mpoint.mgroup
if mplog.tag_val == 1 and mgroup.is_runing is False:
# 从停到开
stlog = StLog.objects.filter(mgroup=mgroup).order_by('start_time').last()
stlog.end_time = mplog.tag_update
stlog.save()
mgroup.is_runing = True
mgroup.save()
cal_shut_hour(stlog.id) # 触发停机时间分配
elif mplog.tag_val == 0 and mgroup.is_runing is True:
# 从开到停
StLog.objects.get_or_create(mgroup=mgroup, end_time=None, defaults={'mgroup': mgroup, 'start_time': mplog.tag_update})
last_stlog = StLog.objects.filter(mgroup=mgroup).order_by('start_time').last()
if last_stlog:
if mplog.tag_update >= last_stlog.start_time: # 认为是有效信号
if last_stlog.end_time is None and mplog.tag_val==1:
last_stlog.end_time = mplog.tag_update
last_stlog.duration = (last_stlog.end_time - last_stlog.start_time).total_seconds()/3600
last_stlog.save()
mgroup.is_runing = True
mgroup.save()
cal_shut_hour(last_stlog.id) # 触发停机时间分配
elif last_stlog.end_time and mplog.tag_val==0 and mplog.tag_update > last_stlog.end_time:
StLog.objects.create(mgroup=mgroup, end_time=None, start_time=mplog.tag_update)
mgroup.is_runing = False
mgroup.save()
else:
StLog.objects.create(mgroup=mgroup, end_time=None, start_time=mplog.tag_update)
mgroup.is_runing = False
mgroup.save()

View File

@ -81,7 +81,7 @@ def cal_shut_hour(stlogId: str):
sflogexp, _ = SfLogExp.objects.get_or_create(stlog=stlog, sflog=sflog, defaults={'stlog': stlog, 'sflog': sflog, 'is_current_down': is_current_down, 'title': '停机'})
# 计算duration
sf_end, sf_start = sflog.end_time, sflog.start_time
duration_item_delta = min(sf_end, st_end) - min(sf_start, st_start)
duration_item_delta = min(sf_end, st_end) - max(sf_start, st_start)
total_seconds = duration_item_delta.total_seconds()
if total_seconds < 0:
duration_item = 0