feat: eval_formula测试通过
This commit is contained in:
parent
5cfb12113e
commit
2f42d8386e
|
@ -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
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue