Skip to content

Commit

Permalink
Refactor "humanize_duration_time" to "00:00:00.000" format
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankravets committed Aug 20, 2019
1 parent a25c115 commit c6de3eb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
16 changes: 8 additions & 8 deletions platformio/commands/run/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def process_env(ctx, name, config, environments, targets, upload_port, silent,

ep = EnvironmentProcessor(ctx, name, config, targets, upload_port, silent,
verbose, jobs)
result = {"env": name, "elapsed": time(), "succeeded": ep.process()}
result['elapsed'] = time() - result['elapsed']
result = {"env": name, "duration": time(), "succeeded": ep.process()}
result['duration'] = time() - result['duration']

# print footer on error or when is not unit testing
if not is_test_running and (not silent or not result['succeeded']):
Expand Down Expand Up @@ -164,18 +164,18 @@ def print_processing_footer(result):
util.print_labeled_bar(
"[%s] Took %.2f seconds" %
((click.style("FAILED", fg="red", bold=True) if is_failed else
click.style("SUCCESS", fg="green", bold=True)), result['elapsed']),
click.style("SUCCESS", fg="green", bold=True)), result['duration']),
is_error=is_failed)


def print_processing_summary(results):
tabular_data = []
succeeded_nums = 0
failed_nums = 0
elapsed = 0
duration = 0

for result in results:
elapsed += result.get("elapsed", 0)
duration += result.get("duration", 0)
if result.get("succeeded") is False:
failed_nums += 1
status_str = click.style("FAILED", fg="red")
Expand All @@ -187,19 +187,19 @@ def print_processing_summary(results):

tabular_data.append(
(click.style(result['env'], fg="cyan"), status_str,
util.humanize_elapsed_time(result.get("elapsed"))))
util.humanize_duration_time(result.get("duration"))))

click.echo()
click.echo(tabulate(tabular_data,
headers=[
click.style(s, bold=True)
for s in ("Environment", "Status", "Time")
for s in ("Environment", "Status", "Duration")
]),
err=failed_nums)

util.print_labeled_bar(
"%s%d succeeded in %s" %
("%d failed, " % failed_nums if failed_nums else "", succeeded_nums,
util.humanize_elapsed_time(elapsed)),
util.humanize_duration_time(duration)),
is_error=failed_nums,
fg="red" if failed_nums else "green")
18 changes: 9 additions & 9 deletions platformio/commands/test/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ def cli( # pylint: disable=redefined-builtin
result = {
"env": envname,
"test": testname,
"elapsed": time(),
"duration": time(),
"succeeded": tp.process()
}
result['elapsed'] = time() - result['elapsed']
result['duration'] = time() - result['duration']
results.append(result)

print_processing_footer(result)
Expand Down Expand Up @@ -179,21 +179,20 @@ def print_processing_footer(result):
util.print_labeled_bar(
"[%s] Took %.2f seconds" %
((click.style("FAILED", fg="red", bold=True) if is_failed else
click.style("PASSED", fg="green", bold=True)), result['elapsed']),
click.style("PASSED", fg="green", bold=True)), result['duration']),
is_error=is_failed)


def print_testing_summary(results):
click.echo()
# util.print_labeled_bar("SUMMARY")

tabular_data = []
succeeded_nums = 0
failed_nums = 0
elapsed = 0
duration = 0

for result in results:
elapsed += result.get("elapsed", 0)
duration += result.get("duration", 0)
if result.get("succeeded") is False:
failed_nums += 1
status_str = click.style("FAILED", fg="red")
Expand All @@ -205,18 +204,19 @@ def print_testing_summary(results):

tabular_data.append(
(result['test'], click.style(result['env'], fg="cyan"), status_str,
util.humanize_elapsed_time(result.get("elapsed"))))
util.humanize_duration_time(result.get("duration"))))

click.echo(tabulate(tabular_data,
headers=[
click.style(s, bold=True)
for s in ("Test", "Environment", "Status", "Time")
for s in ("Test", "Environment", "Status",
"Duration")
]),
err=failed_nums)

util.print_labeled_bar(
"%s%d succeeded in %s" %
("%d failed, " % failed_nums if failed_nums else "", succeeded_nums,
util.humanize_elapsed_time(elapsed)),
util.humanize_duration_time(duration)),
is_error=failed_nums,
fg="red" if failed_nums else "green")
22 changes: 9 additions & 13 deletions platformio/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,20 +455,16 @@ def print_labeled_bar(label, is_error=False, fg=None):
err=is_error)


def humanize_elapsed_time(total):
total = total or 0
constants = ((3600 * 24, "day"), (3600, "hour"), (60, "minute"),
(1, "second"))
def humanize_duration_time(duration):
if duration is None:
return duration
duration = duration * 1000
tokens = []
for coef, name in constants:
t = (math.floor if total > 60 else round)(total / coef)
if t == 0:
continue
tokens.append("%d %s%s" % (t, name, "s" if t > 1 else ""))
total -= t * coef
if len(tokens) > 1:
tokens[-1] = "and %s" % tokens[-1]
return ", ".join(tokens)
for multiplier in (3600000, 60000, 1000, 1):
fraction = math.floor(duration / multiplier)
tokens.append(int(round(duration) if multiplier == 1 else fraction))
duration -= fraction * multiplier
return "{:02d}:{:02d}:{:02d}.{:03d}".format(*tokens)


def get_original_version(version):
Expand Down

0 comments on commit c6de3eb

Please sign in to comment.