fix: translate_eval_formula bug

This commit is contained in:
caoqianming 2024-04-29 16:52:01 +08:00
parent 48c6ad53b9
commit 2e4f6f0445
2 changed files with 13 additions and 9 deletions

View File

@ -27,8 +27,10 @@ def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour:
matches = re.findall(pattern, exp_str)
for match in matches:
mpst = MpointStat.objects.filter(Q(mpoint__id=match) | Q(mpoint__name=match) | Q(mpoint__code=match), type="hour", year=year, month=month, day=day, hour=hour).first()
val = 0
if mpst:
exp_str = exp_str.replace(f"${{{match}}}", str(mpst.val))
val = mpst.val
exp_str = exp_str.replace(f"${{{match}}}", str(val))
rval = eval(exp_str)
return rval

View File

@ -113,14 +113,7 @@ 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
if mpoint.formula:
formula = mpoint.formula
try:
val = translate_eval_formula(formula, year, month, day, hour)
except Exception:
myLogger.error("公式执行错误:{}-{}".format(mpoint.id, formula), exc_info=True)
return
else:
if mpoint.type == Mpoint.MT_AUTO:
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():
@ -132,6 +125,15 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
# 这里判断有可能清零了
max_val = mrs.aggregate(max=Max(f'val_{val_type}'))["max"]
val = max_val - first_val + last_val
elif mpoint.type == Mpoint.MT_COMPUTE and mpoint.formula:
formula = mpoint.formula
try:
val = translate_eval_formula(formula, year, month, day, hour)
except Exception:
myLogger.error("公式执行错误:{}-{}".format(mpoint.name, formula), exc_info=True)
return
else:
return
ms, _ = MpointStat.objects.get_or_create(**params, defaults=params)
ms.val = val
ms.save()