From a08e66a06a37737f9bd2cd6168b6b31febc5dcbc Mon Sep 17 00:00:00 2001 From: zty Date: Mon, 2 Dec 2024 15:36:36 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=20task=20bug=20=E4=BF=AE=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/enm/tasks.py | 95 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 3 deletions(-) diff --git a/apps/enm/tasks.py b/apps/enm/tasks.py index 541a1f7a..edd31b8f 100644 --- a/apps/enm/tasks.py +++ b/apps/enm/tasks.py @@ -121,6 +121,96 @@ def cal_mpointstats_duration(start_time: str, end_time: str, m_code_list=[], cal current_time += datetime.timedelta(hours=1) + + +def update_mpoint_val(month, hour): + + """ + high:用电高峰时段8小时:8:00--11:00, 19:00--24:00 更新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小时: 11:00--13:00, 17:00--19:00 更新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小时: 4:00--8:00, 13:00--17:00 更新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小时: 5,6,7,8,月份 14:00--16:00 更新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)), # 14:00--16:00 + ] + # 构建查询条件 + 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:用电高峰 1,11,12月份 19:00-21:00 -- 7 月份 21:00-23:00 更新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) 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 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_level ms.save() # 更新更高级别的值 @@ -898,9 +989,7 @@ def enm_alarm(year_s: int, month_s: int, day_s: int): "day_s": day_s, "val": real_val, "goal_val": goal_val, - "enstat": enstat.id, - "team_name": enstat.team.name if enstat.team else "", - "shift_name": enstat.sflog.shift.name if enstat.sflog.shift.name else "" + "enstat": enstat.id } event.save() Eventdo.objects.get_or_create(cate=event_cate, event=event, defaults={"cate": event_cate, "event": event})