feat: 修改 tkx 峰谷平

This commit is contained in:
zty 2024-12-04 16:08:43 +08:00
parent cbaf79084c
commit c4c50670fd
3 changed files with 36 additions and 93 deletions

View File

@ -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
return value if value != 0 else default
def get_elec_level(month, hour):
"""
peak:用电尖峰 11112月份 1900-2100 -- 7 月份 2100-2300 更新val_level peak
high:用电高峰时段8小时800--1100 1900--2400 更新val_level high
flat:用电平谷时段8小时 1100--1300 1700--1900 更新val_level flat
low:用电低谷时段8小时 400--800, 13:00--1700 更新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'

View File

@ -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小时800--1100 1900--2400 更新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小时 1100--1300 1700--1900 更新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小时 400--800, 13:00--1700 更新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小时 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 = [
(14, 16), # 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): (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()

View File

@ -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):
"""