Skip to content

Commit d05d932

Browse files
committed
Adding classes for usage info. Wordings.
1 parent 6e6886d commit d05d932

File tree

1 file changed

+98
-44
lines changed

1 file changed

+98
-44
lines changed

dataikuapi/dss/plugin.py

Lines changed: 98 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
from .dataset import DSSDataset
2-
from .recipe import DSSRecipe
3-
from .managedfolder import DSSManagedFolder
4-
from .savedmodel import DSSSavedModel
5-
from .job import DSSJob
6-
from .scenario import DSSScenario
7-
from .apiservice import DSSAPIService
8-
import sys
9-
101
class DSSPluginSettings(object):
112
"""
123
The settings of a plugin.
@@ -124,43 +115,13 @@ def list_usages(self, project_key=None):
124115
"""
125116
Get the list of usages of the plugin.
126117
127-
Returns a dict with two keys:
128-
- usages, a list of (elementKind, elementType, objectType, projectKey, objectId) tuples
129-
- missingTypes, a list of (missingType, objectType, projectKey, objectId) tuples
130-
131-
Each element of the usages list contains:
132-
- an elementKind of the plugin element, such as webapps, python-probes, python-checks, etc.
133-
- an elementType of the plugin element,
134-
- the objectType and objectId of the object using this plugin element, along with their projectKey,
135-
if pertinent. Some objects, for instance clusters, are not contained in a project.
136-
137-
Some custom types may not be found during the analysis. This typically occurs when a plugin was removed,
138-
while still being used. This prevents further analysis of the object relying on this type and may hide
139-
some uses of the plugin. Thus, those missingTypes are enumerated in the missingTypes list, which
140-
includes the missingType along with the same information on the object as for usages.
141-
142118
:param str project_key: optional key of project where to look for usages. Default is None and looking in all projects.
143-
:return: dict
119+
:return: a :class:`DSSPluginUsages`
144120
"""
145-
params = {}
146-
if project_key:
147-
params["projectKey"] = project_key
148-
return self.client._perform_json("GET", "/plugins/{pluginId}/actions/listUsages".format(pluginId=self.plugin_id), body=params)
149-
150-
def prepare_delete(self):
151-
"""
152-
Request pre-deletion checks, as aggregated information on the usage of the plugin.
153-
154-
Information is provided as a dict with the following entries:
155-
- projectCount: count of projects using at least an element of this plugin
156-
- usedElemCount: count of elements of this plugin in use
157-
- objectAnalysisErrors: count of errors encountered while analyzing usages.
158-
159-
Detailed information can be obtained by calling :func:`list_usages`.
160-
:return: dict
161-
"""
162-
163-
return self.client._perform_json("POST", "/plugins/{pluginId}/actions/prepareDelete".format(pluginId=self.plugin_id))
121+
return DSSPluginUsages.build(
122+
self.client._perform_json("GET", "/plugins/{pluginId}/actions/listUsages".format(pluginId=self.plugin_id),
123+
params={"projectKey": project_key})
124+
)
164125

165126
def delete(self, force=False):
166127
"""
@@ -213,3 +174,96 @@ def put_file(self, path, f):
213174
file_name = path.split('/')[-1]
214175
data = f.read() # eat it all, because making it work with a path variable and a MultifilePart in swing looks complicated
215176
return self.client._perform_empty("POST", "/plugins/%s/contents/%s" % (self.plugin_id, path), raw_body=data)
177+
178+
179+
class DSSPluginUsage(object):
180+
"""
181+
Information on a usage of an element of a plugin.
182+
183+
object_id, object_type and project_key are usually provided, excepted for some global
184+
types, such as cluster types.
185+
"""
186+
def __init__(self, element_kind, element_type, object_id, object_type, project_key):
187+
"""
188+
189+
:param str element_kind:
190+
:param str element_type:
191+
:param str object_id:
192+
:param str object_type:
193+
:param str project_key:
194+
"""
195+
self.element_kind = element_kind
196+
self.element_type = element_type
197+
self.object_id = object_id
198+
self.object_type = object_type
199+
self.project_key = project_key
200+
201+
@staticmethod
202+
def build(json_object):
203+
return DSSPluginUsage(
204+
json_object["elementKind"],
205+
json_object["elementType"],
206+
json_object.get("objectId", None),
207+
json_object.get("objectType", None),
208+
json_object.get("projectKey", None)
209+
)
210+
211+
212+
class DSSMissingType(object):
213+
"""
214+
Information on a type not found while analyzing usages of a plugin.
215+
216+
object_id, object_type and project_key are usually provided, excepted for some global
217+
types, such as cluster types.
218+
"""
219+
def __init__(self, missing_type, object_id, object_type, project_key):
220+
"""
221+
222+
:param str missing_type: the missing type
223+
:param str object_id: the object using the missing type (can be None)
224+
:param str object_type: the type of the object using the missing type (can be None)
225+
:param str project_key: the project key where the type was found missing (can be None)
226+
"""
227+
self.missing_type = missing_type
228+
self.object_id = object_id
229+
self.object_type = object_type
230+
self.project_key = project_key
231+
232+
@staticmethod
233+
def build(json_object):
234+
return DSSMissingType(
235+
json_object["missingType"],
236+
json_object.get("objectId", None),
237+
json_object.get("objectType", None),
238+
json_object.get("projectKey", None)
239+
)
240+
241+
242+
class DSSPluginUsages(object):
243+
"""
244+
Information on the usages of a plugin.
245+
246+
Contains both usages (a list of instances of :class:`DSSPluginUsage`) and analysis errors, if any
247+
(a list of instances of :class:`DSSMissingType`).
248+
249+
Some custom types may not be found during usage analysis, typically when a plugin was removed
250+
but is still used. This prevents some detailed analysis and may hide some uses.
251+
This information is provided in missingTypes.
252+
"""
253+
def __init__(self, usages, missing_types):
254+
"""
255+
:param list(:class:`DSSPluginUsage`) usages: plugin usages
256+
:param list(:class:`DSSMissingType`) missing_types:
257+
"""
258+
self.usages = usages
259+
self.missing_types = missing_types
260+
261+
@staticmethod
262+
def build(json_object):
263+
usages = []
264+
missing_types = []
265+
for json_usage in json_object.get("usages", []):
266+
usages.append(DSSPluginUsage.build(json_usage))
267+
for json_missing_type in json_object.get("missingTypes"):
268+
missing_types.append(DSSMissingType.build(json_missing_type))
269+
return DSSPluginUsages(usages, missing_types)

0 commit comments

Comments
 (0)