Coverage for watcher/objects/scoring_engine.py: 94%

58 statements  

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

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

2# Copyright 2016 Intel 

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""" 

18A :ref:`Scoring Engine <scoring_engine_definition>` is an instance of a data 

19model, to which a learning data was applied. 

20 

21Because there might be multiple algorithms used to build a particular data 

22model (and therefore a scoring engine), the usage of scoring engine might 

23vary. A metainfo field is supposed to contain any information which might 

24be needed by the user of a given scoring engine. 

25""" 

26 

27from watcher.common import exception 

28from watcher.common import utils 

29from watcher.db import api as db_api 

30from watcher.objects import base 

31from watcher.objects import fields as wfields 

32 

33 

34@base.WatcherObjectRegistry.register 

35class ScoringEngine(base.WatcherPersistentObject, base.WatcherObject, 

36 base.WatcherObjectDictCompat): 

37 # Version 1.0: Initial version 

38 VERSION = '1.0' 

39 

40 dbapi = db_api.get_instance() 

41 

42 fields = { 

43 'id': wfields.IntegerField(), 

44 'uuid': wfields.UUIDField(), 

45 'name': wfields.StringField(), 

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

47 'metainfo': wfields.StringField(nullable=True), 

48 } 

49 

50 @base.remotable_classmethod 

51 def get(cls, context, scoring_engine_id): 

52 """Find a scoring engine based on its id or uuid 

53 

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

55 be used internally by the indirection_api. 

56 Unfortunately, RPC requires context as the first 

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

58 A context should be set when instantiating the 

59 object, e.g.: ScoringEngine(context) 

60 :param scoring_engine_name: the name of a scoring_engine. 

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

62 """ 

63 if utils.is_int_like(scoring_engine_id): 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true

64 return cls.get_by_id(context, scoring_engine_id) 

65 elif utils.is_uuid_like(scoring_engine_id): 65 ↛ 68line 65 didn't jump to line 68 because the condition on line 65 was always true

66 return cls.get_by_uuid(context, scoring_engine_id) 

67 else: 

68 raise exception.InvalidIdentity(identity=scoring_engine_id) 

69 

70 @base.remotable_classmethod 

71 def get_by_id(cls, context, scoring_engine_id): 

72 """Find a scoring engine based on its id 

73 

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

75 be used internally by the indirection_api. 

76 Unfortunately, RPC requires context as the first 

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

78 A context should be set when instantiating the 

79 object, e.g.: ScoringEngine(context) 

80 :param scoring_engine_id: the id of a scoring_engine. 

81 :returns: a :class:`ScoringEngine` object. 

82 """ 

83 db_scoring_engine = cls.dbapi.get_scoring_engine_by_id( 

84 context, 

85 scoring_engine_id) 

86 scoring_engine = ScoringEngine._from_db_object(cls(context), 

87 db_scoring_engine) 

88 return scoring_engine 

89 

90 @base.remotable_classmethod 

91 def get_by_uuid(cls, context, scoring_engine_uuid): 

92 """Find a scoring engine based on its uuid 

93 

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

95 be used internally by the indirection_api. 

96 Unfortunately, RPC requires context as the first 

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

98 A context should be set when instantiating the 

99 object, e.g.: ScoringEngine(context) 

100 :param scoring_engine_uuid: the uuid of a scoring_engine. 

101 :returns: a :class:`ScoringEngine` object. 

102 """ 

103 db_scoring_engine = cls.dbapi.get_scoring_engine_by_uuid( 

104 context, 

105 scoring_engine_uuid) 

106 scoring_engine = ScoringEngine._from_db_object(cls(context), 

107 db_scoring_engine) 

108 return scoring_engine 

109 

110 @base.remotable_classmethod 

111 def get_by_name(cls, context, scoring_engine_name): 

112 """Find a scoring engine based on its name 

113 

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

115 be used internally by the indirection_api. 

116 Unfortunately, RPC requires context as the first 

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

118 A context should be set when instantiating the 

119 object, e.g.: ScoringEngine(context) 

120 :param scoring_engine_name: the name of a scoring_engine. 

121 :returns: a :class:`ScoringEngine` object. 

122 """ 

123 db_scoring_engine = cls.dbapi.get_scoring_engine_by_name( 

124 context, 

125 scoring_engine_name) 

126 scoring_engine = ScoringEngine._from_db_object(cls(context), 

127 db_scoring_engine) 

128 return scoring_engine 

129 

130 @base.remotable_classmethod 

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

132 sort_key=None, sort_dir=None): 

133 """Return a list of :class:`ScoringEngine` objects. 

134 

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

136 be used internally by the indirection_api. 

137 Unfortunately, RPC requires context as the first 

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

139 A context should be set when instantiating the 

140 object, e.g.: ScoringEngine(context) 

141 :param filters: dict mapping the filter key to a value. 

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

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

144 :param sort_key: column to sort results by. 

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

146 :returns: a list of :class:`ScoringEngine` objects. 

147 """ 

148 db_scoring_engines = cls.dbapi.get_scoring_engine_list( 

149 context, 

150 filters=filters, 

151 limit=limit, 

152 marker=marker, 

153 sort_key=sort_key, 

154 sort_dir=sort_dir) 

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

156 for obj in db_scoring_engines] 

157 

158 @base.remotable 

159 def create(self): 

160 """Create a :class:`ScoringEngine` record in the DB.""" 

161 values = self.obj_get_changes() 

162 db_scoring_engine = self.dbapi.create_scoring_engine(values) 

163 self._from_db_object(self, db_scoring_engine) 

164 

165 def destroy(self): 

166 """Delete the :class:`ScoringEngine` from the DB""" 

167 self.dbapi.destroy_scoring_engine(self.id) 

168 self.obj_reset_changes() 

169 

170 @base.remotable 

171 def save(self): 

172 """Save updates to this :class:`ScoringEngine`. 

173 

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

175 of self.what_changed(). 

176 """ 

177 updates = self.obj_get_changes() 

178 db_obj = self.dbapi.update_scoring_engine(self.uuid, updates) 

179 obj = self._from_db_object(self, db_obj, eager=False) 

180 self.obj_refresh(obj) 

181 self.obj_reset_changes() 

182 

183 def refresh(self): 

184 """Loads updates for this :class:`ScoringEngine`. 

185 

186 Loads a scoring_engine with the same id from the database and 

187 checks for updated attributes. Updates are applied from 

188 the loaded scoring_engine column by column, if there are any updates. 

189 """ 

190 current = self.get_by_id(self._context, scoring_engine_id=self.id) 

191 self.obj_refresh(current) 

192 

193 def soft_delete(self): 

194 """Soft Delete the :class:`ScoringEngine` from the DB""" 

195 db_obj = self.dbapi.soft_delete_scoring_engine(self.id) 

196 obj = self._from_db_object( 

197 self.__class__(self._context), db_obj, eager=False) 

198 self.obj_refresh(obj)