feat: hour计数改为sec计数 步骤2

This commit is contained in:
caoqianming 2024-05-15 10:31:17 +08:00
parent 8753994468
commit b4b3d6eadc
7 changed files with 24 additions and 39 deletions

View File

@ -593,6 +593,6 @@ class TestViewSet(CustomGenericViewSet):
@action(methods=['post'], detail=False, serializer_class=Serializer, permission_classes=[])
def test_cal(self, request, pk=None):
from apps.wpm.tasks import cal_exp_duration_hour
cal_exp_duration_hour('3397169058570170368')
from apps.wpm.tasks import cal_exp_duration_sec
cal_exp_duration_sec('3397169058570170368')
return Response()

View File

@ -123,6 +123,8 @@ class EnStatSerializer(CustomModelSerializer):
def to_representation(self, instance):
ret = super().to_representation(instance)
if 'run_sec' in ret:
ret['run_hour'] = ret['run_sec'] / 3600
my_dic_keys = list(ret.keys())
for key in my_dic_keys:
ret_one_val = ret[key]

View File

@ -23,7 +23,7 @@ from apps.third.king.k import kingClient
from apps.third.king.king_api import kapis
from apps.enm.services import insert_mplogx_from_king_rest_chunk, MpointCache
from django.utils.timezone import localtime
from apps.wpm.tasks import get_total_hour_now, cal_exp_duration_hour
from apps.wpm.tasks import get_total_sec_now, cal_exp_duration_sec
myLogger = logging.getLogger("log")
@ -241,8 +241,8 @@ def cal_mpointstats(is_now=1, year=None, month=None, day=None, hour=None, m_code
cal_mpointstat_hour(item.id, year, month, day, hour)
# 先调整一下班时间,以防计算错误
get_total_hour_now() # 先处理total_hour_now
cal_exp_duration_hour() # 再处理shut_hour
get_total_sec_now() # 先处理total_hour_now
cal_exp_duration_sec() # 再处理shut_hour
# 开始计算enstat
mgroups = Mgroup.objects.filter(need_enm=True).order_by("sort")
@ -527,7 +527,7 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
enstat.save()
# 运转时长相关
if type != "hour_s" and "run_hour" in this_cal_attrs:
enstat.total_sec_now, enstat.shut_sec = get_total_hour_now_and_shut_hour(enstat)
enstat.total_sec_now, enstat.shut_sec = get_total_sec_now_and_shut_sec(enstat)
enstat.run_sec = enstat.total_sec_now - enstat.shut_sec
try:
enstat.run_rate = (enstat.run_sec / enstat.total_sec_now) * 100
@ -542,7 +542,7 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
return year_s, month_s, day_s # 返回这个值主要为了全厂数据计算而用
def get_total_hour_now_and_shut_hour(enstat: EnStat):
def get_total_sec_now_and_shut_sec(enstat: EnStat):
from apps.wpm.models import SfLog
# if enstat.type == 'hour_s':

View File

@ -7,7 +7,7 @@ from apps.utils.tools import ranstr
from datetime import datetime
from apps.wpm.models import StLog
from apps.wpm.services import get_sflog
from apps.wpm.tasks import cal_exp_duration_hour
from apps.wpm.tasks import cal_exp_duration_sec
def get_mgroup_goals(mgroupId, year, reload=False):
@ -93,9 +93,8 @@ def mgroup_run_change(mgroup: Mgroup, new_run: bool, last_timex: datetime):
if last_stlog.end_time is None and new_run: # 从停到开
last_stlog.end_time = last_timex
last_stlog.duration_sec = (last_stlog.end_time - last_stlog.start_time).total_seconds()
last_stlog.duration = last_stlog.duration_sec / 3600
last_stlog.save()
cal_exp_duration_hour(last_stlog.id) # 触发时间分配
cal_exp_duration_sec(last_stlog.id) # 触发时间分配
elif last_stlog.end_time and new_run is False and last_timex > last_stlog.end_time: # 从开到停
StLog.objects.create(title="停机", is_shutdown=True, mgroup=mgroup, end_time=None, start_time=last_timex, sflog=get_sflog(mgroup, last_timex))
elif new_run is False:

View File

@ -8,7 +8,7 @@ from .models import SfLog, StLog, SfLogExp, WMaterial, Mlog, Handover, Mlogb, At
from apps.system.models import Dept, User
from apps.system.serializers import UserSimpleSerializer
from apps.pm.models import Mtask
from apps.wpm.tasks import cal_enstat_when_pcoal_heat_change, cal_enstat_when_team_change, cal_exp_duration_hour
from apps.wpm.tasks import cal_enstat_when_pcoal_heat_change, cal_enstat_when_team_change, cal_exp_duration_sec
from apps.wpm.services import get_sflog
from apps.mtm.models import Mgroup, TeamMember, Shift, Material
from apps.mtm.serializers import MaterialSimpleSerializer
@ -63,7 +63,7 @@ class StLogSerializer(CustomModelSerializer):
current_note = validated_data.pop('current_note', '')
instance = super().update(instance, validated_data)
if end_time: # 需要把涉及到的sflog都拉入
cal_exp_duration_hour(instance.id)
cal_exp_duration_sec(instance.id)
try:
sflogexp = SfLogExp.objects.get(
stlog=instance.stlog, sflog=current_sflog)
@ -83,8 +83,8 @@ class StLogSerializer(CustomModelSerializer):
if end_time > now:
raise ParseError('开始时间应为历史时间')
if end_time > start_time:
attrs['duration'] = (
end_time - start_time).total_seconds() / 3600
attrs['duration_sec'] = (
end_time - start_time).total_seconds()
else:
raise ParseError('结束时间应大于开始时间')
return super().validate(attrs)

View File

@ -44,15 +44,12 @@ def make_sflogs(mgroup: Mgroup, start_date: datetime.date, end_date: datetime.da
else:
start_time -= datetime.timedelta(days=1)
total_sec = (end_time - start_time).total_seconds()
total_hour = total_sec / 3600
SfLog.objects.get_or_create(mgroup=mgroup, shift=shift, start_time=start_time, defaults={
"mgroup": mgroup,
"shift": shift,
"work_date": current_date,
"start_time": start_time,
"end_time": end_time,
"total_hour_now": total_hour,
"total_hour": total_hour,
"total_sec_now": total_sec,
"total_sec": total_sec,
"create_by": create_by

View File

@ -32,38 +32,33 @@ def make_sflogs_simple(days, state_date: str, end_date: str):
@shared_task(base=CustomTask)
def get_total_hour_now(sflogId: str=''):
def get_total_sec_now(sflogId: str=''):
"""
获取当前总时长, 当传入的是一个sflog时, 返回其total_hour_now
否则更新所有total_hour_now
获取当前总时长, 当传入的是一个sflog时, 返回其total_sec_now
否则更新所有total_sec_now
"""
now = timezone.now()
if sflogId:
sflog = SfLog.objects.get(id=sflogId)
if sflog.end_time <= now:
sflog.total_hour_now = sflog.total_hour
sflog.total_sec_now = sflog.total_sec
else:
total_sec_now = (now-sflog.start_time).total_seconds()
sflog.total_sec_now = total_sec_now if total_sec_now > 0 else 0
sflog.total_hour_now = total_sec_now/3600 if total_sec_now > 0 else 0
sflog.save()
return sflog.total_hour_now
return sflog.total_sec_now
else:
SfLog.objects.filter(end_time__lte=now).exclude(
total_hour_now=F('total_hour')).update(total_hour_now=F('total_hour'))
SfLog.objects.filter(end_time__lte=now).exclude(
total_sec_now=F('total_sec')).update(total_sec_now=F('total_sec'))
sf_qs = SfLog.objects.filter(end_time__gt=now)
for i in sf_qs:
total_sec_now = (now-i.start_time).total_seconds()
i.total_sec_now = total_sec_now if total_sec_now > 0 else 0
i.total_hour_now = total_sec_now/3600 if total_sec_now > 0 else 0
i.save()
@shared_task(base=CustomTask)
def cal_exp_duration_hour(stlogId: str='', all=False):
def cal_exp_duration_sec(stlogId: str='', all=False):
"""
计算异常记录对应的每班持续时间
"""
@ -97,26 +92,18 @@ def cal_exp_duration_hour(stlogId: str='', all=False):
sf_end, sf_start = sflog.end_time, sflog.start_time
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
duration_item_sec = 0
else:
duration_item_sec = total_seconds
duration_item = total_seconds/3600
sflogexp.duration = duration_item
sflogexp.duration_sec = duration_item_sec
sflogexp.duration_sec = total_seconds if total_seconds > 0 else 0
sflogexp.save()
if is_shutdown_stlog:
# 计算每班的总停机时间
ret = SfLogExp.objects.filter(
sflog=sflog, stlog__is_shutdown=True).aggregate(sum=Sum('duration'), sum1=Sum('duration_sec'))
sflog=sflog, stlog__is_shutdown=True).aggregate(sum=Sum('duration_sec'))
if ret.get('sum', 0):
sflog.shut_hour = ret['sum']
sflog.shut_sec = ret['sum1']
sflog.shut_sec = ret['sum']
sflog.save()
# 更新sflog总时长
if sflog.end_time > now:
get_total_hour_now(sflog.id)
get_total_sec_now(sflog.id)
# if stlogId:
# cal_enstat('sflog', sflog.id, sflog.mgroup.id, None, None, None,
# None, None, None, None, cascade=True, cal_attrs=['run_hour'])