dagster-sigma 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dagster_sigma-0.0.1/LICENSE +201 -0
- dagster_sigma-0.0.1/MANIFEST.in +3 -0
- dagster_sigma-0.0.1/PKG-INFO +19 -0
- dagster_sigma-0.0.1/README.md +3 -0
- dagster_sigma-0.0.1/dagster_sigma/__init__.py +16 -0
- dagster_sigma-0.0.1/dagster_sigma/py.typed +1 -0
- dagster_sigma-0.0.1/dagster_sigma/resource.py +295 -0
- dagster_sigma-0.0.1/dagster_sigma/translator.py +118 -0
- dagster_sigma-0.0.1/dagster_sigma.egg-info/PKG-INFO +19 -0
- dagster_sigma-0.0.1/dagster_sigma.egg-info/SOURCES.txt +15 -0
- dagster_sigma-0.0.1/dagster_sigma.egg-info/dependency_links.txt +1 -0
- dagster_sigma-0.0.1/dagster_sigma.egg-info/not-zip-safe +1 -0
- dagster_sigma-0.0.1/dagster_sigma.egg-info/requires.txt +2 -0
- dagster_sigma-0.0.1/dagster_sigma.egg-info/top_level.txt +1 -0
- dagster_sigma-0.0.1/setup.cfg +14 -0
- dagster_sigma-0.0.1/setup.py +45 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2023 Dagster Labs, Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: dagster_sigma
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Build assets representing Sigma dashboards.
|
|
5
|
+
Home-page: https://github.com/dagster-io/dagster/tree/master/python_modules/libraries/dagster-sigma
|
|
6
|
+
Author: Dagster Labs
|
|
7
|
+
Author-email: hello@dagsterlabs.com
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Python: >=3.8,<3.13
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: dagster
|
|
19
|
+
Requires-Dist: sqlglot
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from dagster._core.libraries import DagsterLibraryRegistry
|
|
2
|
+
|
|
3
|
+
from dagster_sigma.resource import (
|
|
4
|
+
SigmaBaseUrl as SigmaBaseUrl,
|
|
5
|
+
SigmaOrganization as SigmaOrganization,
|
|
6
|
+
)
|
|
7
|
+
from dagster_sigma.translator import (
|
|
8
|
+
DagsterSigmaTranslator as DagsterSigmaTranslator,
|
|
9
|
+
SigmaDataset as SigmaDataset,
|
|
10
|
+
SigmaWorkbook as SigmaWorkbook,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
# Move back to version.py and edit setup.py once we are ready to publish.
|
|
14
|
+
__version__ = "0.0.1"
|
|
15
|
+
|
|
16
|
+
DagsterLibraryRegistry.register("dagster-sigma", __version__)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
partial
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
from collections import defaultdict
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from typing import Any, Dict, List, Mapping, Optional, Type
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
from dagster import ConfigurableResource
|
|
7
|
+
from dagster._core.definitions.definitions_class import Definitions
|
|
8
|
+
from dagster._core.definitions.definitions_loader import DefinitionsLoadContext, DefinitionsLoadType
|
|
9
|
+
from dagster._serdes.serdes import deserialize_value, serialize_value
|
|
10
|
+
from dagster._utils.cached_method import cached_method
|
|
11
|
+
from pydantic import Field, PrivateAttr
|
|
12
|
+
from sqlglot import exp, parse_one
|
|
13
|
+
|
|
14
|
+
from dagster_sigma.translator import (
|
|
15
|
+
DagsterSigmaTranslator,
|
|
16
|
+
SigmaDataset,
|
|
17
|
+
SigmaOrganizationData,
|
|
18
|
+
SigmaWorkbook,
|
|
19
|
+
_inode_from_url,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
SIGMA_PARTNER_ID_TAG = {"X-Sigma-Partner-Id": "dagster"}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class SigmaBaseUrl(str, Enum):
|
|
26
|
+
"""Enumeration of Sigma API base URLs for different cloud providers.
|
|
27
|
+
|
|
28
|
+
https://help.sigmacomputing.com/reference/get-started-sigma-api#identify-your-api-request-url
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
AWS_US = "https://aws-api.sigmacomputing.com"
|
|
32
|
+
AWS_CANADA = "https://api.ca.aws.sigmacomputing.com"
|
|
33
|
+
AWS_EUROPE = "https://api.eu.aws.sigmacomputing.com"
|
|
34
|
+
AWS_UK = "https://api.uk.aws.sigmacomputing.com"
|
|
35
|
+
AZURE_US = "https://api.us.azure.sigmacomputing.com"
|
|
36
|
+
GCP = "https://api.sigmacomputing.com"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class SigmaOrganization(ConfigurableResource):
|
|
40
|
+
"""Represents a workspace in Sigma and provides utilities
|
|
41
|
+
to interact with the Sigma API.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
base_url: str = Field(
|
|
45
|
+
...,
|
|
46
|
+
description=(
|
|
47
|
+
"Base URL for the cloud type of your Sigma organization, found under the Administration -> Account -> Site settings."
|
|
48
|
+
" See https://help.sigmacomputing.com/reference/get-started-sigma-api#identify-your-api-request-url for more information."
|
|
49
|
+
),
|
|
50
|
+
)
|
|
51
|
+
client_id: str = Field(..., description="A client ID with access to the Sigma API.")
|
|
52
|
+
client_secret: str = Field(..., description="A client secret with access to the Sigma API.")
|
|
53
|
+
|
|
54
|
+
_api_token: Optional[str] = PrivateAttr(None)
|
|
55
|
+
|
|
56
|
+
def _fetch_api_token(self) -> str:
|
|
57
|
+
response = requests.post(
|
|
58
|
+
url=f"{self.base_url}/v2/auth/token",
|
|
59
|
+
headers={
|
|
60
|
+
"Accept": "application/json",
|
|
61
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
62
|
+
**SIGMA_PARTNER_ID_TAG,
|
|
63
|
+
},
|
|
64
|
+
data={
|
|
65
|
+
"grant_type": "client_credentials",
|
|
66
|
+
"client_id": self.client_id,
|
|
67
|
+
"client_secret": self.client_secret,
|
|
68
|
+
},
|
|
69
|
+
)
|
|
70
|
+
response.raise_for_status()
|
|
71
|
+
return response.json()["access_token"]
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def api_token(self) -> str:
|
|
75
|
+
if self._api_token is None:
|
|
76
|
+
self._api_token = self._fetch_api_token()
|
|
77
|
+
|
|
78
|
+
return self._api_token
|
|
79
|
+
|
|
80
|
+
def fetch_json(self, endpoint: str, method: str = "GET") -> Dict[str, Any]:
|
|
81
|
+
response = requests.request(
|
|
82
|
+
method=method,
|
|
83
|
+
url=f"{self.base_url}/v2/{endpoint}",
|
|
84
|
+
headers={
|
|
85
|
+
"Accept": "application/json",
|
|
86
|
+
"Authorization": f"Bearer {self.api_token}",
|
|
87
|
+
**SIGMA_PARTNER_ID_TAG,
|
|
88
|
+
},
|
|
89
|
+
)
|
|
90
|
+
response.raise_for_status()
|
|
91
|
+
return response.json()
|
|
92
|
+
|
|
93
|
+
@cached_method
|
|
94
|
+
def fetch_workbooks(self) -> List[Dict[str, Any]]:
|
|
95
|
+
return self.fetch_json("workbooks")["entries"]
|
|
96
|
+
|
|
97
|
+
@cached_method
|
|
98
|
+
def fetch_datasets(self) -> List[Dict[str, Any]]:
|
|
99
|
+
return self.fetch_json("datasets")["entries"]
|
|
100
|
+
|
|
101
|
+
@cached_method
|
|
102
|
+
def fetch_pages_for_workbook(self, workbook_id: str) -> List[Dict[str, Any]]:
|
|
103
|
+
return self.fetch_json(f"workbooks/{workbook_id}/pages")["entries"]
|
|
104
|
+
|
|
105
|
+
@cached_method
|
|
106
|
+
def fetch_elements_for_page(self, workbook_id: str, page_id: str) -> List[Dict[str, Any]]:
|
|
107
|
+
return self.fetch_json(f"workbooks/{workbook_id}/pages/{page_id}/elements")["entries"]
|
|
108
|
+
|
|
109
|
+
@cached_method
|
|
110
|
+
def fetch_lineage_for_element(self, workbook_id: str, element_id: str) -> Dict[str, Any]:
|
|
111
|
+
return self.fetch_json(f"workbooks/{workbook_id}/lineage/elements/{element_id}")
|
|
112
|
+
|
|
113
|
+
@cached_method
|
|
114
|
+
def fetch_columns_for_element(self, workbook_id: str, element_id: str) -> List[Dict[str, Any]]:
|
|
115
|
+
return self.fetch_json(f"workbooks/{workbook_id}/elements/{element_id}/columns")["entries"]
|
|
116
|
+
|
|
117
|
+
@cached_method
|
|
118
|
+
def fetch_queries_for_workbook(self, workbook_id: str) -> List[Dict[str, Any]]:
|
|
119
|
+
return self.fetch_json(f"workbooks/{workbook_id}/queries")["entries"]
|
|
120
|
+
|
|
121
|
+
@cached_method
|
|
122
|
+
def fetch_dataset_upstreams_by_inode(self) -> Mapping[str, AbstractSet[str]]:
|
|
123
|
+
"""Builds a mapping of dataset inodes to the upstream inputs they depend on.
|
|
124
|
+
Sigma does not expose this information directly, so we have to infer it from
|
|
125
|
+
the lineage of workbooks and the workbook queries.
|
|
126
|
+
"""
|
|
127
|
+
deps_by_dataset_inode = defaultdict(set)
|
|
128
|
+
|
|
129
|
+
raw_workbooks = self.fetch_workbooks()
|
|
130
|
+
|
|
131
|
+
# We first figure out which tables/visualizations ("elements") in each workbook
|
|
132
|
+
# depend on which datasets.
|
|
133
|
+
for workbook in raw_workbooks:
|
|
134
|
+
queries = self.fetch_queries_for_workbook(workbook["workbookId"])
|
|
135
|
+
queries_by_element_id = defaultdict(list)
|
|
136
|
+
for query in queries:
|
|
137
|
+
queries_by_element_id[query["elementId"]].append(query)
|
|
138
|
+
|
|
139
|
+
pages = self.fetch_pages_for_workbook(workbook["workbookId"])
|
|
140
|
+
|
|
141
|
+
for page in pages:
|
|
142
|
+
elements = self.fetch_elements_for_page(workbook["workbookId"], page["pageId"])
|
|
143
|
+
for element in elements:
|
|
144
|
+
# We extract the list of dataset dependencies from the lineage of each element
|
|
145
|
+
# If there is a single dataset dependency, we can then know the queries for that element
|
|
146
|
+
# are associated with that dataset
|
|
147
|
+
lineage = self.fetch_lineage_for_element(
|
|
148
|
+
workbook["workbookId"], element["elementId"]
|
|
149
|
+
)
|
|
150
|
+
dataset_dependencies = [
|
|
151
|
+
dep
|
|
152
|
+
for dep in lineage["dependencies"].values()
|
|
153
|
+
if dep.get("type") == "dataset"
|
|
154
|
+
]
|
|
155
|
+
if len(dataset_dependencies) != 1:
|
|
156
|
+
continue
|
|
157
|
+
|
|
158
|
+
inode = dataset_dependencies[0]["nodeId"]
|
|
159
|
+
for query in queries_by_element_id[element["elementId"]]:
|
|
160
|
+
# Use sqlglot to extract the tables used in each query and add them to the dataset's
|
|
161
|
+
# list of dependencies
|
|
162
|
+
table_deps = set(
|
|
163
|
+
[
|
|
164
|
+
f"{table.catalog}.{table.db}.{table.this}"
|
|
165
|
+
for table in list(parse_one(query["sql"]).find_all(exp.Table))
|
|
166
|
+
if table.catalog
|
|
167
|
+
]
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
deps_by_dataset_inode[inode] = deps_by_dataset_inode[inode].union(
|
|
171
|
+
table_deps
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
return deps_by_dataset_inode
|
|
175
|
+
|
|
176
|
+
@cached_method
|
|
177
|
+
def fetch_dataset_columns_by_inode(self) -> Mapping[str, AbstractSet[str]]:
|
|
178
|
+
"""Builds a mapping of dataset inodes to the columns they contain. Note that
|
|
179
|
+
this is a partial list and will only include columns which are referenced in
|
|
180
|
+
workbooks, since Sigma does not expose a direct API for querying dataset columns.
|
|
181
|
+
"""
|
|
182
|
+
columns_by_dataset_inode = defaultdict(set)
|
|
183
|
+
|
|
184
|
+
for workbook in self.fetch_workbooks():
|
|
185
|
+
pages = self.fetch_pages_for_workbook(workbook["workbookId"])
|
|
186
|
+
for page in pages:
|
|
187
|
+
elements = self.fetch_elements_for_page(workbook["workbookId"], page["pageId"])
|
|
188
|
+
for element in elements:
|
|
189
|
+
# We can't query the list of columns in a dataset directly, so we have to build a partial
|
|
190
|
+
# list from the columns which appear in any workbook.
|
|
191
|
+
columns = self.fetch_columns_for_element(
|
|
192
|
+
workbook["workbookId"], element["elementId"]
|
|
193
|
+
)
|
|
194
|
+
for column in columns:
|
|
195
|
+
split = column["columnId"].split("/")
|
|
196
|
+
if len(split) == 2:
|
|
197
|
+
inode, column_name = split
|
|
198
|
+
columns_by_dataset_inode[inode].add(column_name)
|
|
199
|
+
|
|
200
|
+
return columns_by_dataset_inode
|
|
201
|
+
|
|
202
|
+
@cached_method
|
|
203
|
+
def build_organization_data(self) -> SigmaOrganizationData:
|
|
204
|
+
"""Retrieves all workbooks and datasets in the Sigma organization and builds a
|
|
205
|
+
SigmaOrganizationData object representing the organization's assets.
|
|
206
|
+
"""
|
|
207
|
+
raw_workbooks = self.fetch_workbooks()
|
|
208
|
+
|
|
209
|
+
workbooks: List[SigmaWorkbook] = []
|
|
210
|
+
for workbook in raw_workbooks:
|
|
211
|
+
workbook_deps = set()
|
|
212
|
+
pages = self.fetch_pages_for_workbook(workbook["workbookId"])
|
|
213
|
+
for page in pages:
|
|
214
|
+
elements = self.fetch_elements_for_page(workbook["workbookId"], page["pageId"])
|
|
215
|
+
for element in elements:
|
|
216
|
+
# We extract the list of dataset dependencies from the lineage of each workbook.
|
|
217
|
+
lineage = self.fetch_lineage_for_element(
|
|
218
|
+
workbook["workbookId"], element["elementId"]
|
|
219
|
+
)
|
|
220
|
+
for inode, item in lineage["dependencies"].items():
|
|
221
|
+
if item.get("type") == "dataset":
|
|
222
|
+
workbook_deps.add(item["nodeId"])
|
|
223
|
+
|
|
224
|
+
workbooks.append(SigmaWorkbook(properties=workbook, datasets=workbook_deps))
|
|
225
|
+
|
|
226
|
+
datasets: List[SigmaDataset] = []
|
|
227
|
+
deps_by_dataset_inode = self.fetch_dataset_upstreams_by_inode()
|
|
228
|
+
columns_by_dataset_inode = self.fetch_dataset_columns_by_inode()
|
|
229
|
+
|
|
230
|
+
for dataset in self.fetch_datasets():
|
|
231
|
+
inode = _inode_from_url(dataset["url"])
|
|
232
|
+
datasets.append(
|
|
233
|
+
SigmaDataset(
|
|
234
|
+
properties=dataset,
|
|
235
|
+
columns=columns_by_dataset_inode.get(inode, set()),
|
|
236
|
+
inputs=deps_by_dataset_inode[inode],
|
|
237
|
+
)
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
return SigmaOrganizationData(workbooks=workbooks, datasets=datasets)
|
|
241
|
+
|
|
242
|
+
@property
|
|
243
|
+
def reconstruction_metadata_key(self) -> str:
|
|
244
|
+
return f"sigma_{self.client_id}"
|
|
245
|
+
|
|
246
|
+
def build_defs(
|
|
247
|
+
self,
|
|
248
|
+
context: DefinitionsLoadContext,
|
|
249
|
+
dagster_sigma_translator: Type[DagsterSigmaTranslator] = DagsterSigmaTranslator,
|
|
250
|
+
) -> Definitions:
|
|
251
|
+
"""Returns a Definitions object representing the Sigma content in the organization.
|
|
252
|
+
|
|
253
|
+
Args:
|
|
254
|
+
context (DefinitionsLoadContext): The context in which the definitions are being loaded.
|
|
255
|
+
dagster_sigma_translator (Type[DagsterSigmaTranslator]): The translator to use
|
|
256
|
+
to convert Sigma content into AssetSpecs. Defaults to DagsterSigmaTranslator.
|
|
257
|
+
|
|
258
|
+
Returns:
|
|
259
|
+
Definitions: The set of assets representing the Sigma content in the organization.
|
|
260
|
+
"""
|
|
261
|
+
# Attempt to load cached data from the context.
|
|
262
|
+
if (
|
|
263
|
+
context.load_type == DefinitionsLoadType.RECONSTRUCTION
|
|
264
|
+
and self.reconstruction_metadata_key in context.reconstruction_metadata
|
|
265
|
+
):
|
|
266
|
+
cached_data = context.reconstruction_metadata[self.reconstruction_metadata_key]
|
|
267
|
+
workbooks = [
|
|
268
|
+
deserialize_value(workbook, as_type=SigmaWorkbook)
|
|
269
|
+
for workbook in cached_data["workbooks"]
|
|
270
|
+
]
|
|
271
|
+
datasets = [
|
|
272
|
+
deserialize_value(dataset, as_type=SigmaDataset)
|
|
273
|
+
for dataset in cached_data["datasets"]
|
|
274
|
+
]
|
|
275
|
+
else:
|
|
276
|
+
organization_data = self.build_organization_data()
|
|
277
|
+
workbooks = organization_data.workbooks
|
|
278
|
+
datasets = organization_data.datasets
|
|
279
|
+
cached_data = {
|
|
280
|
+
"workbooks": [serialize_value(workbook) for workbook in workbooks],
|
|
281
|
+
"datasets": [serialize_value(dataset) for dataset in datasets],
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
org_data = SigmaOrganizationData(workbooks=workbooks, datasets=datasets)
|
|
285
|
+
|
|
286
|
+
translator = dagster_sigma_translator(context=org_data)
|
|
287
|
+
|
|
288
|
+
asset_specs = [
|
|
289
|
+
*[translator.get_workbook_spec(workbook) for workbook in org_data.workbooks],
|
|
290
|
+
*[translator.get_dataset_spec(dataset) for dataset in org_data.datasets],
|
|
291
|
+
]
|
|
292
|
+
|
|
293
|
+
return Definitions(assets=asset_specs).with_reconstruction_metadata(
|
|
294
|
+
{self.reconstruction_metadata_key: cached_data}
|
|
295
|
+
)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from typing import AbstractSet, Any, Dict, List
|
|
3
|
+
|
|
4
|
+
from dagster import AssetKey, AssetSpec, MetadataValue, TableSchema
|
|
5
|
+
from dagster._core.definitions.metadata.metadata_set import TableMetadataSet
|
|
6
|
+
from dagster._core.definitions.metadata.table import TableColumn
|
|
7
|
+
from dagster._core.definitions.tags import build_kind_tag
|
|
8
|
+
from dagster._record import record
|
|
9
|
+
from dagster._serdes.serdes import whitelist_for_serdes
|
|
10
|
+
from dagster._utils.cached_method import cached_method
|
|
11
|
+
from dagster._vendored.dateutil.parser import isoparse
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _clean_asset_name(name: str) -> str:
|
|
15
|
+
"""Cleans an input to be a valid Dagster asset name."""
|
|
16
|
+
return re.sub(r"[^A-Za-z0-9_]+", "_", name)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _inode_from_url(url: str) -> str:
|
|
20
|
+
"""Builds a Sigma internal inode value from a Sigma URL."""
|
|
21
|
+
return f'inode-{url.split("/")[-1]}'
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@whitelist_for_serdes
|
|
25
|
+
@record
|
|
26
|
+
class SigmaWorkbook:
|
|
27
|
+
"""Represents a Sigma workbook, a collection of visualizations and queries
|
|
28
|
+
for data exploration and analysis.
|
|
29
|
+
|
|
30
|
+
https://help.sigmacomputing.com/docs/workbooks
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
properties: Dict[str, Any]
|
|
34
|
+
datasets: AbstractSet[str]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@whitelist_for_serdes
|
|
38
|
+
@record
|
|
39
|
+
class SigmaDataset:
|
|
40
|
+
"""Represents a Sigma dataset, a centralized data definition which can
|
|
41
|
+
contain aggregations or other manipulations.
|
|
42
|
+
|
|
43
|
+
https://help.sigmacomputing.com/docs/datasets
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
properties: Dict[str, Any]
|
|
47
|
+
columns: AbstractSet[str]
|
|
48
|
+
inputs: AbstractSet[str]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@record
|
|
52
|
+
class SigmaOrganizationData:
|
|
53
|
+
workbooks: List[SigmaWorkbook]
|
|
54
|
+
datasets: List[SigmaDataset]
|
|
55
|
+
|
|
56
|
+
@cached_method
|
|
57
|
+
def get_datasets_by_inode(self) -> Dict[str, SigmaDataset]:
|
|
58
|
+
return {_inode_from_url(dataset.properties["url"]): dataset for dataset in self.datasets}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class DagsterSigmaTranslator:
|
|
62
|
+
"""Translator class which converts raw response data from the Sigma API into AssetSpecs.
|
|
63
|
+
Subclass this class to provide custom translation logic.
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
def __init__(self, context: SigmaOrganizationData):
|
|
67
|
+
self._context = context
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def organization_data(self) -> SigmaOrganizationData:
|
|
71
|
+
return self._context
|
|
72
|
+
|
|
73
|
+
def get_workbook_key(self, data: SigmaWorkbook) -> AssetKey:
|
|
74
|
+
return AssetKey(_clean_asset_name(data.properties["name"]))
|
|
75
|
+
|
|
76
|
+
def get_workbook_spec(self, data: SigmaWorkbook) -> AssetSpec:
|
|
77
|
+
metadata = {
|
|
78
|
+
"dagster_sigma/web_url": MetadataValue.url(data.properties["url"]),
|
|
79
|
+
"dagster_sigma/version": data.properties["latestVersion"],
|
|
80
|
+
"dagster_sigma/created_at": MetadataValue.timestamp(
|
|
81
|
+
isoparse(data.properties["createdAt"])
|
|
82
|
+
),
|
|
83
|
+
}
|
|
84
|
+
datasets = [self._context.get_datasets_by_inode()[inode] for inode in data.datasets]
|
|
85
|
+
return AssetSpec(
|
|
86
|
+
key=self.get_workbook_key(data),
|
|
87
|
+
metadata=metadata,
|
|
88
|
+
tags={
|
|
89
|
+
**build_kind_tag("sigma"),
|
|
90
|
+
},
|
|
91
|
+
deps={self.get_dataset_key(dataset) for dataset in datasets},
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
def get_dataset_key(self, data: SigmaDataset) -> AssetKey:
|
|
95
|
+
return AssetKey(_clean_asset_name(data.properties["name"]))
|
|
96
|
+
|
|
97
|
+
def get_dataset_spec(self, data: SigmaDataset) -> AssetSpec:
|
|
98
|
+
metadata = {
|
|
99
|
+
"dagster_sigma/web_url": MetadataValue.url(data.properties["url"]),
|
|
100
|
+
"dagster_sigma/created_at": MetadataValue.timestamp(
|
|
101
|
+
isoparse(data.properties["createdAt"])
|
|
102
|
+
),
|
|
103
|
+
**TableMetadataSet(
|
|
104
|
+
column_schema=TableSchema(
|
|
105
|
+
columns=[TableColumn(name=column_name) for column_name in sorted(data.columns)]
|
|
106
|
+
)
|
|
107
|
+
),
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return AssetSpec(
|
|
111
|
+
key=self.get_dataset_key(data),
|
|
112
|
+
metadata=metadata,
|
|
113
|
+
tags={
|
|
114
|
+
**build_kind_tag("sigma"),
|
|
115
|
+
},
|
|
116
|
+
deps={AssetKey(input_name.lower().split(".")) for input_name in data.inputs},
|
|
117
|
+
description=data.properties.get("description"),
|
|
118
|
+
)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: dagster_sigma
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Build assets representing Sigma dashboards.
|
|
5
|
+
Home-page: https://github.com/dagster-io/dagster/tree/master/python_modules/libraries/dagster-sigma
|
|
6
|
+
Author: Dagster Labs
|
|
7
|
+
Author-email: hello@dagsterlabs.com
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Python: >=3.8,<3.13
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: dagster
|
|
19
|
+
Requires-Dist: sqlglot
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
setup.cfg
|
|
5
|
+
setup.py
|
|
6
|
+
dagster_sigma/__init__.py
|
|
7
|
+
dagster_sigma/py.typed
|
|
8
|
+
dagster_sigma/resource.py
|
|
9
|
+
dagster_sigma/translator.py
|
|
10
|
+
dagster_sigma.egg-info/PKG-INFO
|
|
11
|
+
dagster_sigma.egg-info/SOURCES.txt
|
|
12
|
+
dagster_sigma.egg-info/dependency_links.txt
|
|
13
|
+
dagster_sigma.egg-info/not-zip-safe
|
|
14
|
+
dagster_sigma.egg-info/requires.txt
|
|
15
|
+
dagster_sigma.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dagster_sigma
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from setuptools import find_packages, setup
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def get_version() -> str:
|
|
5
|
+
return "0.0.1"
|
|
6
|
+
# Uncomment when ready to publish
|
|
7
|
+
# version: Dict[str, str] = {}
|
|
8
|
+
# with open(Path(__file__).parent / "dagster_sigma/version.py", encoding="utf8") as fp:
|
|
9
|
+
# exec(fp.read(), version)
|
|
10
|
+
|
|
11
|
+
# return version["__version__"]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
ver = get_version()
|
|
15
|
+
# dont pin dev installs to avoid pip dep resolver issues
|
|
16
|
+
pin = ""
|
|
17
|
+
setup(
|
|
18
|
+
name="dagster_sigma",
|
|
19
|
+
version=get_version(),
|
|
20
|
+
author="Dagster Labs",
|
|
21
|
+
author_email="hello@dagsterlabs.com",
|
|
22
|
+
license="Apache-2.0",
|
|
23
|
+
description="Build assets representing Sigma dashboards.",
|
|
24
|
+
url=(
|
|
25
|
+
"https://github.com/dagster-io/dagster/tree/master/python_modules/libraries/"
|
|
26
|
+
"dagster-sigma"
|
|
27
|
+
),
|
|
28
|
+
classifiers=[
|
|
29
|
+
"Programming Language :: Python :: 3.8",
|
|
30
|
+
"Programming Language :: Python :: 3.9",
|
|
31
|
+
"Programming Language :: Python :: 3.10",
|
|
32
|
+
"Programming Language :: Python :: 3.11",
|
|
33
|
+
"Programming Language :: Python :: 3.12",
|
|
34
|
+
"License :: OSI Approved :: Apache Software License",
|
|
35
|
+
"Operating System :: OS Independent",
|
|
36
|
+
],
|
|
37
|
+
packages=find_packages(exclude=["dagster_sigma_tests*"]),
|
|
38
|
+
install_requires=[
|
|
39
|
+
f"dagster{pin}",
|
|
40
|
+
"sqlglot",
|
|
41
|
+
],
|
|
42
|
+
include_package_data=True,
|
|
43
|
+
python_requires=">=3.8,<3.13",
|
|
44
|
+
zip_safe=False,
|
|
45
|
+
)
|