77
88class DSSStatisticsWorksheet (object ):
99 """
10- A handle to interact with a worksheet on the DSS instance
10+ A handle to interact with a worksheet.
1111 """
1212
1313 def __init__ (self , client , project_key , dataset_name , worksheet_id ):
@@ -16,109 +16,169 @@ def __init__(self, client, project_key, dataset_name, worksheet_id):
1616 self .dataset_name = dataset_name
1717 self .worksheet_id = worksheet_id
1818
19- ########################################################
20- # Worksheet deletion
21- ########################################################
22-
2319 def delete (self ):
2420 """
25- Delete the worksheet
21+ Deletes the worksheet
2622 """
2723 return self .client ._perform_empty (
2824 "DELETE" , "/projects/%s/datasets/%s/statistics/worksheets/%s" % (self .project_key , self .dataset_name , self .worksheet_id ))
2925
30- ########################################################
31- # Worksheet definition
32- ########################################################
26+ def get_settings ( self ):
27+ """
28+ Fetches the settings of this worksheet.
3329
34- def get_definition (self ):
30+ :return: an object to interact with the settings
31+ :rtype: :class:`DSSStatisticsWorksheetSettings`
32+ """
33+ worksheet_json = self .client ._perform_json (
34+ "GET" , "/projects/%s/datasets/%s/statistics/worksheets/%s" % (
35+ self .project_key , self .dataset_name , self .worksheet_id )
36+ )
37+ return DSSStatisticsWorksheetSettings (self .client , self .project_key ,
38+ self .dataset_name , self .worksheet_id , worksheet_json )
39+
40+ def run_worksheet (self ):
3541 """
36- Get the definition of the worksheet
42+ Computes the results of the whole worksheet.
3743
38- Returns:
39- the definition, as a JSON object
44+ :returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle
4045 """
41- return self .client ._perform_json (
42- "GET" , "/projects/%s/datasets/%s/statistics/worksheets/%s" % (self .project_key , self .dataset_name , self .worksheet_id ))
4346
44- def set_definition (self , definition ):
47+ root_card = self .get_settings ().get_raw ()['rootCard' ]
48+ return self .run_card (root_card )
49+
50+ def run_card (self , card ):
4551 """
46- Set the definition of the worksheet
52+ Runs a card in the context of the worksheet.
53+
54+ Note: the card does not need to belong to the worksheet.
4755
48- Args:
49- definition: the definition, as a JSON object. You should only set a definition object
50- that has been retrieved using the get_definition call.
56+ :param card: a card to compute
57+ :type card: :class:`DSSStatisticsCardSettings` or dict
58+ :returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle to the task of computing card's results
5159 """
52- return self .client ._perform_json (
53- "PUT" , "/projects/%s/datasets/%s/statistics/worksheets/%s" % (self .project_key , self .dataset_name , self .worksheet_id ), body = definition )
5460
55- def add_card (self , card_definition ):
61+ card = DSSStatisticsCardSettings ._from_card_or_dict (self .client , card )
62+ future_response = self .client ._perform_json (
63+ "POST" ,
64+ "/projects/%s/datasets/%s/statistics/worksheets/%s/actions/run-card" % (
65+ self .project_key , self .dataset_name , self .worksheet_id ),
66+ body = card .get_raw ()
67+ )
68+ return DSSFuture (self .client , future_response .get ("jobId" , None ), future_response )
69+
70+ def run_computation (self , computation ):
5671 """
57- Add a new card to the worksheet
72+ Runs a computation in the context of the worksheet.
5873
59- The precise structure of ``card_definition`` depends on the specific card type. To know which
60- fields exist for a given card type, create a worksheet from the UI, and use
61- :meth:`get_definition` to retrieve the configuration of the worksheet and inspect it.
74+ :param computation: a card to compute
75+ : type computation: :class:`DSSStatisticsComputationSettings` or dict
76+ :returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle to the task of computing computation's results
6277 """
63- worksheet = self .get_definition ()
64- worksheet ["rootCard" ]["cards" ].append (card_definition )
65- self .set_definition (worksheet )
6678
67- def get_standalone_cards (self ):
79+ computation = DSSStatisticsComputationSettings ._from_computation_or_dict (
80+ computation )
81+ future_response = self .client ._perform_json (
82+ "POST" ,
83+ "/projects/%s/datasets/%s/statistics/worksheets/%s/actions/run-computation" % (
84+ self .project_key , self .dataset_name , self .worksheet_id ),
85+ body = computation .get_raw ()
86+ )
87+ return DSSFuture (self .client , future_response .get ("jobId" , None ), future_response )
88+
89+
90+ class DSSStatisticsWorksheetSettings (object ):
91+ def __init__ (self , client , project_key , dataset_name , worksheet_id , worksheet_definition ):
92+ self .client = client
93+ self .project_key = project_key
94+ self .dataset_name = dataset_name
95+ self .worksheet_id = worksheet_id
96+ self ._worksheet_definition = worksheet_definition
97+
98+ def add_card (self , card ):
6899 """
69- Extract cards from this worksheet. A standalone card can be computed without worksheet.
100+ Adds a new card to the worksheet.
70101
71- :returns: An list of :class:`dataikuapi.dss.worksheet.DSSStatisticsCard`
102+ :param card: card to be added
103+ :type card: :class:`DSSStatisticsCardSettings` or dict
72104 """
105+ card = DSSStatisticsCardSettings ._from_card_or_dict (self .client , card )
106+ self ._worksheet_definition ['rootCard' ]['cards' ].append (card )
73107
74- definition = self .get_definition ()
75- standalone_cards = []
76- for card in definition ["rootCard" ]["cards" ]:
77- standalone_cards .append (DSSStatisticsCard (
78- self .client , self .project_key , self .dataset_name , definition ['dataSpec' ], card ))
108+ def list_cards (self ):
109+ """
110+ Lists the cards of this worksheet.
79111
80- return standalone_cards
112+ :rtype: list of :class:`DSSStatisticsCardSettings`
113+ """
114+ return [DSSStatisticsCardSettings (self .client , card_definition )
115+ for card_definition in self ._worksheet_definition ['rootCard' ]['cards' ]]
81116
82- ########################################################
83- # Obtaining worksheet results
84- ########################################################
117+ def get_raw ( self ):
118+ """
119+ Gets a reference to the raw settings of the worksheet.
85120
86- def compute ( self ):
121+ :rtype: dict
87122 """
88- Compute the results of the worksheet
123+ return self . _worksheet_definition
89124
90- :returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle to the task of computing worksheet's results
125+ def save (self ):
126+ """
127+ Saves the settings to DSS
91128 """
129+ return self .client ._perform_json (
130+ "PUT" ,
131+ "/projects/%s/datasets/%s/statistics/worksheets/%s" % (
132+ self .project_key , self .dataset_name , self .worksheet_id ),
133+ body = self ._worksheet_definition
134+ )
92135
93- future_response = self .client ._perform_json (
94- "POST" , "/projects/%s/datasets/%s/statistics/worksheets/%s/actions/compute-worksheet" % (self .project_key , self .dataset_name , self .worksheet_id ))
95136
96- return DSSFuture (self .client , future_response .get ("jobId" , None ), future_response )
137+ class DSSStatisticsCardSettings (object ):
138+ def __init__ (self , client , card_definition ):
139+ self .client = client
140+ self ._card_definition = card_definition
97141
142+ def get_raw (self ):
143+ """
144+ Gets a reference to the raw settings of the card.
98145
99- class DSSStatisticsCard ( object ):
100- """
101- A handle to interact with a standalone card (a card outside a worksheet)
146+ :rtype: dict
147+ """
148+ return self . _card_definition
102149
103- Unlike a worksheet, a standalone card is not persisted on the DSS instance
104- """
150+ def compile (self ):
151+ """
152+ Gets the underlying computation used to compute the card results.
105153
106- def __init__ (self , client , project_key , dataset_name , data_spec , card ):
107- self .client = client
108- self .project_key = project_key
109- self .dataset_name = dataset_name
110- self .data_spec = data_spec
111- self .card = card
154+ :rtype: DSSStatisticsComputationSettings
155+ """
156+ computation_json = self .client ._perform_json (
157+ "POST" , "/statistics/cards/compile" , body = self ._card_definition
158+ )
159+ return DSSStatisticsComputationSettings (computation_json )
160+
161+ @staticmethod
162+ def _from_card_or_dict (client , card_or_dict ):
163+ if isinstance (card_or_dict , DSSStatisticsCardSettings ):
164+ card_or_dict = card_or_dict .get_raw ()
165+ return DSSStatisticsCardSettings (client , card_or_dict )
112166
113- def compute (self ):
167+
168+ class DSSStatisticsComputationSettings (object ):
169+ def __init__ (self , computation_definition ):
170+ self ._computation_definition = computation_definition
171+
172+ def get_raw (self ):
114173 """
115- Compute the results of this single card (without worksheet)
174+ Gets the raw settings of the computation.
116175
117- :returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle to the task of computing card's results
176+ :rtype: dict
118177 """
178+ return self ._computation_definition
119179
120- future_response = self . client . _perform_json (
121- "POST" , "/projects/%s/datasets/%s/statistics/cards/compute-card" % (
122- self . project_key , self . dataset_name ),
123- body = { "card" : self . card , "dataSpec" : self . data_spec } )
124- return DSSFuture ( self . client , future_response . get ( "jobId" , None ), future_response )
180+ @ staticmethod
181+ def _from_computation_or_dict ( computation_or_dict ):
182+ if isinstance ( computation_or_dict , DSSStatisticsComputationSettings ):
183+ computation_or_dict = computation_or_dict . get_raw ( )
184+ return DSSStatisticsComputationSettings ( computation_or_dict )
0 commit comments