feat: eval_formula测试通过

This commit is contained in:
caoqianming 2023-08-04 16:57:12 +08:00
parent 5cfb12113e
commit 2f42d8386e
3 changed files with 7 additions and 8 deletions

View File

@ -9,14 +9,12 @@ def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour:
"""
传入
"""
pattern = r'\${(\d+)}'
pattern = r"\${(.*?)}"
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()
if mpst:
exp_str = exp_str.replace(f"${{{match}}}", mpst.val)
exp_str = exp_str.replace(f"${{{match}}}", str(mpst.val))
rval = eval(exp_str)
return rval

View File

@ -64,12 +64,12 @@ 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
if mpoint.formula:
formular = mpoint.formular
formula = mpoint.formula
try:
val = translate_eval_formula(formular, year, month, day, hour)
val = translate_eval_formula(formula, year, month, day, hour)
except:
myLogger.error('公式执行错误:{}-{}'.format(mpoint.id, formular), exc_info=True)
raise
myLogger.error('公式执行错误:{}-{}'.format(mpoint.id, formula), exc_info=True)
return
else:
mrs = MpLog.objects.filter(
mpoint=mpoint,

View File

@ -67,6 +67,7 @@ class EnStatViewSet(ListModelMixin, CustomGenericViewSet):
serializer_class = EnStatSerializer
select_related_fields = ['mgroup', 'team', 'mgroup__belong_dept']
filterset_class = EnStatFilter
ordering = ['mgroup__sort', 'year_s', 'month_s', 'day_s', 'hour']
class EnStat2ViewSet(ListModelMixin, CustomGenericViewSet):