diff --git a/apps/enm/tasks.py b/apps/enm/tasks.py index 8d780b0a..e50b7ab4 100644 --- a/apps/enm/tasks.py +++ b/apps/enm/tasks.py @@ -165,12 +165,15 @@ def get_first_stlog_time_from_duration(mgroup:Mgroup, dt_start:datetime, dt_end: st_qs = StLog.objects.filter(is_shutdown=True, mgroup=mgroup) st = st_qs.filter(start_time__lte=dt_start, end_time=None).order_by("start_time").last() if st: + return st, "ending" st = st_qs.filter(start_time__gte=dt_start, start_time__lte=dt_end, duration_sec__lte=600).order_by("start_time").last() if st: + return st, "start" st = st_qs.filter(end_time__gte=dt_start, end_time__lte=dt_end, duration_sec__lte=600).order_by("end_time").first() if st: + return st, "end" return None, "" @@ -191,14 +194,15 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in params["year"], params["month"], params["day"], params["hour"] = year, month, day, hour val = 0 val_type = mpoint.val_type + mpointfrom = mpoint.mpoint_from if mpoint.mpoint_from else mpoint if mpoint.type == Mpoint.MT_AUTO: if mpoint.is_unit: # 单位量 - val = MpLogx.objects.filter(mpoint=mpoint, timex__gte=dt, timex__lt=dt_hour_n).aggregate(sum=Sum(f'val_{mpoint.val_type}'))["sum"] + val = MpLogx.objects.filter(mpoint=mpointfrom, timex__gte=dt, timex__lt=dt_hour_n).aggregate(sum=Sum(f'val_{mpointfrom.val_type}'))["sum"] if val is None: val = 0 elif mpoint.up_down: # 液位量 - mrs0 = MpLogx.objects.filter(mpoint=mpoint, timex__gte=dt_hour_p, timex__lte=dt).order_by("timex") - mrs = MpLogx.objects.filter(mpoint=mpoint, timex__gte=dt, timex__lte=dt_hour_n).order_by("timex") + mrs0 = MpLogx.objects.filter(mpoint=mpointfrom, timex__gte=dt_hour_p, timex__lte=dt).order_by("timex") + mrs = MpLogx.objects.filter(mpoint=mpointfrom, timex__gte=dt, timex__lte=dt_hour_n).order_by("timex") if mrs0.exists() and mrs.exists(): last_val = getattr(mrs.last(), f'val_{val_type}') first_val = getattr(mrs0.last(), f'val_{val_type}') @@ -208,19 +212,21 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in # 加入氨水 需要手动校正 ,目前先取绝对值。 val = abs(first_val - last_val) else: - mpointfrom = mpoint.mpoint_from xtype = "normal" if mpointfrom and mpoint.cal_related_mgroup_running == 10: + stlog, xtype = get_first_stlog_time_from_duration(mpoint.mgroup, dt, dt_hour_n) + if xtype == "ending": val = 0 if xtype == "start": dt_hour_n = stlog.start_time elif xtype == "end": dt = stlog.end_time + # 需要将来源于自采的测点对象传入到下面 if xtype != "ending": - mrs0 = MpLogx.objects.filter(mpoint=mpoint, timex__gte=dt_hour_p, timex__lte=dt).order_by("timex") - mrs = MpLogx.objects.filter(mpoint=mpoint, timex__gte=dt, timex__lte=dt_hour_n).order_by("timex") + mrs0 = MpLogx.objects.filter(mpoint=mpointfrom, timex__gte=dt_hour_p, timex__lte=dt).order_by("timex") + mrs = MpLogx.objects.filter(mpoint=mpointfrom, timex__gte=dt, timex__lte=dt_hour_n).order_by("timex") if mrs0.exists() and mrs.exists(): last_val = getattr(mrs.last(), f'val_{val_type}') first_val = getattr(mrs0.last(), f'val_{val_type}') @@ -228,11 +234,11 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in val = last_val - first_val elif first_val - last_val > 0 and (first_val - last_val)/first_val < 0.01: val = 0 - myLogger.info(f'{mpoint.code}--{dt}--{last_val}--{first_val}--last_val 小于 first_val') + myLogger.info(f'{mpointfrom.code}--{dt}--{last_val}--{first_val}--last_val 小于 first_val') else: # 这里判断有可能清零了 max_val = max(mrs.aggregate(max=Max(f'val_{val_type}'))["max"], first_val) - myLogger.info(f'{mpoint.id}--{mpoint.code}--{dt}--{last_val}--{first_val}--清零') + myLogger.info(f'{mpointfrom.id}--{mpointfrom.code}--{dt}--{last_val}--{first_val}--清零') val = max_val - first_val + last_val elif mpoint.type == Mpoint.MT_COMPUTE and mpoint.formula: formula = mpoint.formula diff --git a/apps/utils/tasks.py b/apps/utils/tasks.py index f9cb68fe..dd61f50c 100644 --- a/apps/utils/tasks.py +++ b/apps/utils/tasks.py @@ -8,6 +8,7 @@ import importlib from django.core.cache import cache import requests import subprocess +from server.settings import SD_PWD # 实例化myLogger myLogger = logging.getLogger('log') @@ -68,9 +69,11 @@ def ctask_run(func_str: str, *args, **kwargs): def restart_ntp(): """更新服务器时间 """ - try: + # 执行命令 - subprocess.run(['sudo', 'systemctl', 'restart', 'ntp'], check=True) + command = 'echo "{}" | sudo -S systemctl restart ntp'.format(SD_PWD) + result = subprocess.run(command, shell=True, capture_output=True, text=True) + if result.returncode == 0: myLogger.info("NTP service restarted successfully.") - except subprocess.CalledProcessError as e: - myLogger.error(f"Error restarting NTP service: {e}") \ No newline at end of file + else: + myLogger.error(f"Error restarting NTP service: {result.stderr}") \ No newline at end of file