Skip to content

Commit 082217e

Browse files
committed
switch to jinja for report building; start to tweak output
1 parent b09deb8 commit 082217e

File tree

3 files changed

+97
-11
lines changed

3 files changed

+97
-11
lines changed

Diff for: .github/workflows/build-dbt.yml

+1-10
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,7 @@ jobs:
7878
run: |
7979
cd warehouse
8080
gsutil cp -r gs://calitp-dbt-artifacts/latest/ .
81-
echo "Warehouse report 📦" >> report.md
82-
echo "### New models 🌱" >> report.md
83-
poetry run dbt --no-use-colors ls --resource-type model --select state:new --state ./latest | grep "^calitp_warehouse\." | sed "s/calitp_warehouse\.//g" >> report.md
84-
echo "### Changed incremental models 🔀" >> report.md
85-
poetry run dbt --no-use-colors ls --resource-type model --select state:modified,config.materialized:incremental --exclude state:new --state ./latest | grep "^calitp_warehouse\." | sed "s/calitp_warehouse\.//g" >> report.md
86-
echo "### DAG" >> report.md
87-
poetry run dbt --no-use-colors ls --resource-type model --select state:modified --state ./latest | grep "^calitp_warehouse\." | sed 's/^/--include /g' | xargs poetry run python scripts/visualize.py man --output=target/dag.png
88-
echo "" >> report.md
89-
echo '![](./target/dag.png "Changed models")' >> report.md
90-
echo "> Note: if you have changed any incremental models, you will need to run a full refresh on them at some point, regardless of how `on_schema_change` is configured." >> report.md
81+
poetry run python scripts/visualize.py ci-report
9182
cml comment update report.md
9283
9384
build_push:

Diff for: warehouse/scripts/templates/ci_report.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Warehouse report 📦
2+
3+
### New models 🌱
4+
5+
{% for model in new_models %}
6+
{{ model }}
7+
{% endfor %}
8+
9+
### Changed incremental models 🔀
10+
11+
{% for model in changed_incremental_models %}
12+
{{ model }}
13+
{% endfor %}
14+
15+
### DAG
16+
17+
![](./dag.png "Changed models")
18+
19+
### Checks/potential follow-ups
20+
{% if changed_incremental_models %}
21+
* Full refresh as a follow-up action item
22+
{% endif %}

Diff for: warehouse/scripts/visualize.py

+74-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
Provide more visualizations than what dbt provides.
33
"""
44
import json
5+
import os
56
import webbrowser
67
from pathlib import Path
78
from typing import Any, List, Optional, Tuple, Type, Union
89

910
import gcsfs # type: ignore
1011
import networkx as nx # type: ignore
1112
import typer
13+
from dbt.cli.main import dbtRunner
1214
from dbt_artifacts import (
1315
Catalog,
1416
DbtNode,
@@ -19,6 +21,7 @@
1921
SeedNode,
2022
SourceDefinition,
2123
)
24+
from jinja2 import Environment, FileSystemLoader, select_autoescape
2225

2326
app = typer.Typer(pretty_exceptions_enable=False)
2427

@@ -122,7 +125,11 @@ def build_graph(
122125
G.add_node(node_or_result.gvrepr, **node_or_result.gvattrs, style="filled")
123126

124127
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]
126133
if not should_display(
127134
node,
128135
analyses,
@@ -216,5 +223,71 @@ def viz(
216223
webbrowser.open(url, new=2) # open in new tab
217224

218225

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+
219292
if __name__ == "__main__":
220293
app()

0 commit comments

Comments
 (0)