-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write run results to disk (#829) #904
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b801f9d
Make RunModelResult an APIObject with a contract, add write_json
4b43b6d
Add a JSONEncoder that encodes decimals to floats
9c9baf9
Integration tests
92566fd
Update changelog, I forgot as usual
5ccaf5b
make write_json just use write_file(...json.dumps())
b6f5283
Merge branch 'development' into write-run-results
beckjake b89018e
status can also be boolean now
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import codecs | ||
import json | ||
|
||
WHICH_PYTHON = None | ||
|
||
|
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
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,125 @@ | ||
from dbt.api.object import APIObject | ||
from dbt.utils import deep_merge | ||
from dbt.contracts.graph.manifest import COMPILE_RESULT_NODE_CONTRACT | ||
from dbt.contracts.graph.parsed import PARSED_NODE_CONTRACT | ||
from dbt.contracts.graph.compiled import COMPILED_NODE_CONTRACT | ||
from dbt.contracts.graph.manifest import PARSED_MANIFEST_CONTRACT | ||
|
||
RUN_MODEL_RESULT_CONTRACT = { | ||
'type': 'object', | ||
'additionalProperties': False, | ||
'description': 'The result of a single node being run', | ||
'properties': { | ||
'error': { | ||
'type': ['string', 'null'], | ||
'description': 'The error string, or None if there was no error', | ||
}, | ||
'skip': { | ||
'type': 'boolean', | ||
'description': 'True if this node was skipped', | ||
}, | ||
# This is assigned by dbt.ui.printer.print_test_result_line, if a test | ||
# has no error and a non-zero status | ||
'fail': { | ||
'type': ['boolean', 'null'], | ||
'description': 'On tests, true if the test failed', | ||
}, | ||
'status': { | ||
'type': ['string', 'null', 'number'], | ||
'description': 'The status result of the node execution', | ||
}, | ||
'execution_time': { | ||
'type': 'number', | ||
'description': 'The execution time, in seconds', | ||
}, | ||
'node': COMPILE_RESULT_NODE_CONTRACT, | ||
}, | ||
'required': ['node'], | ||
} | ||
|
||
|
||
def named_property(name, doc=None): | ||
def get_prop(self): | ||
return self._contents.get(name) | ||
|
||
def set_prop(self, value): | ||
self._contents[name] = value | ||
self.validate() | ||
|
||
return property(get_prop, set_prop, doc=doc) | ||
|
||
|
||
class RunModelResult(APIObject): | ||
SCHEMA = RUN_MODEL_RESULT_CONTRACT | ||
|
||
def __init__(self, node, error=None, skip=False, status=None, failed=None, | ||
execution_time=0): | ||
super(RunModelResult, self).__init__(node=node, error=error, skip=skip, | ||
status=status, fail=failed, | ||
execution_time=execution_time) | ||
|
||
# these all get set after the fact, generally | ||
error = named_property('error', | ||
'If there was an error, the text of that error') | ||
skip = named_property('skip', 'True if the model was skipped') | ||
fail = named_property('fail', 'True if this was a test and it failed') | ||
status = named_property('status', 'The status of the model execution') | ||
execution_time = named_property('execution_time', | ||
'The time in seconds to execute the model') | ||
|
||
@property | ||
def errored(self): | ||
return self.error is not None | ||
|
||
@property | ||
def failed(self): | ||
return self.fail | ||
|
||
@property | ||
def skipped(self): | ||
return self.skip | ||
|
||
def serialize(self): | ||
result = super(RunModelResult, self).serialize() | ||
result['node'] = self.node.serialize() | ||
return result | ||
|
||
|
||
EXECUTION_RESULT_CONTRACT = { | ||
'type': 'object', | ||
'additionalProperties': False, | ||
'description': 'The result of a single dbt invocation', | ||
'properties': { | ||
'results': { | ||
'type': 'array', | ||
'items': RUN_MODEL_RESULT_CONTRACT, | ||
'description': 'An array of results, one per model', | ||
}, | ||
'generated_at': { | ||
'type': 'string', | ||
'format': 'date-time', | ||
'description': ( | ||
'The time at which the execution result was generated' | ||
), | ||
}, | ||
'elapsed_time': { | ||
'type': 'number', | ||
'description': ( | ||
'The time elapsed from before_run to after_run (hooks are not ' | ||
'included)' | ||
), | ||
} | ||
}, | ||
'required': ['results', 'generated_at', 'elapsed_time'], | ||
} | ||
|
||
|
||
class ExecutionResult(APIObject): | ||
SCHEMA = EXECUTION_RESULT_CONTRACT | ||
|
||
def serialize(self): | ||
return { | ||
'results': [r.serialize() for r in self.results], | ||
'generated_at': self.generated_at, | ||
'elapsed_time': self.elapsed_time, | ||
} |
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
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from datetime import datetime | ||
from decimal import Decimal | ||
import os | ||
import hashlib | ||
import itertools | ||
import json | ||
import collections | ||
import copy | ||
import functools | ||
|
@@ -441,3 +443,14 @@ def timestring(): | |
"""Get the current datetime as an RFC 3339-compliant string""" | ||
# isoformat doesn't include the mandatory trailing 'Z' for UTC. | ||
return datetime.utcnow().isoformat() + 'Z' | ||
|
||
|
||
class JSONEncoder(json.JSONEncoder): | ||
"""A 'custom' json encoder that does normal json encoder things, but also | ||
handles `Decimal`s. Naturally, this can lose precision because they get | ||
converted to floats. | ||
""" | ||
def default(self, obj): | ||
if isinstance(obj, Decimal): | ||
return float(obj) | ||
return super(JSONEncoder, self).default(obj) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really don't like the way Python handles JSON encoding. This looks like a good start in handling this better in dbt. |
17 changes: 17 additions & 0 deletions
17
test/integration/029_docs_generate_tests/ref_models/docs.md
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,17 @@ | ||
{% docs ephemeral_summary %} | ||
A summmary table of the ephemeral copy of the seed data | ||
{% enddocs %} | ||
|
||
{% docs summary_first_name %} | ||
The first name being summarized | ||
{% enddocs %} | ||
|
||
{% docs summary_count %} | ||
The number of instances of the first name | ||
{% enddocs %} | ||
|
||
{% docs view_summary %} | ||
A view of the summary of the ephemeral copy of the seed data | ||
{% enddocs %} | ||
|
||
|
7 changes: 7 additions & 0 deletions
7
test/integration/029_docs_generate_tests/ref_models/ephemeral_copy.sql
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,7 @@ | ||
{{ | ||
config( | ||
materialized = "ephemeral" | ||
) | ||
}} | ||
|
||
select * from {{ this.schema }}.seed |
9 changes: 9 additions & 0 deletions
9
test/integration/029_docs_generate_tests/ref_models/ephemeral_summary.sql
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,9 @@ | ||
{{ | ||
config( | ||
materialized = "table" | ||
) | ||
}} | ||
|
||
select first_name, count(*) as ct from {{ref('ephemeral_copy')}} | ||
group by first_name | ||
order by first_name asc |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love this