-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathprinter.py
155 lines (127 loc) · 4.48 KB
/
printer.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
from typing import Dict
from dbt.logger import (
DbtStatusMessage,
TextOnly,
)
from dbt.events.functions import fire_event
from dbt.events.types import (
EmptyLine,
RunResultWarning,
RunResultFailure,
StatsLine,
RunResultError,
RunResultErrorNoMessage,
SQLCompiledPath,
CheckNodeTestFailure,
FirstRunResultError,
AfterFirstRunResultError,
EndOfRunSummary,
)
from dbt.tracking import InvocationProcessor
from dbt.events.format import pluralize
from dbt.contracts.results import NodeStatus
from dbt.node_types import NodeType
def get_counts(flat_nodes) -> str:
counts: Dict[str, int] = {}
for node in flat_nodes:
t = node.resource_type
if node.resource_type == NodeType.Model:
t = "{} {}".format(node.get_materialization(), t)
elif node.resource_type == NodeType.Operation:
t = "hook"
counts[t] = counts.get(t, 0) + 1
stat_line = ", ".join([pluralize(v, k) for k, v in counts.items()])
return stat_line
def interpret_run_result(result) -> str:
if result.status in (NodeStatus.Error, NodeStatus.Fail):
return "error"
elif result.status == NodeStatus.Skipped:
return "skip"
elif result.status == NodeStatus.Warn:
return "warn"
elif result.status in (NodeStatus.Pass, NodeStatus.Success):
return "pass"
else:
raise RuntimeError(f"unhandled result {result}")
def print_run_status_line(results) -> None:
stats = {
"error": 0,
"skip": 0,
"pass": 0,
"warn": 0,
"total": 0,
}
for r in results:
result_type = interpret_run_result(r)
stats[result_type] += 1
stats["total"] += 1
with TextOnly():
fire_event(EmptyLine())
fire_event(StatsLine(stats=stats))
def print_run_result_error(result, newline: bool = True, is_warning: bool = False) -> None:
if newline:
with TextOnly():
fire_event(EmptyLine())
if result.status == NodeStatus.Fail or (is_warning and result.status == NodeStatus.Warn):
if is_warning:
fire_event(
RunResultWarning(
resource_type=result.node.resource_type,
node_name=result.node.name,
path=result.node.original_file_path,
)
)
else:
fire_event(
RunResultFailure(
resource_type=result.node.resource_type,
node_name=result.node.name,
path=result.node.original_file_path,
)
)
if result.message:
fire_event(RunResultError(msg=result.message))
else:
fire_event(RunResultErrorNoMessage(status=result.status))
if result.node.build_path is not None:
with TextOnly():
fire_event(EmptyLine())
fire_event(SQLCompiledPath(path=result.node.compiled_path))
if result.node.should_store_failures:
with TextOnly():
fire_event(EmptyLine())
fire_event(CheckNodeTestFailure(relation_name=result.node.relation_name))
elif result.message is not None:
first = True
for line in result.message.split("\n"):
if first:
fire_event(FirstRunResultError(msg=line))
first = False
else:
fire_event(AfterFirstRunResultError(msg=line))
def print_run_end_messages(results, keyboard_interrupt: bool = False) -> None:
errors, warnings = [], []
for r in results:
if r.status in (NodeStatus.RuntimeErr, NodeStatus.Error, NodeStatus.Fail):
errors.append(r)
elif r.status == NodeStatus.Skipped and r.message is not None:
# this means we skipped a node because of an issue upstream,
# so include it as an error
errors.append(r)
elif r.status == NodeStatus.Warn:
warnings.append(r)
with DbtStatusMessage(), InvocationProcessor():
with TextOnly():
fire_event(EmptyLine())
fire_event(
EndOfRunSummary(
num_errors=len(errors),
num_warnings=len(warnings),
keyboard_interrupt=keyboard_interrupt,
)
)
for error in errors:
print_run_result_error(error, is_warning=False)
for warning in warnings:
print_run_result_error(warning, is_warning=True)
print_run_status_line(results)