Completed
Pull Request — master (#2290)
by Manas
05:20
created

st2reactor.rules.RulesMatcher.get_matching_rules()   C

Complexity

Conditions 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 5.5
cc 7
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from st2common import log as logging
17
from st2common.constants.rules import RULE_TYPE_BACKSTOP
18
from st2reactor.rules.filter import RuleFilter, SecondPassRuleFilter
19
20
LOG = logging.getLogger('st2reactor.rules.RulesMatcher')
21
22
23
class RulesMatcher(object):
24
    def __init__(self, trigger_instance, trigger, rules, extra_info=False):
25
        self.trigger_instance = trigger_instance
26
        self.trigger = trigger
27
        self.rules = rules
28
        self.extra_info = extra_info
29
30
    def get_matching_rules(self):
31
        first_pass, second_pass = self._split_rules_into_passes()
32
        # first pass
33
        rule_filters = [RuleFilter(trigger_instance=self.trigger_instance,
34
                                   trigger=self.trigger,
35
                                   rule=rule,
36
                                   extra_info=self.extra_info)
37
                        for rule in first_pass]
38
        matched_rules = [rule_filter.rule for rule_filter in rule_filters if rule_filter.filter()]
39
        LOG.debug('[1st_pass] %d rule(s) found to enforce for %s.', len(matched_rules),
40
                  self.trigger['name'])
41
        # second pass
42
        rule_filters = [SecondPassRuleFilter(self.trigger_instance, self.trigger, rule,
43
                                             matched_rules)
44
                        for rule in second_pass]
45
        matched_in_second_pass = [rule_filter.rule for rule_filter in rule_filters
46
                                  if rule_filter.filter()]
47
        LOG.debug('[2nd_pass] %d rule(s) found to enforce for %s.', len(matched_in_second_pass),
48
                  self.trigger['name'])
49
        matched_rules.extend(matched_in_second_pass)
50
        LOG.info('%d rule(s) found to enforce for %s.', len(matched_rules),
51
                 self.trigger['name'])
52
        return matched_rules
53
54
    def _split_rules_into_passes(self):
55
        """
56
        Splits the rules in the Matcher into first_pass and second_pass collections.
57
58
        Since the
59
        """
60
        first_pass = []
61
        second_pass = []
62
        for rule in self.rules:
63
            if self._is_first_pass_rule(rule):
64
                first_pass.append(rule)
65
            else:
66
                second_pass.append(rule)
67
        return first_pass, second_pass
68
69
    def _is_first_pass_rule(self, rule):
70
        return rule.type['ref'] != RULE_TYPE_BACKSTOP
71