|
2 | 2 | Provide more visualizations than what dbt provides.
|
3 | 3 | """
|
4 | 4 | import json
|
| 5 | +import os |
5 | 6 | import webbrowser
|
6 | 7 | from pathlib import Path
|
7 | 8 | from typing import Any, List, Optional, Tuple, Type, Union
|
8 | 9 |
|
9 | 10 | import gcsfs # type: ignore
|
10 | 11 | import networkx as nx # type: ignore
|
11 | 12 | import typer
|
| 13 | +from dbt.cli.main import dbtRunner |
12 | 14 | from dbt_artifacts import (
|
13 | 15 | Catalog,
|
14 | 16 | DbtNode,
|
|
19 | 21 | SeedNode,
|
20 | 22 | SourceDefinition,
|
21 | 23 | )
|
| 24 | +from jinja2 import Environment, FileSystemLoader, select_autoescape |
22 | 25 |
|
23 | 26 | app = typer.Typer(pretty_exceptions_enable=False)
|
24 | 27 |
|
@@ -122,7 +125,11 @@ def build_graph(
|
122 | 125 | G.add_node(node_or_result.gvrepr, **node_or_result.gvattrs, style="filled")
|
123 | 126 |
|
124 | 127 | for node_or_result in nodes:
|
125 |
| - node: DbtNode = node_or_result.node if isinstance(node_or_result, RunResultOutput) else node_or_result # type: ignore[no-redef] |
| 128 | + node: DbtNode = ( |
| 129 | + node_or_result.node |
| 130 | + if isinstance(node_or_result, RunResultOutput) |
| 131 | + else node_or_result |
| 132 | + ) # type: ignore[no-redef] |
126 | 133 | if not should_display(
|
127 | 134 | node,
|
128 | 135 | analyses,
|
@@ -216,5 +223,71 @@ def viz(
|
216 | 223 | webbrowser.open(url, new=2) # open in new tab
|
217 | 224 |
|
218 | 225 |
|
| 226 | +@app.command() |
| 227 | +def ci_report( |
| 228 | + latest_dir: str = "./latest", |
| 229 | + output: str = "target/report.md", |
| 230 | +): |
| 231 | + dbt = dbtRunner() |
| 232 | + new_models = dbt.invoke( |
| 233 | + [ |
| 234 | + "ls", |
| 235 | + "--resource-type", |
| 236 | + "model", |
| 237 | + "--select", |
| 238 | + "state:new", |
| 239 | + "--state", |
| 240 | + latest_dir, |
| 241 | + ] |
| 242 | + ).result |
| 243 | + |
| 244 | + changed_incremental_models = dbt.invoke( |
| 245 | + [ |
| 246 | + "ls", |
| 247 | + "--resource-type", |
| 248 | + "model", |
| 249 | + "--select", |
| 250 | + "state:modified,config.materialized:incremental", |
| 251 | + "--exclude", |
| 252 | + "state:new", |
| 253 | + "--state", |
| 254 | + latest_dir, |
| 255 | + ] |
| 256 | + ).result |
| 257 | + |
| 258 | + modified_models = dbt.invoke( |
| 259 | + [ |
| 260 | + "ls", |
| 261 | + "--resource-type", |
| 262 | + "model", |
| 263 | + "--select", |
| 264 | + "state:modified", |
| 265 | + "--exclude", |
| 266 | + "state:new", |
| 267 | + "--state", |
| 268 | + latest_dir, |
| 269 | + ] |
| 270 | + ).result |
| 271 | + viz( |
| 272 | + "man", |
| 273 | + include=modified_models, |
| 274 | + output=Path("./target/dag.png"), |
| 275 | + ) |
| 276 | + |
| 277 | + print(os.path.join(__file__, "templates")) |
| 278 | + env = Environment( |
| 279 | + loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates")), |
| 280 | + autoescape=select_autoescape(), |
| 281 | + ) |
| 282 | + template = env.get_template("ci_report.md") |
| 283 | + report = template.render( |
| 284 | + new_models=new_models, |
| 285 | + changed_incremental_models=changed_incremental_models, |
| 286 | + ) |
| 287 | + typer.secho(f"Writing to {output}", fg=typer.colors.GREEN) |
| 288 | + with open(output, "w") as f: |
| 289 | + f.write(report) |
| 290 | + |
| 291 | + |
219 | 292 | if __name__ == "__main__":
|
220 | 293 | app()
|
0 commit comments