1
1
# (C) 2021 GoodData Corporation
2
2
from __future__ import annotations
3
3
4
- from typing import Optional , Union
4
+ from typing import Callable , Optional , Union
5
5
6
6
import pandas
7
7
from gooddata_api_client import models
8
8
from gooddata_sdk import (
9
9
Attribute ,
10
10
BareExecutionResponse ,
11
+ Execution ,
11
12
ExecutionDefinition ,
12
13
Filter ,
13
14
GoodDataSdk ,
@@ -68,19 +69,25 @@ def __init__(self, sdk: GoodDataSdk, workspace_id: str) -> None:
68
69
self ._workspace_id = workspace_id
69
70
70
71
def indexed (
71
- self , index_by : IndexDef , columns : ColumnsDef , filter_by : Optional [Union [Filter , list [Filter ]]] = None
72
+ self ,
73
+ index_by : IndexDef ,
74
+ columns : ColumnsDef ,
75
+ filter_by : Optional [Union [Filter , list [Filter ]]] = None ,
76
+ on_execution_submitted : Optional [Callable [[Execution ], None ]] = None ,
72
77
) -> pandas .DataFrame :
73
78
"""
74
79
Creates a data frame indexed by values of the label. The data frame columns will be created from either
75
80
metrics or other label values.
76
81
77
- Note that depending on composition of the labels, the DataFrame's index may or may not be unique.
82
+ Note that depending on the composition of the labels, the DataFrame's index may or may not be unique.
78
83
79
84
Args:
80
85
index_by (IndexDef): One or more labels to index by.
81
86
columns (ColumnsDef): Dictionary mapping column name to its definition.
82
87
filter_by (Optional[Union[Filter, list[Filter]]]):
83
88
Optional filters to apply during computation on the server.
89
+ on_execution_submitted (Optional[Callable[[Execution], None]]): Callback to call when the execution was
90
+ submitted to the backend.
84
91
85
92
Returns:
86
93
pandas.DataFrame: A DataFrame instance.
@@ -91,14 +98,18 @@ def indexed(
91
98
columns = columns ,
92
99
index_by = index_by ,
93
100
filter_by = filter_by ,
101
+ on_execution_submitted = on_execution_submitted ,
94
102
)
95
103
96
104
_idx = make_pandas_index (index )
97
105
98
106
return pandas .DataFrame (data = data , index = _idx )
99
107
100
108
def not_indexed (
101
- self , columns : ColumnsDef , filter_by : Optional [Union [Filter , list [Filter ]]] = None
109
+ self ,
110
+ columns : ColumnsDef ,
111
+ filter_by : Optional [Union [Filter , list [Filter ]]] = None ,
112
+ on_execution_submitted : Optional [Callable [[Execution ], None ]] = None ,
102
113
) -> pandas .DataFrame :
103
114
"""
104
115
Creates a data frame with columns created from metrics and or labels.
@@ -107,28 +118,42 @@ def not_indexed(
107
118
columns (ColumnsDef): Dictionary mapping column name to its definition.
108
119
filter_by (Optional[Union[Filter, list[Filter]]]): Optionally specify filters to apply during
109
120
computation on the server.
121
+ on_execution_submitted (Optional[Callable[[Execution], None]]): Callback to call when the execution was
122
+ submitted to the backend.
110
123
111
124
Returns:
112
125
pandas.DataFrame: A DataFrame instance.
113
126
"""
114
127
115
- data , _ = compute_and_extract (self ._sdk , self ._workspace_id , columns = columns , filter_by = filter_by )
128
+ data , _ = compute_and_extract (
129
+ self ._sdk ,
130
+ self ._workspace_id ,
131
+ columns = columns ,
132
+ filter_by = filter_by ,
133
+ on_execution_submitted = on_execution_submitted ,
134
+ )
116
135
117
136
return pandas .DataFrame (data = data )
118
137
119
138
def for_items (
120
- self , items : ColumnsDef , filter_by : Optional [Union [Filter , list [Filter ]]] = None , auto_index : bool = True
139
+ self ,
140
+ items : ColumnsDef ,
141
+ filter_by : Optional [Union [Filter , list [Filter ]]] = None ,
142
+ auto_index : bool = True ,
143
+ on_execution_submitted : Optional [Callable [[Execution ], None ]] = None ,
121
144
) -> pandas .DataFrame :
122
145
"""
123
146
Creates a data frame for named items. This is a convenience method that will create DataFrame with or
124
- without index based on the context of the items that you pass.
147
+ without an index based on the context of the items that you pass.
125
148
126
149
Args:
127
150
items (ColumnsDef): Dictionary mapping item name to its definition.
128
151
filter_by (Optional[Union[Filter, list[Filter]]]): Optionally specify filters to apply during computation
129
152
on the server.
130
153
auto_index (bool): Default True. Enables creation of DataFrame with index depending on the contents
131
154
of the items.
155
+ on_execution_submitted (Optional[Callable[[Execution], None]]): Callback to call when the execution was
156
+ submitted to the backend.
132
157
133
158
Returns:
134
159
pandas.DataFrame: A DataFrame instance.
@@ -157,16 +182,24 @@ def for_items(
157
182
index_by = resolved_attr_cols ,
158
183
columns = resolved_measure_cols ,
159
184
filter_by = filter_by ,
185
+ on_execution_submitted = on_execution_submitted ,
160
186
)
161
187
162
- def for_visualization (self , visualization_id : str , auto_index : bool = True ) -> pandas .DataFrame :
188
+ def for_visualization (
189
+ self ,
190
+ visualization_id : str ,
191
+ auto_index : bool = True ,
192
+ on_execution_submitted : Optional [Callable [[Execution ], None ]] = None ,
193
+ ) -> pandas .DataFrame :
163
194
"""
164
195
Creates a data frame with columns based on the content of the visualization with the provided identifier.
165
196
166
197
Args:
167
198
visualization_id (str): Visualization identifier.
168
199
auto_index (bool): Default True. Enables creation of DataFrame with index depending on the contents
169
200
of the visualization.
201
+ on_execution_submitted (Optional[Callable[[Execution], None]]): Callback to call when the execution was
202
+ submitted to the backend.
170
203
171
204
Returns:
172
205
pandas.DataFrame: A DataFrame instance.
@@ -181,22 +214,31 @@ def for_visualization(self, visualization_id: str, auto_index: bool = True) -> p
181
214
** {naming .col_name_for_metric (m ): m .as_computable () for m in visualization .metrics },
182
215
}
183
216
184
- return self .for_items (columns , filter_by = filter_by , auto_index = auto_index )
217
+ return self .for_items (
218
+ columns , filter_by = filter_by , auto_index = auto_index , on_execution_submitted = on_execution_submitted
219
+ )
185
220
186
221
def for_created_visualization (
187
- self , created_visualizations_response : dict
222
+ self ,
223
+ created_visualizations_response : dict ,
224
+ on_execution_submitted : Optional [Callable [[Execution ], None ]] = None ,
188
225
) -> tuple [pandas .DataFrame , DataFrameMetadata ]:
189
226
"""
190
227
Creates a data frame using a created visualization.
191
228
192
229
Args:
193
230
created_visualizations_response (dict): Created visualization response.
231
+ on_execution_submitted (Optional[Callable[[Execution], None]]): Callback to call when the execution was
232
+ submitted to the backend.
194
233
195
234
Returns:
196
235
pandas.DataFrame: A DataFrame instance.
197
236
"""
198
237
execution_definition = self ._sdk .compute .build_exec_def_from_chat_result (created_visualizations_response )
199
- return self .for_exec_def (exec_def = execution_definition )
238
+ return self .for_exec_def (
239
+ exec_def = execution_definition ,
240
+ on_execution_submitted = on_execution_submitted ,
241
+ )
200
242
201
243
def result_cache_metadata_for_exec_result_id (self , result_id : str ) -> ResultCacheMetadata :
202
244
"""
@@ -217,6 +259,7 @@ def for_exec_def(
217
259
result_size_dimensions_limits : ResultSizeDimensions = (),
218
260
result_size_bytes_limit : Optional [int ] = None ,
219
261
page_size : int = _DEFAULT_PAGE_SIZE ,
262
+ on_execution_submitted : Optional [Callable [[Execution ], None ]] = None ,
220
263
) -> tuple [pandas .DataFrame , DataFrameMetadata ]:
221
264
"""
222
265
Creates a data frame using an execution definition.
@@ -247,6 +290,8 @@ def for_exec_def(
247
290
result_size_dimensions_limits (ResultSizeDimensions): A tuple containing maximum size of result dimensions.
248
291
result_size_bytes_limit (Optional[int]): Maximum size of result in bytes.
249
292
page_size (int): Number of records per page.
293
+ on_execution_submitted (Optional[Callable[[Execution], None]]): Callback to call when the execution was
294
+ submitted to the backend.
250
295
251
296
Returns:
252
297
Tuple[pandas.DataFrame, DataFrameMetadata]: Tuple holding DataFrame and DataFrame metadata.
@@ -257,6 +302,9 @@ def for_exec_def(
257
302
execution = self ._sdk .compute .for_exec_def (workspace_id = self ._workspace_id , exec_def = exec_def )
258
303
result_cache_metadata = self .result_cache_metadata_for_exec_result_id (execution .result_id )
259
304
305
+ if on_execution_submitted is not None :
306
+ on_execution_submitted (execution )
307
+
260
308
return convert_execution_response_to_dataframe (
261
309
execution_response = execution .bare_exec_response ,
262
310
result_cache_metadata = result_cache_metadata ,
@@ -302,7 +350,7 @@ def for_exec_result_id(
302
350
label_overrides (Optional[LabelOverrides]): Label overrides for metrics and attributes.
303
351
result_cache_metadata (Optional[ResultCacheMetadata]): Cache metadata for the execution result.
304
352
result_size_dimensions_limits (ResultSizeDimensions): A tuple containing maximum size of result dimensions.
305
- result_size_bytes_limit (Optional[int]): Maximum size of result in bytes.
353
+ result_size_bytes_limit (Optional[int]): Maximum size of the result in bytes.
306
354
use_local_ids_in_headers (bool): Use local identifier in headers.
307
355
use_primary_labels_in_attributes (bool): Use primary labels in attributes.
308
356
page_size (int): Number of records per page.
0 commit comments