Coverage for watcher/objects/efficacy_indicator.py: 100%

51 statements  

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

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

2# Copyright 2013 IBM Corp. 

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 

17from watcher.common import exception 

18from watcher.common import utils 

19from watcher.db import api as db_api 

20from watcher.objects import base 

21from watcher.objects import fields as wfields 

22 

23 

24@base.WatcherObjectRegistry.register 

25class EfficacyIndicator(base.WatcherPersistentObject, base.WatcherObject, 

26 base.WatcherObjectDictCompat): 

27 # Version 1.0: Initial version 

28 VERSION = '1.0' 

29 

30 dbapi = db_api.get_instance() 

31 

32 fields = { 

33 'id': wfields.IntegerField(), 

34 'uuid': wfields.UUIDField(), 

35 'action_plan_id': wfields.IntegerField(), 

36 'name': wfields.StringField(), 

37 'description': wfields.StringField(nullable=True), 

38 'unit': wfields.StringField(nullable=True), 

39 'value': wfields.NumericField(), 

40 } 

41 

42 @base.remotable_classmethod 

43 def get(cls, context, efficacy_indicator_id): 

44 """Find an efficacy indicator object given its ID or UUID 

45 

46 :param efficacy_indicator_id: the ID or UUID of an efficacy indicator. 

47 :returns: a :class:`EfficacyIndicator` object. 

48 """ 

49 if utils.is_int_like(efficacy_indicator_id): 

50 return cls.get_by_id(context, efficacy_indicator_id) 

51 elif utils.is_uuid_like(efficacy_indicator_id): 

52 return cls.get_by_uuid(context, efficacy_indicator_id) 

53 else: 

54 raise exception.InvalidIdentity(identity=efficacy_indicator_id) 

55 

56 @base.remotable_classmethod 

57 def get_by_id(cls, context, efficacy_indicator_id): 

58 """Find an efficacy indicator given its integer ID 

59 

60 :param efficacy_indicator_id: the id of an efficacy indicator. 

61 :returns: a :class:`EfficacyIndicator` object. 

62 """ 

63 db_efficacy_indicator = cls.dbapi.get_efficacy_indicator_by_id( 

64 context, efficacy_indicator_id) 

65 efficacy_indicator = EfficacyIndicator._from_db_object( 

66 cls(context), db_efficacy_indicator) 

67 return efficacy_indicator 

68 

69 @base.remotable_classmethod 

70 def get_by_uuid(cls, context, uuid): 

71 """Find an efficacy indicator given its UUID 

72 

73 :param uuid: the uuid of an efficacy indicator. 

74 :param context: Security context 

75 :returns: a :class:`EfficacyIndicator` object. 

76 """ 

77 db_efficacy_indicator = cls.dbapi.get_efficacy_indicator_by_uuid( 

78 context, uuid) 

79 efficacy_indicator = EfficacyIndicator._from_db_object( 

80 cls(context), db_efficacy_indicator) 

81 return efficacy_indicator 

82 

83 @base.remotable_classmethod 

84 def list(cls, context, limit=None, marker=None, filters=None, 

85 sort_key=None, sort_dir=None): 

86 """Return a list of EfficacyIndicator objects. 

87 

88 :param context: Security context. 

89 :param limit: maximum number of resources to return in a single result. 

90 :param marker: pagination marker for large data sets. 

91 :param filters: Filters to apply. Defaults to None. 

92 :param sort_key: column to sort results by. 

93 :param sort_dir: direction to sort. "asc" or "desc". 

94 :returns: a list of :class:`EfficacyIndicator` object. 

95 

96 """ 

97 db_efficacy_indicators = cls.dbapi.get_efficacy_indicator_list( 

98 context, 

99 limit=limit, 

100 marker=marker, 

101 filters=filters, 

102 sort_key=sort_key, 

103 sort_dir=sort_dir) 

104 

105 return [cls._from_db_object(cls(context), obj) 

106 for obj in db_efficacy_indicators] 

107 

108 @base.remotable 

109 def create(self, context=None): 

110 """Create a EfficacyIndicator record in the DB. 

111 

112 :param context: Security context. NOTE: This should only 

113 be used internally by the indirection_api. 

114 Unfortunately, RPC requires context as the first 

115 argument, even though we don't use it. 

116 A context should be set when instantiating the 

117 object, e.g.: EfficacyIndicator(context) 

118 

119 """ 

120 values = self.obj_get_changes() 

121 db_efficacy_indicator = self.dbapi.create_efficacy_indicator(values) 

122 self._from_db_object(self, db_efficacy_indicator) 

123 

124 def destroy(self, context=None): 

125 """Delete the EfficacyIndicator from the DB. 

126 

127 :param context: Security context. NOTE: This should only 

128 be used internally by the indirection_api. 

129 Unfortunately, RPC requires context as the first 

130 argument, even though we don't use it. 

131 A context should be set when instantiating the 

132 object, e.g.: EfficacyIndicator(context) 

133 """ 

134 self.dbapi.destroy_efficacy_indicator(self.uuid) 

135 self.obj_reset_changes() 

136 

137 @base.remotable 

138 def save(self, context=None): 

139 """Save updates to this EfficacyIndicator. 

140 

141 Updates will be made column by column based on the result 

142 of self.what_changed(). 

143 

144 :param context: Security context. NOTE: This should only 

145 be used internally by the indirection_api. 

146 Unfortunately, RPC requires context as the first 

147 argument, even though we don't use it. 

148 A context should be set when instantiating the 

149 object, e.g.: EfficacyIndicator(context) 

150 """ 

151 updates = self.obj_get_changes() 

152 self.dbapi.update_efficacy_indicator(self.uuid, updates) 

153 

154 self.obj_reset_changes() 

155 

156 @base.remotable 

157 def refresh(self, context=None): 

158 """Loads updates for this EfficacyIndicator. 

159 

160 Loads an efficacy indicator with the same uuid from the database and 

161 checks for updated attributes. Updates are applied to the loaded 

162 efficacy indicator column by column, if there are any updates. 

163 

164 :param context: Security context. NOTE: This should only 

165 be used internally by the indirection_api. 

166 Unfortunately, RPC requires context as the first 

167 argument, even though we don't use it. 

168 A context should be set when instantiating the 

169 object, e.g.: EfficacyIndicator(context) 

170 """ 

171 current = self.__class__.get_by_uuid(self._context, uuid=self.uuid) 

172 self.obj_refresh(current) 

173 

174 @base.remotable 

175 def soft_delete(self, context=None): 

176 """Soft Delete the efficacy indicator from the DB. 

177 

178 :param context: Security context. NOTE: This should only 

179 be used internally by the indirection_api. 

180 Unfortunately, RPC requires context as the first 

181 argument, even though we don't use it. 

182 A context should be set when instantiating the 

183 object, e.g.: Audit(context) 

184 """ 

185 self.dbapi.soft_delete_efficacy_indicator(self.uuid)