Skip to content

Commit

Permalink
Prettify duration message at end of execution (#5364)
Browse files Browse the repository at this point in the history
* Prettify duration message at end of execution
  • Loading branch information
jared-rimmer authored Jun 15, 2022
1 parent 26bb5c3 commit 6572b7e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Features-20220611-163931.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Features
body: Prettify duration message at the end of execution
time: 2022-06-11T16:39:31.725960083+01:00
custom:
Author: jared-rimmer
Issue: "5253"
PR: "5364"
5 changes: 3 additions & 2 deletions core/dbt/events/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,10 +1501,11 @@ def message(self) -> str:
class HookFinished(InfoLevel):
stat_line: str
execution: str
execution_time: float
code: str = "E040"

def message(self) -> str:
return f"Finished running {self.stat_line}{self.execution}."
return f"Finished running {self.stat_line}{self.execution} ({self.execution_time:0.2f}s)."


@dataclass
Expand Down Expand Up @@ -2620,7 +2621,7 @@ def message(self) -> str:
DatabaseErrorRunning(hook_type="")
EmptyLine()
HooksRunning(num_hooks=0, hook_type="")
HookFinished(stat_line="", execution="")
HookFinished(stat_line="", execution="", execution_time=0)
WriteCatalogFailure(num_exceptions=0)
CatalogWritten(path="")
CannotGenerateDocs()
Expand Down
6 changes: 4 additions & 2 deletions core/dbt/task/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,13 @@ def print_results_line(self, results, execution_time):
execution = ""

if execution_time is not None:
execution = " in {execution_time:0.2f}s".format(execution_time=execution_time)
execution = utils.humanize_execution_time(execution_time=execution_time)

with TextOnly():
fire_event(EmptyLine())
fire_event(HookFinished(stat_line=stat_line, execution=execution))
fire_event(
HookFinished(stat_line=stat_line, execution=execution, execution_time=execution_time)
)

def _get_deferred_manifest(self) -> Optional[WritableManifest]:
if not self.args.defer:
Expand Down
7 changes: 7 additions & 0 deletions core/dbt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ def timestring() -> str:
return datetime.datetime.utcnow().isoformat() + "Z"


def humanize_execution_time(execution_time: int) -> str:
minutes, seconds = divmod(execution_time, 60)
hours, minutes = divmod(minutes, 60)

return f" in {int(hours)} hours {int(minutes)} minutes and {seconds:0.2f} seconds"


class JSONEncoder(json.JSONEncoder):
"""A 'custom' json encoder that does normal json encoder things, but also
handles `Decimal`s and `Undefined`s. Decimals can lose precision because
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def MockNode():
DatabaseErrorRunning(hook_type=''),
EmptyLine(),
HooksRunning(num_hooks=0, hook_type=''),
HookFinished(stat_line='', execution=''),
HookFinished(stat_line='', execution='', execution_time=0),
WriteCatalogFailure(num_exceptions=0),
CatalogWritten(path=''),
CannotGenerateDocs(),
Expand Down
19 changes: 19 additions & 0 deletions test/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,22 @@ def test_two_members_overlap(self):
assert md['c'] == 1
assert md['d'] == 2
assert md['e'] == 3

class TestHumanizeExecutionTime(unittest.TestCase):
def test_humanzing_execution_time_with_integer(self):

result = dbt.utils.humanize_execution_time(execution_time=9460)

assert result == " in 2 hours 37 minutes and 40.00 seconds"

def test_humanzing_execution_time_with_two_decimal_place_float(self):

result = dbt.utils.humanize_execution_time(execution_time=0.32)

assert result == " in 0 hours 0 minutes and 0.32 seconds"

def test_humanzing_execution_time_with_four_decimal_place_float(self):

result = dbt.utils.humanize_execution_time(execution_time=0.3254)

assert result == " in 0 hours 0 minutes and 0.33 seconds"
10 changes: 9 additions & 1 deletion tests/functional/basic/test_jaffle_shop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dbt.tests.util import run_dbt, get_manifest
from dbt.tests.util import run_dbt, get_manifest, run_dbt_and_capture


from tests.fixtures.jaffle_shop import JaffleShopProject
Expand All @@ -14,3 +14,11 @@ def test_basic(self, project):
assert len(results) == 5
manifest = get_manifest(project.project_root)
assert "model.jaffle_shop.orders" in manifest.nodes

def test_execution_time_format_is_humanized(self, project):
# Create the data from seeds
run_dbt(["seed"])
_, log_output = run_dbt_and_capture(["run"])

assert " in 0 hours 0 minutes and " in log_output
assert " seconds" in log_output

0 comments on commit 6572b7e

Please sign in to comment.