fix: translate_eval_formula bug
This commit is contained in:
parent
48c6ad53b9
commit
2e4f6f0445
|
|
@ -27,8 +27,10 @@ def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour:
|
||||||
matches = re.findall(pattern, exp_str)
|
matches = re.findall(pattern, exp_str)
|
||||||
for match in matches:
|
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()
|
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:
|
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)
|
rval = eval(exp_str)
|
||||||
return rval
|
return rval
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
params["year"], params["month"], params["day"], params["hour"] = year, month, day, hour
|
||||||
val = 0
|
val = 0
|
||||||
val_type = mpoint.val_type
|
val_type = mpoint.val_type
|
||||||
if mpoint.formula:
|
if mpoint.type == Mpoint.MT_AUTO:
|
||||||
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:
|
|
||||||
mrs0 = MpLogx.objects.filter(mpoint=mpoint, timex__gte=dt_hour_p, timex__lte=dt).order_by("timex")
|
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")
|
mrs = MpLogx.objects.filter(mpoint=mpoint, timex__gte=dt, timex__lte=dt_hour_n).order_by("timex")
|
||||||
if mrs0.exists() and mrs.exists():
|
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"]
|
max_val = mrs.aggregate(max=Max(f'val_{val_type}'))["max"]
|
||||||
val = max_val - first_val + last_val
|
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, _ = MpointStat.objects.get_or_create(**params, defaults=params)
|
||||||
ms.val = val
|
ms.val = val
|
||||||
ms.save()
|
ms.save()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue