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

View File

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