fix: get_total_sec_now及cal_exp_duration_sec 添加now参数
This commit is contained in:
parent
313e9ed1cd
commit
dc7e6f9d03
|
@ -4,7 +4,7 @@ from apps.utils.tasks import CustomTask
|
|||
from celery import shared_task
|
||||
from apps.enm.models import MpLogx, Mpoint, MpointStat, EnStat, EnStat2
|
||||
from apps.wpm.models import SfLog
|
||||
import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from django.db.models import Sum, Avg
|
||||
from dateutil import tz
|
||||
from django.conf import settings
|
||||
|
@ -33,8 +33,8 @@ myLogger = logging.getLogger("log")
|
|||
|
||||
|
||||
def get_current_and_previous_time():
|
||||
now = datetime.datetime.now()
|
||||
pre = now - datetime.timedelta(hours=1)
|
||||
now = datetime.now()
|
||||
pre = now - timedelta(hours=1)
|
||||
return now, pre
|
||||
|
||||
|
||||
|
@ -84,15 +84,15 @@ def cal_mpointstats_duration(start_time: str, end_time: str, m_code_list=[], cal
|
|||
重跑某一段时间的任务
|
||||
"""
|
||||
mytz = tz.gettz(settings.TIME_ZONE)
|
||||
start_time = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
|
||||
start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
|
||||
start_time.replace(tzinfo=mytz)
|
||||
end_time = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
|
||||
end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
|
||||
start_time.replace(tzinfo=mytz)
|
||||
current_time = start_time
|
||||
while current_time <= end_time:
|
||||
year, month, day, hour = current_time.year, current_time.month, current_time.day, current_time.hour
|
||||
cal_mpointstats(0, year, month, day, hour, m_code_list, cal_attrs=[])
|
||||
current_time += datetime.timedelta(hours=1)
|
||||
current_time += timedelta(hours=1)
|
||||
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
|
@ -102,9 +102,9 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
|
|||
"""
|
||||
mpoint = Mpoint.objects.get(id=mpointId)
|
||||
mytz = tz.gettz(settings.TIME_ZONE)
|
||||
dt = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=0, second=0, tzinfo=mytz) # 整点时间
|
||||
dt_hour_p= dt - datetime.timedelta(hours=1) # 上个整点
|
||||
dt_hour_n= dt + datetime.timedelta(hours=1) # 下个整点
|
||||
dt = datetime(year=year, month=month, day=day, hour=hour, minute=0, second=0, tzinfo=mytz) # 整点时间
|
||||
dt_hour_p= dt - timedelta(hours=1) # 上个整点
|
||||
dt_hour_n= dt + timedelta(hours=1) # 下个整点
|
||||
if mpoint.material and mpoint.val_type in ['float', 'int']: # 如果计量的是物料 # 累计量 有的会清零,需要额外处理(还未做)
|
||||
params = {"mpoint": mpoint, "type": "hour"}
|
||||
params["year"], params["month"], params["day"], params["hour"] = year, month, day, hour
|
||||
|
@ -254,8 +254,11 @@ 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_sec_now() # 先处理total_sec
|
||||
cal_exp_duration_sec() # 再处理shut_sec
|
||||
# 为了保持一致使用统计的时间点
|
||||
nowx = datetime.now()
|
||||
get_total_sec_now(now=nowx) # 再处理total_sec
|
||||
cal_exp_duration_sec(now=nowx) # 先处理shut_sec
|
||||
|
||||
|
||||
# 开始计算enstat
|
||||
mgroups = Mgroup.objects.filter(need_enm=True).order_by("sort")
|
||||
|
@ -360,7 +363,7 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
|
|||
sflog = SfLog.objects.get(id=sflogId)
|
||||
elif year and month and day and hour is not None:
|
||||
mytz = tz.gettz(settings.TIME_ZONE)
|
||||
dt = datetime.datetime(year=year, month=month, day=day, hour=hour, tzinfo=mytz)
|
||||
dt = datetime(year=year, month=month, day=day, hour=hour, tzinfo=mytz)
|
||||
sflog = get_sflog(mgroup, dt)
|
||||
team = None
|
||||
if sflog:
|
||||
|
@ -594,8 +597,8 @@ def get_total_sec_now_and_shut_sec(enstat: EnStat):
|
|||
|
||||
# if enstat.type == 'hour_s':
|
||||
# # 找到停机记录,并划分到该小时
|
||||
# end_time = datetime.datetime(enstat.year, enstat.month, enstat.day, enstat.hour)
|
||||
# start_time = end_time - datetime.timedelta(hours=1)
|
||||
# end_time = datetime(enstat.year, enstat.month, enstat.day, enstat.hour)
|
||||
# start_time = end_time - timedelta(hours=1)
|
||||
# sts = StLog.objects.filter(mgroup=enstat.mgroup)
|
||||
# sts = (sts.filter(start_time__gt=start_time, start_time__lt=end_time)|sts.filter(start_time__lt=start_time, end_time=None)|sts.filter(end_time__gt=start_time, end_time__lt=end_time)).distinct()
|
||||
# shut_hour = 0
|
||||
|
|
|
@ -3,7 +3,7 @@ from __future__ import absolute_import, unicode_literals
|
|||
from apps.utils.tasks import CustomTask
|
||||
from celery import shared_task
|
||||
from apps.mtm.models import Mgroup
|
||||
import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from django.db.models import Sum
|
||||
from apps.wpm.services import make_sflogs
|
||||
from apps.wpm.models import SfLog, StLog, SfLogExp
|
||||
|
@ -18,22 +18,23 @@ def make_sflogs_simple(days, state_date: str, end_date: str):
|
|||
根据班次规则生成今明两天的排班记录
|
||||
"""
|
||||
if days:
|
||||
start_date = datetime.datetime.today()
|
||||
end_date = start_date + datetime.timedelta(days=days)
|
||||
start_date = datetime.today()
|
||||
end_date = start_date + timedelta(days=days)
|
||||
else:
|
||||
start_date = datetime.datetime.strptime(state_date, "%Y-%m-%d").date()
|
||||
end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d").date()
|
||||
start_date = datetime.strptime(state_date, "%Y-%m-%d").date()
|
||||
end_date = datetime.strptime(end_date, "%Y-%m-%d").date()
|
||||
mgroups = Mgroup.objects.filter(need_enm=True, cate='section').all()
|
||||
for mgroup in mgroups:
|
||||
make_sflogs(mgroup, start_date, end_date)
|
||||
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
def get_total_sec_now(sflogId: str=''):
|
||||
def get_total_sec_now(sflogId: str='', now: datetime=None):
|
||||
"""
|
||||
获取当前总时长, 当传入的是一个sflog时, 返回其total_sec_now
|
||||
否则更新所有total_sec_now
|
||||
"""
|
||||
if now is None:
|
||||
now = timezone.now()
|
||||
if sflogId:
|
||||
sflog = SfLog.objects.get(id=sflogId)
|
||||
|
@ -55,7 +56,7 @@ def get_total_sec_now(sflogId: str=''):
|
|||
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
def cal_exp_duration_sec(stlogId: str='', all=False):
|
||||
def cal_exp_duration_sec(stlogId: str='', all=False, now: datetime=None):
|
||||
"""
|
||||
计算异常记录对应的每班持续时间(只针对工段)
|
||||
"""
|
||||
|
@ -66,6 +67,7 @@ def cal_exp_duration_sec(stlogId: str='', all=False):
|
|||
stlogs = StLog.objects.all()
|
||||
else: # 不传就默认更新未结束的
|
||||
stlogs = StLog.objects.filter(end_time=None)
|
||||
if now is None:
|
||||
now = timezone.now()
|
||||
for stlog in stlogs:
|
||||
is_shutdown_stlog = True
|
||||
|
|
Loading…
Reference in New Issue