fix: task bug 修字段名

This commit is contained in:
zty 2024-12-02 15:36:36 +08:00
parent c0169d25ac
commit a08e66a06a
1 changed files with 92 additions and 3 deletions

View File

@ -121,6 +121,96 @@ def cal_mpointstats_duration(start_time: str, end_time: str, m_code_list=[], cal
current_time += datetime.timedelta(hours=1) current_time += datetime.timedelta(hours=1)
def update_mpoint_val(month, hour):
"""
high:用电高峰时段8小时800--1100 1900--2400 更新val_level high
"""
from datetime import time
from django.db.models import Q
if hour in [8, 9, 10, 19, 20, 21, 22, 23]:
high_time_ranges = [
(time(8, 0), time(11, 0)), # 上午高峰
(time(19, 0), time(23, 59)), # 晚上高峰
]
# 构建查询条件
time_conditions = Q()
for start_time, end_time in high_time_ranges:
time_conditions |= Q(hour__gte=start_time, hour__lt=end_time)
MpointStat.objects.filter(
type="hour",
mpoint__material__name='动力电'
).filter(time_conditions).update(val_level="high")
"""
flat:用电平谷时段8小时 1100--1300 1700--1900 更新val_level flat
"""
if hour in [11, 12, 17, 18, 0, 1, 2, 3]:
flat_time_ranges = [
(time(11, 0), time(13, 0)), # 上午平谷
(time(17, 0), time(19, 0)),
(time(0, 0), time(4, 0)), # 晚上平谷
]
# 构建查询条件
time_conditions = Q()
for start_time, end_time in flat_time_ranges:
time_conditions |= Q(hour__gte=start_time, hour__lt=end_time)
MpointStat.objects.filter(
type="hour", mpoint__material__name='动力电').filter(time_conditions).update(val_level="flat")
"""
low:用电低谷时段8小时 400--800, 13:00--1700 更新val_level low
"""
if hour in [4, 5, 6, 7, 13, 14, 15, 16]:
low_time_ranges = [
(time(4, 0), time(8, 0)), # 上午低谷
(time(13, 0), time(17, 0)), # 晚上低谷
]
# 构建查询条件
time_conditions = Q()
for start_time, end_time in low_time_ranges:
time_conditions |= Q(hour__gte=start_time, hour__lt=end_time)
MpointStat.objects.filter(
type="hour", mpoint__material__name='动力电').filter(time_conditions).update(val_level="low")
"""
low:用电深谷时段2小时 5678月份 1400--1600 更新val_level deep
"""
if month in [5, 6, 7, 8] and hour in [14, 15]:
deep_months = [5, 6, 7, 8]
deep_time_ranges = [
(time(14, 0), time(16, 0)), # 1400--1600
]
# 构建查询条件
time_conditions = Q()
for start_time, end_time in deep_time_ranges:
time_conditions |= Q(hour__gte=start_time, hour__lt=end_time)
MpointStat.objects.filter(
type="hour",
mpoint__material__name='动力电',
month__in=deep_months,
).filter(time_conditions).update(val_level="deep")
"""
peak:用电高峰 11112月份 1900-2100 -- 7 月份 2100-2300 更新val_level peak
"""
if (month in [1, 11, 12] and hour in [19, 20, 21]) or (month == 7 and hour in [21, 22]):
peak_months = {(1, 11, 12): (time(19, 0), time(21, 0)),
(7,): (time(21, 0), time(23, 0)),}
for months, time_range in peak_months.items():
start_time, end_time = time_range
MpointStat.objects.filter(
type="hour",
month__in=months,
mpoint__material__name='动力电',
hour__gte = start_time,
hour__lt = end_time,
).update(val_level="peak")
@shared_task(base=CustomTask) @shared_task(base=CustomTask)
def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: int, cascade=True, sflog_hours=[]): def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: int, cascade=True, sflog_hours=[]):
""" """
@ -165,6 +255,7 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
return return
ms, _ = MpointStat.objects.get_or_create(**params, defaults=params) ms, _ = MpointStat.objects.get_or_create(**params, defaults=params)
ms.val = ms.val_correct if ms.val_correct is not None else val ms.val = ms.val_correct if ms.val_correct is not None else val
# ms.val_level
ms.save() ms.save()
# 更新更高级别的值 # 更新更高级别的值
@ -898,9 +989,7 @@ def enm_alarm(year_s: int, month_s: int, day_s: int):
"day_s": day_s, "day_s": day_s,
"val": real_val, "val": real_val,
"goal_val": goal_val, "goal_val": goal_val,
"enstat": enstat.id, "enstat": enstat.id
"team_name": enstat.team.name if enstat.team else "",
"shift_name": enstat.sflog.shift.name if enstat.sflog.shift.name else ""
} }
event.save() event.save()
Eventdo.objects.get_or_create(cate=event_cate, event=event, defaults={"cate": event_cate, "event": event}) Eventdo.objects.get_or_create(cate=event_cate, event=event, defaults={"cate": event_cate, "event": event})