11from ..utils import DataikuException
2+ from .utils import DSSDatasetSelectionBuilder
23from .future import DSSFuture
4+ from ..utils import DSSInternalDict
35import json
46from .metrics import ComputedMetrics
57from .discussion import DSSObjectDiscussions
@@ -37,24 +39,24 @@ def get_settings(self):
3739 return DSSStatisticsWorksheetSettings (self .client , self .project_key ,
3840 self .dataset_name , self .worksheet_id , worksheet_json )
3941
40- def run_worksheet (self ):
42+ def run_worksheet (self , wait = True ):
4143 """
4244 Computes the results of the whole worksheet.
4345
4446 :returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle
4547 """
4648
4749 root_card = self .get_settings ().get_raw ()['rootCard' ]
48- return self .run_card (root_card )
50+ return self .run_card (root_card , wait = wait )
4951
50- def run_card (self , card ):
52+ def run_card (self , card , wait = True ):
5153 """
5254 Runs a card in the context of the worksheet.
5355
5456 Note: the card does not need to belong to the worksheet.
5557
5658 :param card: a card to compute
57- :type card: :class:`DSSStatisticsCardSettings` or dict
59+ :type card: :class:`DSSStatisticsCardSettings` or dict (obtained from ``DSSStatisticsCardSettings.get_raw()``)
5860 :returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle to the task of computing card's results
5961 """
6062
@@ -65,14 +67,16 @@ def run_card(self, card):
6567 self .project_key , self .dataset_name , self .worksheet_id ),
6668 body = card .get_raw ()
6769 )
68- return DSSFuture (self .client , future_response .get ("jobId" , None ), future_response )
70+ future = DSSFuture (self .client , future_response .get (
71+ "jobId" , None ), future_response )
72+ return future .wait_for_result () if wait else future
6973
70- def run_computation (self , computation ):
74+ def run_computation (self , computation , wait = True ):
7175 """
7276 Runs a computation in the context of the worksheet.
7377
7478 :param computation: a card to compute
75- :type computation: :class:`DSSStatisticsComputationSettings` or dict
79+ :type computation: :class:`DSSStatisticsComputationSettings` or dict (obtained from ``DSSStatisticsComputationSettings.get_raw()``)
7680 :returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle to the task of computing computation's results
7781 """
7882
@@ -84,26 +88,29 @@ def run_computation(self, computation):
8488 self .project_key , self .dataset_name , self .worksheet_id ),
8589 body = computation .get_raw ()
8690 )
87- return DSSFuture (self .client , future_response .get ("jobId" , None ), future_response )
91+ future = DSSFuture (self .client , future_response .get (
92+ "jobId" , None ), future_response )
93+ return future .wait_for_result () if wait else future
8894
8995
90- class DSSStatisticsWorksheetSettings (object ):
96+ class DSSStatisticsWorksheetSettings (DSSInternalDict ):
9197 def __init__ (self , client , project_key , dataset_name , worksheet_id , worksheet_definition ):
98+ super (DSSStatisticsWorksheetSettings ,
99+ self ).__init__ (worksheet_definition )
92100 self .client = client
93101 self .project_key = project_key
94102 self .dataset_name = dataset_name
95103 self .worksheet_id = worksheet_id
96- self ._worksheet_definition = worksheet_definition
97104
98105 def add_card (self , card ):
99106 """
100107 Adds a new card to the worksheet.
101108
102109 :param card: card to be added
103- :type card: :class:`DSSStatisticsCardSettings` or dict
110+ :type card: :class:`DSSStatisticsCardSettings` or dict (obtained from ``DSSStatisticsCardSettings.get_raw()``)
104111 """
105112 card = DSSStatisticsCardSettings ._from_card_or_dict (self .client , card )
106- self ._worksheet_definition ['rootCard' ]['cards' ].append (card .get_raw ())
113+ self ._internal_dict ['rootCard' ]['cards' ].append (card .get_raw ())
107114
108115 def list_cards (self ):
109116 """
@@ -112,40 +119,59 @@ def list_cards(self):
112119 :rtype: list of :class:`DSSStatisticsCardSettings`
113120 """
114121 return [DSSStatisticsCardSettings (self .client , card_definition )
115- for card_definition in self ._worksheet_definition ['rootCard' ]['cards' ]]
122+ for card_definition in self ._internal_dict ['rootCard' ]['cards' ]]
116123
117124 def get_raw (self ):
118125 """
119126 Gets a reference to the raw settings of the worksheet.
120127
121128 :rtype: dict
122129 """
123- return self ._worksheet_definition
130+ return self ._internal_dict
131+
132+ def set_sampling_settings (self , selection ):
133+ """
134+ Sets the sampling settings of the worksheet
135+
136+ :type card: :class:`DSSDatasetSelectionBuilder` or dict (obtained from ``get_raw_sampling_selection()``)
137+ """
138+ raw_selection = selection .build () if isinstance (
139+ selection , DSSDatasetSelectionBuilder ) else selection
140+ self ._internal_dict ['dataSpec' ]['datasetSelection' ] = raw_selection
141+
142+ def get_raw_sampling_settings (self ):
143+ """
144+ Gets a reference to the raw sampling settings of the worksheet.
145+
146+ :rtype: dict
147+ """
148+ return self ._internal_dict ['dataSpec' ]['datasetSelection' ]
124149
125150 def save (self ):
126151 """
127152 Saves the settings to DSS
128153 """
129- self ._worksheet_definition = self .client ._perform_json (
154+ self ._internal_dict = self .client ._perform_json (
130155 "PUT" ,
131156 "/projects/%s/datasets/%s/statistics/worksheets/%s" % (
132157 self .project_key , self .dataset_name , self .worksheet_id ),
133- body = self ._worksheet_definition
158+ body = self ._internal_dict
134159 )
135160
136161
137- class DSSStatisticsCardSettings (object ):
162+ class DSSStatisticsCardSettings (DSSInternalDict ):
138163 def __init__ (self , client , card_definition ):
164+ super (DSSStatisticsCardSettings , self ).__init__ (card_definition )
139165 self .client = client
140- self ._card_definition = card_definition
166+ self ._internal_dict = card_definition
141167
142168 def get_raw (self ):
143169 """
144170 Gets a reference to the raw settings of the card.
145171
146172 :rtype: dict
147173 """
148- return self ._card_definition
174+ return self ._internal_dict
149175
150176 def compile (self ):
151177 """
@@ -154,7 +180,7 @@ def compile(self):
154180 :rtype: DSSStatisticsComputationSettings
155181 """
156182 computation_json = self .client ._perform_json (
157- "POST" , "/statistics/cards/compile" , body = self ._card_definition
183+ "POST" , "/statistics/cards/compile" , body = self ._internal_dict
158184 )
159185 return DSSStatisticsComputationSettings (computation_json )
160186
@@ -165,17 +191,19 @@ def _from_card_or_dict(client, card_or_dict):
165191 return DSSStatisticsCardSettings (client , card_or_dict )
166192
167193
168- class DSSStatisticsComputationSettings (object ):
194+ class DSSStatisticsComputationSettings (DSSInternalDict ):
169195 def __init__ (self , computation_definition ):
170- self ._computation_definition = computation_definition
196+ super (DSSStatisticsComputationSettings ,
197+ self ).__init__ (computation_definition )
198+ self ._internal_dict = computation_definition
171199
172200 def get_raw (self ):
173201 """
174202 Gets the raw settings of the computation.
175203
176204 :rtype: dict
177205 """
178- return self ._computation_definition
206+ return self ._internal_dict
179207
180208 @staticmethod
181209 def _from_computation_or_dict (computation_or_dict ):
0 commit comments