Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rule break support #73

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions risk_models/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, d):
self.id = d['id']
self.uuid = d['uuid']
self.name = d['title']
self.allow_break = d['allow_break'],
self.allow_break = d['allow_break'] if 'allow_break' in d else False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个直接 d.get('auto_break', False) 就可以

self.strategy_group_list = []
origin_strategy_group_list = json.loads(d['strategys'])
for strategy_group in origin_strategy_group_list:
Expand Down Expand Up @@ -234,12 +234,10 @@ def calculate_rule(id_, req_body, rules=None, ac=None):

# 目前策略为过全部策略原子组以积累数据,若无此需求,可自行进行短路
if all(results):
if not result_seted:
rv_control, rv_weight, result_seted = control, weight, True

# 当前命中的策略组在此规则中是第几个命中的
hit_number += 1

# 记录命中日志
msg = json.dumps(dict(rule_id=id_,
kwargs={},
req_body=req_body,
Expand All @@ -249,6 +247,11 @@ def calculate_rule(id_, req_body, rules=None, ac=None):
group_uuid=group_uuid,
hit_number=hit_number))
hit_logger.info(msg)
if allow_break and hit_number > 0:
return rv_control, rv_weight, custom
return rv_control, rv_weight, custom

# 允许策略短路, 命中权重高的策略后, 之后的策略就不走了
if allow_break:
return rv_control, rv_weight

if not result_seted:
rv_control, rv_weight, result_seted = control, weight, True
return rv_control, rv_weight
4 changes: 2 additions & 2 deletions server/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def query_handler(req_body):
try:
assert rule_id
rule_id = str(rule_id)
control, weight, custom = calculate_rule(rule_id, req_body, rules=rules, ac=ac)
result = {'control': control, 'weight': weight, 'custom': custom}
control, weight = calculate_rule(rule_id, req_body, rules=rules, ac=ac)
result = {'control': control, 'weight': weight}
except AssertionError:
error = 'must contain rule_id'
ec = 100
Expand Down
30 changes: 0 additions & 30 deletions www/rule/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import json

from django import forms
from django.forms import BooleanField
from django.forms.utils import flatatt
from django.utils import timezone
from django.utils.encoding import force_text
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _

from core.forms import BaseFilterForm, BaseForm
Expand All @@ -34,37 +30,11 @@
}


class PrettyCheckboxWidget(forms.widgets.CheckboxInput):
def render(self, name, value, attrs=None):
attrs = {type: 'checkbox', name: name}
final_attrs = self.build_attrs(attrs)
if self.check_test(value):
final_attrs['checked'] = 'checked'
if not (value is True or value is False or value is None or value == ''):
final_attrs['value'] = force_text(value)
if 'prettycheckbox-label' in final_attrs:
label = _(final_attrs.pop('prettycheckbox-label'))
else:
label = ''
return format_html('<label for="{0}"><input{1} /> {2}</label>', attrs['id'], flatatt(final_attrs), label)


class PrettyCheckboxField(BooleanField):
widget = PrettyCheckboxWidget

def __init__(self, *args, **kwargs):
if kwargs['label']:
kwargs['widget'].attrs['prettycheckbox-label'] = kwargs['label']
kwargs['label'] = ''
super(PrettyCheckboxField, self).__init__(*args, **kwargs)


class RulesForm(BaseForm):
title = forms.CharField(label=_(u"规则名称"))
describe = forms.CharField(required=False, label=_(u"规则描述"),
widget=forms.Textarea)
status = forms.ChoiceField(label=_(u"状态"), choices=STATUS_CHOICES)
#allow_break = PrettyCheckboxField(label=_(u"允许策略短路"), widget=PrettyCheckboxWidget())
allow_break = forms.BooleanField(label=_(u"允许策略短路"), initial=True, required=True)
end_time = forms.DateTimeField()
strategy = forms.ChoiceField(label=_("策略原子"), required=False)
Expand Down