-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathfreshness.py
220 lines (192 loc) · 7.25 KB
/
freshness.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
import threading
import time
from .base import BaseRunner
from .printer import (
print_run_result_error,
)
from .runnable import GraphRunnableTask
from dbt.contracts.results import (
FreshnessExecutionResultArtifact,
FreshnessResult,
PartialSourceFreshnessResult,
SourceFreshnessResult,
FreshnessStatus,
)
from dbt.exceptions import RuntimeException, InternalException
from dbt.events.functions import fire_event
from dbt.events.types import (
FreshnessCheckComplete,
PrintStartLine,
PrintHookEndErrorLine,
PrintHookEndErrorStaleLine,
PrintHookEndWarnLine,
PrintHookEndPassLine,
)
from dbt.node_types import NodeType
from dbt.graph import ResourceTypeSelector
from dbt.contracts.graph.parsed import ParsedSourceDefinition
RESULT_FILE_NAME = "sources.json"
class FreshnessRunner(BaseRunner):
def on_skip(self):
raise RuntimeException("Freshness: nodes cannot be skipped!")
def before_execute(self):
description = "freshness of {0.source_name}.{0.name}".format(self.node)
fire_event(
PrintStartLine(
description=description,
index=self.node_index,
total=self.num_nodes,
node_info=self.node.node_info,
)
)
def after_execute(self, result):
if hasattr(result, "node"):
source_name = result.node.source_name
table_name = result.node.name
else:
source_name = result.source_name
table_name = result.table_name
if result.status == FreshnessStatus.RuntimeErr:
fire_event(
PrintHookEndErrorLine(
source_name=source_name,
table_name=table_name,
index=self.node_index,
total=self.num_nodes,
execution_time=result.execution_time,
node_info=self.node.node_info,
)
)
elif result.status == FreshnessStatus.Error:
fire_event(
PrintHookEndErrorStaleLine(
source_name=source_name,
table_name=table_name,
index=self.node_index,
total=self.num_nodes,
execution_time=result.execution_time,
node_info=self.node.node_info,
)
)
elif result.status == FreshnessStatus.Warn:
fire_event(
PrintHookEndWarnLine(
source_name=source_name,
table_name=table_name,
index=self.node_index,
total=self.num_nodes,
execution_time=result.execution_time,
node_info=self.node.node_info,
)
)
else:
fire_event(
PrintHookEndPassLine(
source_name=source_name,
table_name=table_name,
index=self.node_index,
total=self.num_nodes,
execution_time=result.execution_time,
node_info=self.node.node_info,
)
)
def error_result(self, node, message, start_time, timing_info):
return self._build_run_result(
node=node,
start_time=start_time,
status=FreshnessStatus.RuntimeErr,
timing_info=timing_info,
message=message,
)
def _build_run_result(self, node, start_time, status, timing_info, message):
execution_time = time.time() - start_time
thread_id = threading.current_thread().name
return PartialSourceFreshnessResult(
status=status,
thread_id=thread_id,
execution_time=execution_time,
timing=timing_info,
message=message,
node=node,
adapter_response={},
failures=None,
)
def from_run_result(self, result, start_time, timing_info):
result.execution_time = time.time() - start_time
result.timing.extend(timing_info)
return result
def execute(self, compiled_node, manifest):
# we should only be here if we compiled_node.has_freshness, and
# therefore loaded_at_field should be a str. If this invariant is
# broken, raise!
if compiled_node.loaded_at_field is None:
raise InternalException(
"Got to execute for source freshness of a source that has no " "loaded_at_field!"
)
relation = self.adapter.Relation.create_from_source(compiled_node)
# given a Source, calculate its fresnhess.
with self.adapter.connection_for(compiled_node):
self.adapter.clear_transaction()
freshness = self.adapter.calculate_freshness(
relation,
compiled_node.loaded_at_field,
compiled_node.freshness.filter,
manifest=manifest,
)
status = compiled_node.freshness.status(freshness["age"])
return SourceFreshnessResult(
node=compiled_node,
status=status,
thread_id=threading.current_thread().name,
timing=[],
execution_time=0,
message=None,
adapter_response={},
failures=None,
**freshness,
)
def compile(self, manifest):
if self.node.resource_type != NodeType.Source:
# should be unreachable...
raise RuntimeException("fresnhess runner: got a non-Source")
# we don't do anything interesting when we compile a source node
return self.node
class FreshnessSelector(ResourceTypeSelector):
def node_is_match(self, node):
if not super().node_is_match(node):
return False
if not isinstance(node, ParsedSourceDefinition):
return False
return node.has_freshness
class FreshnessTask(GraphRunnableTask):
def result_path(self):
if self.args.output:
return os.path.realpath(self.args.output)
else:
return os.path.join(self.config.target_path, RESULT_FILE_NAME)
def raise_on_first_error(self):
return False
def get_node_selector(self):
if self.manifest is None or self.graph is None:
raise InternalException("manifest and graph must be set to get perform node selection")
return FreshnessSelector(
graph=self.graph,
manifest=self.manifest,
previous_state=self.previous_state,
resource_types=[NodeType.Source],
)
def get_runner_type(self, _):
return FreshnessRunner
def write_result(self, result):
artifact = FreshnessExecutionResultArtifact.from_result(result)
artifact.write(self.result_path())
def get_result(self, results, elapsed_time, generated_at):
return FreshnessResult.from_node_results(
elapsed_time=elapsed_time, generated_at=generated_at, results=results
)
def task_end_messages(self, results):
for result in results:
if result.status in (FreshnessStatus.Error, FreshnessStatus.RuntimeErr):
print_run_result_error(result)
fire_event(FreshnessCheckComplete())