4 from slaclient import wsag_model
\r
5 from slaclient.wsag_model import AgreementStatus
\r
6 from slaclient.wsag_model import Violation
\r
9 VIOLATED = AgreementStatus.StatusEnum.VIOLATED
\r
10 NON_DETERMINED = AgreementStatus.StatusEnum.NON_DETERMINED
\r
11 FULFILLED = AgreementStatus.StatusEnum.FULFILLED
\r
14 def get_violations_bydate(violations):
\r
15 """Returns a list of violations per date, from a list of violations
\r
17 :param violations list[Violation]:
\r
21 for v in violations:
\r
22 assert isinstance(v, Violation)
\r
23 date = v.datetime.date()
\r
28 result = [(key, d[key]) for key in sorted(d.keys(), reverse=True)]
\r
32 class AgreementAnnotator(object):
\r
33 """Annotates an agreement with the following attributes:
\r
35 agreement.guaranteestatus
\r
36 agreement.statusclass
\r
37 agreement.guaranteeterms[*].status
\r
38 agreement.guaranteeterms[*].statusclass
\r
39 agreement.guaranteeterms[*].nviolations
\r
40 agreement.guaranteeterms[*].servicelevelobjetive.bounds
\r
47 def _get_statusclass(status):
\r
48 if status is None or status == "" or status == NON_DETERMINED:
\r
49 return "non-determined"
\r
50 return "success" if status == FULFILLED else "error"
\r
53 def _parse_bounds(servicelevel):
\r
54 # pattern = re.compile(".*BETWEEN *[(]?(.*), *([^)]*)[)]?")
\r
55 pattern = re.compile(".*GT *([+-]?\\d*\\.\\d+)(?![-+0-9\\.])")
\r
56 constraint = eval(servicelevel.strip(' \t\n\r'))
\r
57 m = pattern.match(constraint['constraint'])
\r
60 def _annotate_guaranteeterm(self, term, violations):
\r
62 # Annotate a guarantee term: set bounds and violations
\r
64 level = term.servicelevelobjective.customservicelevel
\r
65 bounds = AgreementAnnotator._parse_bounds(level)
\r
66 term.servicelevelobjective.bounds = bounds
\r
69 # set status attribute if not set before
\r
71 if not hasattr(term, 'status'):
\r
72 term.status = wsag_model.AgreementStatus.StatusEnum.NON_DETERMINED
\r
77 for violation in violations:
\r
78 if violation.metric_name == term.servicelevelobjective.kpiname:
\r
80 term.nviolations = n
\r
82 def _annotate_guaranteeterm_by_status(
\r
83 self, agreement, termstatus, violations):
\r
85 # Annotate a guarantee term: it is different from the previous
\r
86 # one in that this takes the status into account.
\r
88 name = termstatus.name
\r
89 status = termstatus.status
\r
91 term = agreement.guaranteeterms[name]
\r
92 term.status = status
\r
93 term.statusclass = AgreementAnnotator._get_statusclass(status)
\r
94 self._annotate_guaranteeterm(term, violations)
\r
96 def annotate_agreement(self, agreement, status=None, violations=()):
\r
98 """Annotate an agreement with certain values needed in the templates
\r
100 :param wsag_model.Agreement agreement: agreement to annotate
\r
101 :param wsag_model.AgreementStatus status: status of the agreement.
\r
102 :param violations: list of agreement's violations
\r
103 (wsag_model.Violation[])
\r
107 if status is not None:
\r
108 a.guaranteestatus = status.guaranteestatus
\r
109 a.statusclass = self._get_statusclass(status.guaranteestatus)
\r
110 for termstatus in status.guaranteeterms:
\r
111 self._annotate_guaranteeterm_by_status(
\r
112 agreement, termstatus, violations)
\r
114 a.guaranteestatus = NON_DETERMINED
\r
115 for termname, term in agreement.guaranteeterms.items():
\r
116 self._annotate_guaranteeterm(term, violations)
\r