diff --git a/apps/enm/services.py b/apps/enm/services.py index d91e3134..ebf74492 100644 --- a/apps/enm/services.py +++ b/apps/enm/services.py @@ -482,4 +482,24 @@ def get_analyse_data_mgroups_duration(start_date: datetime, end_date: datetime) return res def get_safe_value(value, default=1): - return value if value != 0 else default \ No newline at end of file + return value if value != 0 else default + + +def get_elec_level(month, hour): + """ + peak:用电尖峰 1,11,12月份 19:00-21:00 -- 7 月份 21:00-23:00 更新val_level 为 peak + high:用电高峰时段8小时:8:00--11:00, 19:00--24:00 更新val_level 为 high + flat:用电平谷时段8小时: 11:00--13:00, 17:00--19:00 更新val_level 为 flat + low:用电低谷时段8小时: 4:00--8:00, 13:00--17:00 更新val_level 为 low + """ + if (month in [1, 11, 12] and hour in [19, 20, 21]) or (month == 7 and hour in [21, 22]): + return 'peak' + elif month in [5, 6, 7, 8] and hour in [14, 15]: + return 'deep' + elif hour in [8, 9, 10, 19, 20, 21, 22, 23]: + return 'high' + elif hour in [4, 5, 6, 7, 13, 14, 15, 16]: + return 'low' + elif hour in [11, 12, 17, 18, 0, 1, 2, 3]: + return 'flat' + return 'flat' \ No newline at end of file diff --git a/apps/enm/tasks.py b/apps/enm/tasks.py index 9b347910..17c534eb 100644 --- a/apps/enm/tasks.py +++ b/apps/enm/tasks.py @@ -26,7 +26,7 @@ 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_sec_now, cal_exp_duration_sec from apps.utils.sql import DbConnection -from apps.enm.services import db_insert_mplogx_batch +from apps.enm.services import db_insert_mplogx_batch, get_elec_level from apps.enm.xscript import main from django.core.exceptions import ObjectDoesNotExist myLogger = logging.getLogger("log") @@ -122,107 +122,19 @@ 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 - """ - if hour in [8, 9, 10, 19, 20, 21, 22, 23]: - high_time_ranges = [ - (8, 11), # 上午高峰(小时数范围) - (19, 23), # 晚上高峰(小时数范围) - ] - # 构建查询条件 - 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 = [ - (11, 13), # 上午平谷 - (17, 19), # 晚上平谷 - (0, 4), - ] - # 构建查询条件 - 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 = [ - (4, 8), # 上午低谷 - (13, 17), # 晚上低谷 - ] - # 构建查询条件 - 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 = [ - (14, 16), # 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): (19, 21), - (7,): (21, 23) - } - 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") - return '' - - @shared_task(base=CustomTask) def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: int, cascade=True, sflog_hours=[]): """ 计算某一测点, 某一时间点某一小时的统计值 """ + val_level = None 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) # 下个整点 if (mpoint.material or mpoint.type == Mpoint.MT_COMPUTE) and mpoint.val_type in ['float', 'int']: # 如果计量的是物料 # 累计量 有的会清零,需要额外处理(还未做) + material_code = mpoint.material.code if mpoint.material else None params = {"mpoint": mpoint, "type": "hour"} params["year"], params["month"], params["day"], params["hour"] = year, month, day, hour val = 0 @@ -257,7 +169,9 @@ 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 - update_mpoint_val(month, hour) + if material_code == 'elec': + val_level = get_elec_level(month, hour) + ms.val_level = val_level # ms.val_level ms.save() diff --git a/apps/enm/views.py b/apps/enm/views.py index a5c1ef07..a3fb1232 100644 --- a/apps/enm/views.py +++ b/apps/enm/views.py @@ -207,6 +207,15 @@ class MpointStatViewSet(BulkCreateModelMixin, BulkDestroyModelMixin, CustomListM task = cal_mpointstats_duration.delay(data["start_time"], data["end_time"]) return Response({"task_id": task.task_id}) + # @action(methods=["get"], detail=False, perms_map={"get": "*"}) + # def group_ana(self, request, *args, **kwargs): + # """ + # 测点统计数据聚合查询 + + # 测点统计数据聚合查询 + # """ + # qs = self.filter_queryset(self.get_queryset()) + # qs.annote class EnStatViewSet(CustomListModelMixin, CustomGenericViewSet): """