Coverage for watcher/decision_engine/goal/efficacy/base.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-17 12:22 +0000

1# -*- encoding: utf-8 -*- 

2# Copyright (c) 2016 b<>com 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# 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 

13# implied. 

14# See the License for the specific language governing permissions and 

15# limitations under the License. 

16 

17""" 

18An efficacy specification is a contract that is associated to each :ref:`Goal 

19<goal_definition>` that defines the various :ref:`efficacy indicators 

20<efficacy_indicator_definition>` a strategy achieving the associated goal 

21should provide within its :ref:`solution <solution_definition>`. Indeed, each 

22solution proposed by a strategy will be validated against this contract before 

23calculating its :ref:`global efficacy <efficacy_definition>`. 

24""" 

25 

26import abc 

27import jsonschema 

28from oslo_serialization import jsonutils 

29 

30 

31class EfficacySpecification(object, metaclass=abc.ABCMeta): 

32 

33 def __init__(self): 

34 self._indicators_specs = self.get_indicators_specifications() 

35 

36 @property 

37 def indicators_specs(self): 

38 return self._indicators_specs 

39 

40 @abc.abstractmethod 

41 def get_indicators_specifications(self): 

42 """List the specifications of the indicator for this efficacy spec 

43 

44 :return: Tuple of indicator specifications 

45 :rtype: Tuple of :py:class:`~.IndicatorSpecification` instances 

46 """ 

47 raise NotImplementedError() 

48 

49 @abc.abstractmethod 

50 def get_global_efficacy_indicator(self, indicators_map): 

51 """Compute the global efficacy for the goal it achieves 

52 

53 :param indicators_map: dict-like object containing the 

54 efficacy indicators related to this spec 

55 :type indicators_map: :py:class:`~.IndicatorsMap` instance 

56 :raises: NotImplementedError 

57 :returns: :py:class:`~.Indicator` instance list, each instance specify 

58 global efficacy for different openstack resource. 

59 """ 

60 raise NotImplementedError() 

61 

62 @property 

63 def schema(self): 

64 """Combined schema from the schema of the indicators""" 

65 schema = { 

66 "type": "object", 

67 "properties": {}, 

68 "required": [] 

69 } 

70 for indicator in self.indicators_specs: 

71 schema["properties"][indicator.name] = indicator.schema 

72 if indicator.required: 

73 schema["required"].append(indicator.name) 

74 return schema 

75 

76 def validate_efficacy_indicators(self, indicators_map): 

77 if indicators_map: 

78 jsonschema.validate(indicators_map, self.schema) 

79 else: 

80 True 

81 

82 def get_indicators_specs_dicts(self): 

83 return [indicator.to_dict() 

84 for indicator in self.indicators_specs] 

85 

86 def serialize_indicators_specs(self): 

87 return jsonutils.dumps(self.get_indicators_specs_dicts())