Skip to content

Commit bfad6fb

Browse files
committed
metrics: diff: add --no-path
Part of iterative#3690
1 parent 84712c4 commit bfad6fb

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

dvc/command/metrics.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def run(self):
8787
return 0
8888

8989

90-
def _show_diff(diff, markdown=False):
90+
def _show_diff(diff, markdown=False, no_path=False):
9191
from collections import OrderedDict
9292

9393
from dvc.utils.diff import table
@@ -96,16 +96,18 @@ def _show_diff(diff, markdown=False):
9696
for fname, mdiff in diff.items():
9797
sorted_mdiff = OrderedDict(sorted(mdiff.items()))
9898
for metric, change in sorted_mdiff.items():
99-
rows.append(
100-
[
101-
fname,
102-
metric,
103-
change["new"],
104-
change.get("diff", "diff not supported"),
105-
]
106-
)
99+
row = [] if no_path else [fname]
100+
row.append(metric)
101+
row.append(change["new"])
102+
row.append(change.get("diff", "diff not supported"))
103+
rows.append(row)
104+
105+
header = [] if no_path else ["Path"]
106+
header.append("Metric")
107+
header.append("Value")
108+
header.append("Change")
107109

108-
return table(["Path", "Metric", "Value", "Change"], rows, markdown)
110+
return table(header, rows, markdown)
109111

110112

111113
class CmdMetricsDiff(CmdBase):
@@ -124,7 +126,7 @@ def run(self):
124126

125127
logger.info(json.dumps(diff))
126128
else:
127-
table = _show_diff(diff, self.args.show_md)
129+
table = _show_diff(diff, self.args.show_md, self.args.no_path)
128130
if table:
129131
logger.info(table)
130132

@@ -269,6 +271,12 @@ def add_parser(subparsers, parent_parser):
269271
default=False,
270272
help="Show tabulated output in the Markdown format (GFM).",
271273
)
274+
metrics_diff_parser.add_argument(
275+
"--no-path",
276+
action="store_true",
277+
default=False,
278+
help="Don't show metric path.",
279+
)
272280
metrics_diff_parser.set_defaults(func=CmdMetricsDiff)
273281

274282
METRICS_REMOVE_HELP = "Remove metric mark on a DVC-tracked file."

scripts/completion/dvc.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ _dvc_lock_COMPGEN=_dvc_compgen_DVCFiles
3838
_dvc_metrics='add diff modify remove show'
3939
_dvc_metrics_add='-t --type -x --xpath'
4040
_dvc_metrics_add_COMPGEN=_dvc_compgen_files
41-
_dvc_metrics_diff='--targets -t --type -x --xpath -R --show-json --show-md'
41+
_dvc_metrics_diff='--targets -t --type -x --xpath -R --show-json --show-md --no-path'
4242
_dvc_metrics_modify='-t --type -x --xpath'
4343
_dvc_metrics_modify_COMPGEN=_dvc_compgen_files
4444
_dvc_metrics_remove=''

tests/unit/command/test_metrics.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def test_metrics_diff(dvc, mocker):
1717
"--targets",
1818
"target1",
1919
"target2",
20+
"--show-md",
21+
"--no-path",
2022
]
2123
)
2224
assert cli_args.func == CmdMetricsDiff
@@ -174,3 +176,22 @@ def test_metrics_diff_markdown():
174176
| metrics.yaml | a.d.e | 4 | 1 |
175177
| metrics.yaml | x.b | 6 | diff not supported |"""
176178
)
179+
180+
181+
def test_metrics_diff_no_path():
182+
assert _show_diff(
183+
{
184+
"metrics.yaml": {
185+
"x.b": {"old": 5, "new": 6, "diff": 1},
186+
"a.d.e": {"old": 3, "new": 4, "diff": 1},
187+
"a.b.c": {"old": 1, "new": 2, "diff": 1},
188+
}
189+
},
190+
no_path=True,
191+
) == textwrap.dedent(
192+
"""\
193+
Metric Value Change
194+
a.b.c 2 1
195+
a.d.e 4 1
196+
x.b 6 1"""
197+
)

0 commit comments

Comments
 (0)