-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add GraphViz and D2 as new outputs (#35)
- Loading branch information
Showing
18 changed files
with
1,010 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from dbterd.adapters.targets.constants import Strategies | ||
from dbterd.adapters.targets.d2 import d2_test_relationship | ||
from dbterd.adapters.targets.default import default | ||
|
||
run_operation_default = default | ||
run_operation_dispatcher = { | ||
Strategies.D2_TEST_RELATIONSHIP: d2_test_relationship.run, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from dbterd.adapters.algos import test_relationship | ||
|
||
|
||
def run(manifest, catalog, **kwargs): | ||
"""Parse dbt artifacts and export D2 file | ||
Args: | ||
manifest (dict): Manifest json | ||
catalog (dict): Catalog json | ||
Returns: | ||
Tuple(str, str): File name and the D2 content | ||
""" | ||
return ("output.d2", parse(manifest, catalog, **kwargs)) | ||
|
||
|
||
def parse(manifest, catalog, **kwargs): | ||
"""Get the D2 content from dbt artifacts | ||
Args: | ||
manifest (dict): Manifest json | ||
catalog (dict): Catalog json | ||
Returns: | ||
str: D2 content | ||
""" | ||
tables, relationships = test_relationship.parse( | ||
manifest=manifest, catalog=catalog, **kwargs | ||
) | ||
|
||
# Build D2 content | ||
# https://play.d2lang.com/?script=qlDQtVOo5AIEAAD__w%3D%3D&, https://github.com/terrastruct/d2 | ||
d2 = "" | ||
for table in tables: | ||
d2 += '"{table}": {{\n shape: sql_table\n{columns}\n}}\n'.format( | ||
table=table.name, | ||
columns="\n".join([f" {x.name}: {x.data_type}" for x in table.columns]), | ||
) | ||
|
||
for rel in relationships: | ||
key_from = f'"{rel.table_map[1]}"' | ||
key_to = f'"{rel.table_map[0]}"' | ||
connector = f"{rel.column_map[1]} = {rel.column_map[0]}" | ||
d2 += f'{key_from} {get_rel_symbol(rel.type)} {key_to}: "{connector}"\n' | ||
|
||
return d2 | ||
|
||
|
||
def get_rel_symbol(relationship_type: str) -> str: | ||
"""Get D2 relationship symbol | ||
Args: | ||
relationship_type (str): relationship type | ||
Returns: | ||
str: Relation symbol supported in D2 | ||
""" | ||
return "->" # no supports for rel type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from dbterd.adapters.targets.constants import Strategies | ||
from dbterd.adapters.targets.default import default | ||
from dbterd.adapters.targets.graphviz import graphviz_test_relationship | ||
|
||
run_operation_default = default | ||
run_operation_dispatcher = { | ||
Strategies.GRAPHVIZ_TEST_RELATIONSHIP: graphviz_test_relationship.run, | ||
} |
91 changes: 91 additions & 0 deletions
91
dbterd/adapters/targets/graphviz/graphviz_test_relationship.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from dbterd.adapters.algos import test_relationship | ||
|
||
|
||
def run(manifest, catalog, **kwargs): | ||
"""Parse dbt artifacts and export GraphViz file | ||
Args: | ||
manifest (dict): Manifest json | ||
catalog (dict): Catalog json | ||
Returns: | ||
Tuple(str, str): File name and the GraphViz content | ||
""" | ||
return ("output.graphviz", parse(manifest, catalog, **kwargs)) | ||
|
||
|
||
def parse(manifest, catalog, **kwargs): | ||
"""Get the GraphViz content from dbt artifacts | ||
Args: | ||
manifest (dict): Manifest json | ||
catalog (dict): Catalog json | ||
Returns: | ||
str: GraphViz content | ||
""" | ||
tables, relationships = test_relationship.parse( | ||
manifest=manifest, catalog=catalog, **kwargs | ||
) | ||
|
||
# Build GraphViz content | ||
# https://dreampuf.github.io/GraphvizOnline/, https://graphviz.org/ | ||
graphviz = ( | ||
"digraph g {\n" | ||
' fontname="Helvetica,Arial,sans-serif"\n' | ||
' node [fontname="Helvetica,Arial,sans-serif"]\n' | ||
' edge [fontname="Helvetica,Arial,sans-serif"]\n' | ||
' graph [fontsize=30 labelloc="t" label="" splines=true overlap=false rankdir="LR"];\n' | ||
" ratio=auto;\n" | ||
) | ||
for table in tables: | ||
graphviz += ( | ||
' "{table}" [\n' | ||
' style = "filled, bold"\n' | ||
" penwidth = 1\n" | ||
' fillcolor = "white"\n' | ||
' fontname = "Courier New"\n' | ||
' shape = "Mrecord"\n' | ||
" label =<\n" | ||
' <table border="0" cellborder="0" cellpadding="3" bgcolor="white">\n' | ||
' <tr><td bgcolor="black" align="center" colspan="2">' | ||
'<font color="white">{table}</font></td></tr>\n{columns}\n' | ||
" </table>> ];\n" | ||
).format( | ||
table=table.name, | ||
columns="\n".join( | ||
[ | ||
f' <tr><td align="left">({x.data_type}) {x.name}</td></tr>' | ||
for x in table.columns | ||
] | ||
), | ||
) | ||
|
||
for rel in relationships: | ||
key_from = f'"{rel.table_map[1]}"' | ||
key_to = f'"{rel.table_map[0]}"' | ||
connector = f"{rel.column_map[1]} = {rel.column_map[0]}" | ||
graphviz += ( | ||
f" {key_from} {get_rel_symbol(rel.type)} {key_to} [ \n" | ||
" penwidth = 1\n" | ||
" fontsize = 12\n" | ||
' fontcolor = "black"\n' | ||
f' label = "{connector}"\n' | ||
" ];\n" | ||
) | ||
|
||
graphviz += "}" | ||
|
||
return graphviz | ||
|
||
|
||
def get_rel_symbol(relationship_type: str) -> str: | ||
"""Get GraphViz relationship symbol | ||
Args: | ||
relationship_type (str): relationship type | ||
Returns: | ||
str: Relation symbol supported in GraphViz | ||
""" | ||
return "->" # no supports for rel type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generate D2 ERD | ||
|
||
## 1. Install D2 CLI | ||
|
||
```bash | ||
# With --dry-run the install script will print the commands it will use | ||
# to install without actually installing so you know what it's going to do. | ||
curl -fsSL https://d2lang.com/install.sh | sh -s -- --dry-run | ||
# If things look good, install for real. | ||
curl -fsSL https://d2lang.com/install.sh | sh -s -- | ||
``` | ||
|
||
> Check [installations](https://github.com/terrastruct/d2/blob/master/docs/INSTALL.md) for more details | ||
## 2. Generate D2 ERD content | ||
|
||
```bash | ||
dbterd run -t d2 -ad "samples/dbtresto" -o "target" -s schema:dbt.mart | ||
# Expected: ./target/output.d2 | ||
``` | ||
|
||
## 2. Export to SVG | ||
|
||
```bash | ||
d2 -w ./target/output.d2 ./target/output.svg | ||
``` | ||
|
||
### 3. Embeded into Markdown | ||
|
||
```markdown | ||
# Sample D2 ERD | ||
|
||
![d2](./target/output.svg) | ||
``` | ||
|
||
Sample Output: | ||
|
||
![d2](https://github.com/datnguye/dbterd/blob/main/samples/dbtresto/d2.svg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Generate GraphViz ERD | ||
|
||
## 1. Install GraphViz CLI | ||
|
||
```bash | ||
sudo apt install graphviz | ||
``` | ||
|
||
> Check [installations](https://graphviz.org/download/#linux) for more details | ||
## 2. Generate GraphViz ERD content | ||
|
||
```bash | ||
dbterd run -t graphviz -ad "samples/dbtresto" -o "target" -s schema:dbt.mart | ||
# Expected: ./target/output.graphviz | ||
``` | ||
|
||
## 2. Export to PNG | ||
|
||
```bash | ||
dot -Tpng ./target/output.d2 > ./target/output.png | ||
``` | ||
|
||
### 3. Embeded into Markdown | ||
|
||
```markdown | ||
# Sample GraphViz ERD | ||
|
||
![graphviz](./target/output.png) | ||
``` | ||
|
||
Sample Output: | ||
|
||
![graphviz](https://github.com/datnguye/dbterd/blob/main/samples/dbtresto/graphviz.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.