Skip to content

Commit

Permalink
fix metrics show all when no target is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
shcheklein committed Feb 12, 2019
1 parent 95345c4 commit 51381fb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
61 changes: 23 additions & 38 deletions dvc/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,52 +1127,35 @@ def metrics_show(
for branch in self.scm.brancher(
all_branches=all_branches, all_tags=all_tags
):

astages = self.active_stages()
outs = [out for stage in astages for out in stage.outs]

if path:
outs = self._find_output_by_path(
path, outs=outs, recursive=recursive
)
stages = [out.stage.path for out in outs]
entries = []
for out in outs:
if all(
[out.metric, not typ, isinstance(out.metric, dict)]
):
entries += [
(
out.path,
out.metric.get(out.PARAM_METRIC_TYPE, None),
out.metric.get(out.PARAM_METRIC_XPATH, None),
)
]
else:
entries += [(out.path, typ, xpath)]
if not entries:
if os.path.isdir(path):
logger.warning(
"Path '{path}' is a directory. "
"Consider running with '-R'.".format(path=path)
)
return {}

else:
entries += [(path, typ, xpath)]
metrics = filter(lambda o: o.metric, outs)
stages = set()
entries = []

else:
metrics = filter(lambda o: o.metric, outs)
stages = None
entries = []
for o in metrics:
if not typ and isinstance(o.metric, dict):
t = o.metric.get(o.PARAM_METRIC_TYPE, typ)
x = o.metric.get(o.PARAM_METRIC_XPATH, xpath)
else:
t = typ
x = xpath
entries.append((o.path, t, x))
for o in metrics:
if not typ and isinstance(o.metric, dict):
t = o.metric.get(o.PARAM_METRIC_TYPE, typ)
x = o.metric.get(o.PARAM_METRIC_XPATH, xpath)
else:
t = typ
x = xpath
entries.append((o.path, t, x))
stages.add(o.stage.path)

if path and not entries:
if os.path.isdir(path):
logger.warning(
"Path '{path}' is a directory. "
"Consider running with '-R'.".format(path=path)
)
return {}

for fname, t, x in entries:
if stages:
Expand Down Expand Up @@ -1202,7 +1185,9 @@ def metrics_show(
return res

if path:
msg = "file '{}' does not exist or malformed".format(path)
msg = "file '{}' does not exist, not a metric file or is malformed".format(
path
)
else:
msg = (
"no metric files in this repository."
Expand Down
37 changes: 27 additions & 10 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ def setUp(self):
fd.write("branch\n")
fd.write(branch)

self.dvc.scm.add(
[
"metric",
"metric_json",
"metric_tsv",
"metric_htsv",
"metric_csv",
"metric_hcsv",
]
)
files = [
"metric",
"metric_json",
"metric_tsv",
"metric_htsv",
"metric_csv",
"metric_hcsv",
]

self.dvc.run(metrics_no_cache=files, overwrite=True)

self.dvc.scm.add(files + ["metric.dvc"])

self.dvc.scm.commit("metric")

self.dvc.scm.checkout("master")
Expand Down Expand Up @@ -367,6 +370,7 @@ def _do_add(self, branch):
json.dump({"metrics": branch}, fd)

stages = self.dvc.add("metrics.json")
self.dvc.metrics_add("metrics.json", typ="json", xpath="metrics")
self.assertEqual(len(stages), 1)
stage = stages[0]
self.assertIsNotNone(stage)
Expand Down Expand Up @@ -424,6 +428,19 @@ def _test_metrics(self, func):
},
)

res = self.dvc.metrics_show(
"", all_branches=True, typ="json", xpath="metrics"
)

self.assertEqual(
res,
{
"master": {"metrics.json": ["master"]},
"one": {"metrics.json": ["one"]},
"two": {"metrics.json": ["two"]},
},
)

def test_add(self):
self._test_metrics(self._do_add)

Expand Down

0 comments on commit 51381fb

Please sign in to comment.