-
Notifications
You must be signed in to change notification settings - Fork 1
/
superset.py
269 lines (223 loc) · 12.1 KB
/
superset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import json
from bs4 import BeautifulSoup
import requests
from requests import Session
direct_mapping_types = ['boolean', 'integer', 'boolean', 'date']
datetime_format = {
}
# Logic taken from https://github.com/metriql/metriql-superset/blob/main/metriql2superset/superset.py
# Issue https://github.com/apache/superset/issues/16398
class DatabaseOperation:
superset_url: str
session: Session
def __init__(self, superset_url, username, password) -> None:
self.superset_url = superset_url
self.username = username
self.password = password
# set up session for auth
self.session = requests.Session()
self.session.headers["Referer"] = self.superset_url
## TODO: check hostname
if '.app.preset.io/' in superset_url.lower():
self.login_preset(username, password)
else:
self.login_self_hosted_superset(username, password)
def login_preset(self, token, secret):
preset_auth = self.session.post('https://manage.app.preset.io/api/v1/auth/',
json={"name": token, "secret": secret})
if preset_auth.status_code != 200:
raise Exception("Unable to login: {}".format(preset_auth.text))
access_token = preset_auth.json().get('payload').get('access_token')
self.session.headers["Authorization"] = "Bearer {}".format(access_token)
def login_self_hosted_superset(self, username, password):
self.setup_access_token()
self.session.headers["X-CSRFToken"] = self.csrf_token
self.session.headers["X-Authorization"] = 'Bearer {}'.format(self.current_access_token)
# This is done so that we get the right cookie for the session
# The JWT token is not working for create data endpoint
self.session.post("{}/login/".format(self.superset_url), data={"username":username, "password":password})
# Superset Dataset update API doesn't support JWT so we don't use this function at the moment
def setup_access_token(self):
r = requests.post("{}/api/v1/security/login".format(self.superset_url), json={
"password": self.password,
"provider": "db",
"refresh": True,
"username": self.username
})
if r.status_code != 200:
raise Exception("Unable to authenticate: {}".format(r.text))
info = r.json()
self.current_access_token = info.get('access_token')
self.refresh_token = info.get('refresh_token')
self.csrf_token = self._create_csrf_token()
def _refresh_token(self):
r = requests.post("{}/api/v1/security/refresh".format(self.superset_url), self.refresh_token)
if r.status_code != 200:
raise Exception("Unable to authenticate: {}".format(r.text))
info = r.json()
self.current_access_token = info.get('access_token')
def _create_csrf_token(self):
token_request = requests.get("{}/api/v1/security/csrf_token".format(self.superset_url),
headers={'Authorization': 'Bearer {}'.format(self.current_access_token)})
if token_request.status_code != 200:
raise Exception("Unable to get CSRF token: {}".format(token_request.text))
return token_request.json().get('result')
def create(self, metriql_url, database_name):
raise Exception("Operation is not implemented. See: https://github.com/apache/superset/issues/16398")
protocol = "https"
uri = "trino://TODO:TODO@{}:443/metriql?protocol={}".format(metriql_url, protocol)
extra = {"engine_params": {"connect_args": {"http_scheme": "https"}}}
r = self.session.post("{}/api/v1/database".format(self.superset_url),
json={
"allow_csv_upload": False,
"allow_ctas": True,
"allow_cvas": False,
"allow_dml": False,
"allow_multi_schema_metadata_fetch": True,
"allow_run_async": True,
"extra": json.dumps(extra),
"extra_json": extra,
"database_name": database_name,
"expose_in_sqllab": True,
"sqlalchemy_uri": uri
})
if r.status_code == 401:
raise Exception("Invalid credentials")
elif r.status_code != 200:
raise Exception("Unable to perform operation: {}".format(r.text))
return self.list()
@staticmethod
def _get_column_datatype(field_type):
if field_type is None:
return "string"
if field_type in ['string', 'boolean']:
return field_type
if field_type in ['double', 'long', 'integer']:
return 'numeric'
if field_type in ['timestamp', 'date', 'time']:
return 'datetime'
raise Exception("Unknown type {}".format(field_type))
@staticmethod
def get_python_date_format(field_type):
return None
@staticmethod
def _build_metrics(measures, existing_metrics):
existing_metric_lookup = {}
for metric in existing_metrics:
existing_metric_lookup[metric.get('metric_name')] = metric.get('id')
return [{
"d3format": ((measure.get('reportOptions') or {}).get('superset') or {}).get('d3_format'),
"description": measure.get('description'),
"expression": '"{}"'.format(name.replace('"', '""')),
"id": name,
"metric_name": name,
"verbose_name": measure.get('label'),
} for (name, (measure, relation)) in measures.items()]
@staticmethod
def _build_columns(dimensions, measures, mappings, existing_columns):
existing_column_lookup = {}
for metric in existing_columns:
existing_column_lookup[metric.get('column_name')] = metric.get('id')
columns = []
for name, (dimension, relation) in dimensions.items():
column_id = existing_column_lookup.get(name)
columns.append(DatabaseOperation._build_column(column_id, name, dimension, True, mappings))
return columns
@staticmethod
def _build_column(column_id, name, field, is_dimension, mappings):
return {
"column_name": name,
"description": field.get('description'),
"filterable": True,
"groupby": is_dimension,
"id": name,
"is_active": not field.get('hidden'),
"is_dttm": mappings.get('event_timestamp') == field.get('name') if is_dimension else False,
"python_date_format": DatabaseOperation.get_python_date_format(field.get('fieldType')),
"type": DatabaseOperation._get_column_datatype(field.get('fieldType')),
"verbose_name": field.get('label')
}
def sync(self, database_id, metadata):
superset_dataset_requests = self.session.get("{}/api/v1/dataset".format(self.superset_url),
params={"page_size": "100000",
"keys": ['sql', 'table_name', 'schema', 'id', 'metrics',
'columns'],
"filters": [{"col": "database", "opr": "rel_m_m",
"value": database_id}]})
if superset_dataset_requests.status_code == 401:
raise Exception("Invalid credentials")
elif superset_dataset_requests.status_code != 200:
raise Exception("Unable to perform operation: {}".format(superset_dataset_requests.text))
superset_datasets = superset_dataset_requests.json().get('result')
metriql_datasets = metadata.get_datasets()
for dataset in metriql_datasets:
table_schema = "public" if dataset.get('category') is None else dataset.get('category')
table_name = dataset.get('name')
mappings = dataset.get('mappings')
# find existing dataset
existing_dataset = next(
filter(lambda db: db.get('schema') == table_schema and db.get('table_name') == table_name,
superset_datasets), None)
existing_dataset_id = existing_dataset.get('id') if existing_dataset is not None else None
existing_columns = []
existing_metrics = []
new_dataset_count = 0
if existing_dataset is not None:
superset_dataset_content = self.session.get(
"{}/api/v1/dataset/{}".format(self.superset_url, existing_dataset_id),
params={
"columns": [
"columns", "metrics"
],
"keys": [
"show_columns"
]
})
if superset_dataset_content.status_code != 200:
raise Exception("Unable to get dataset {}.{}: {}".format(table_schema, table_name,
superset_dataset_content.text))
content = superset_dataset_content.json().get('result')
existing_columns = content.get('columns')
existing_metrics = content.get('metrics')
existing_dataset_id = content.get('id')
else:
new_dataset_count += 1
dimensions = metadata.get_dimensions(table_name)
measures = metadata.get_measures(table_name)
metrics = DatabaseOperation._build_metrics(measures, existing_metrics)
columns = DatabaseOperation._build_columns(dimensions, measures, mappings, existing_columns)
if existing_dataset is None:
created_dataset_request = self.session.post(
"{}/api/v1/dataset".format(self.superset_url), json={"database": database_id,
"schema": table_schema,
"table_name": table_name,
# "owners": [
# 0
# ],
})
if created_dataset_request.status_code not in [201, 200]:
raise Exception("Unable to create dataset: {}".format(created_dataset_request.text))
existing_dataset_id = created_dataset_request.json().get('id')
updated_dataset_request = self.session.post(
"{}/datasource/save".format(self.superset_url), files=[], data={"data": json.dumps({
"id": existing_dataset_id,
"columns": columns,
# "datasource_type": "table",
"type": "table",
"database": {"id": database_id},
"description": dataset.get('description'),
"filter_select_enabled": True,
"is_sqllab_view": False,
"main_dttm_col": mappings.get('event_timestamp'),
"metrics": metrics,
# "owners": [
# 0
# ],
"schema": table_schema,
"table_name": table_name
})})
if updated_dataset_request.status_code != 200:
raise Exception("Unable to update dataset: {}".format(updated_dataset_request.text))
print(dataset.get('name'))
print("Successfully synchronized existing {} datasets, created {} datasets".format(
len(metriql_datasets) - new_dataset_count, new_dataset_count))