Merge branch 'master' of https://e.coding.net/ctcdevteam/ehs/ehs_server
This commit is contained in:
commit
7dc11e032d
|
@ -0,0 +1,24 @@
|
|||
# Generated by Django 3.2.12 on 2025-02-11 06:22
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('enm', '0056_auto_20250121_0940'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='mpoint',
|
||||
name='cal_related_mgroup_running',
|
||||
field=models.PositiveSmallIntegerField(blank=True, choices=[(10, '运行时统计')], default=10, null=True, verbose_name='与工段运行状态的关联'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='mpoint',
|
||||
name='mpoint_from',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='mp_mpoint_from', to='enm.mpoint', verbose_name='来源自采测点'),
|
||||
),
|
||||
]
|
|
@ -59,6 +59,9 @@ class Mpoint(CommonBModel):
|
|||
cal_coefficient_method = models.PositiveSmallIntegerField("计算系数方法", default=1, choices=((1, "乘"), (2, "除")))
|
||||
cal_coefficient = models.FloatField("计算系数", null=True, blank=True)
|
||||
|
||||
mpoint_from = models.ForeignKey("self", verbose_name="来源自采测点", related_name="mp_mpoint_from", on_delete=models.SET_NULL, null=True, blank=True)
|
||||
cal_related_mgroup_running = models.PositiveSmallIntegerField("与工段运行状态的关联", default=10, choices=[(10, "运行时统计")], null=True, blank=True)
|
||||
|
||||
@classmethod
|
||||
def cache_key(cls, code: str):
|
||||
return f"mpoint_{code}"
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
from apps.utils.tasks import CustomTask
|
||||
from celery import shared_task
|
||||
from apps.enm.models import MpLogx, Mpoint, MpointStat, EnStat, EnStat2, Xscript
|
||||
from apps.wpm.models import SfLog
|
||||
from apps.wpm.models import SfLog, StLog
|
||||
import datetime
|
||||
from django.db.models import Q
|
||||
from django.db.models import Sum, Avg
|
||||
|
@ -161,7 +161,18 @@ def cal_mpointstats_scheduled_tasks(m_code_list=None, cal_attrs=None):
|
|||
cal_mpointstats(0, year, month, day, hour, m_code_list, cal_attrs)
|
||||
current_time += datetime.timedelta(hours=1)
|
||||
|
||||
|
||||
def get_first_stlog_time_from_duration(mgroup:Mgroup, dt_start:datetime, dt_end: datetime):
|
||||
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, ""
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: int, cascade=True, sflog_hours=[]):
|
||||
|
@ -181,11 +192,11 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
|
|||
val = 0
|
||||
val_type = mpoint.val_type
|
||||
if mpoint.type == Mpoint.MT_AUTO:
|
||||
if mpoint.is_unit:
|
||||
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"]
|
||||
if val is None:
|
||||
val = 0
|
||||
elif mpoint.up_down:
|
||||
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")
|
||||
if mrs0.exists() and mrs.exists():
|
||||
|
@ -197,23 +208,32 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
|
|||
# 加入氨水 需要手动校正 ,目前先取绝对值。
|
||||
val = abs(first_val - last_val)
|
||||
else:
|
||||
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")
|
||||
if mrs0.exists() and mrs.exists():
|
||||
last_val = getattr(mrs.last(), f'val_{val_type}')
|
||||
first_val = getattr(mrs0.last(), f'val_{val_type}')
|
||||
if last_val >= first_val or first_val == 0:
|
||||
val = last_val - first_val
|
||||
elif first_val - last_val > 0 and (first_val - last_val)/first_val < 0.01:
|
||||
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
|
||||
myLogger.info(f'{mpoint.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}--清零')
|
||||
val = max_val - first_val + last_val
|
||||
# if mpoint.code == 'x水泥+P.O42.5 散装':
|
||||
|
||||
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")
|
||||
if mrs0.exists() and mrs.exists():
|
||||
last_val = getattr(mrs.last(), f'val_{val_type}')
|
||||
first_val = getattr(mrs0.last(), f'val_{val_type}')
|
||||
if last_val >= first_val or first_val == 0:
|
||||
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')
|
||||
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}--清零')
|
||||
val = max_val - first_val + last_val
|
||||
elif mpoint.type == Mpoint.MT_COMPUTE and mpoint.formula:
|
||||
formula = mpoint.formula
|
||||
val = translate_eval_formula(formula, year, month, day, hour)
|
||||
|
|
|
@ -7,6 +7,7 @@ from server.settings import get_sysconfig
|
|||
import importlib
|
||||
from django.core.cache import cache
|
||||
import requests
|
||||
import subprocess
|
||||
|
||||
# 实例化myLogger
|
||||
myLogger = logging.getLogger('log')
|
||||
|
@ -60,4 +61,16 @@ def ctask_run(func_str: str, *args, **kwargs):
|
|||
module, func = func_str.rsplit(".", 1)
|
||||
m = importlib.import_module(module)
|
||||
f = getattr(m, func)
|
||||
f(*args, **kwargs)
|
||||
f(*args, **kwargs)
|
||||
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
def restart_ntp():
|
||||
"""更新服务器时间
|
||||
"""
|
||||
try:
|
||||
# 执行命令
|
||||
subprocess.run(['sudo', 'systemctl', 'restart', 'ntp'], check=True)
|
||||
myLogger.info("NTP service restarted successfully.")
|
||||
except subprocess.CalledProcessError as e:
|
||||
myLogger.error(f"Error restarting NTP service: {e}")
|
Loading…
Reference in New Issue