14
14
def show_metrics (
15
15
metrics , all_branches = False , all_tags = False , all_commits = False
16
16
):
17
- """
18
- Args:
19
- metrics (list): Where each element is either a `list`
20
- if an xpath was specified, otherwise a `str`
21
- """
17
+ from flatten_json import flatten
18
+ from dvc .utils .diff import format_dict
19
+
22
20
# When `metrics` contains a `None` key, it means that some files
23
21
# specified as `targets` in `repo.metrics.show` didn't contain any metrics.
24
22
missing = metrics .pop (None , None )
@@ -28,21 +26,13 @@ def show_metrics(
28
26
logger .info ("{branch}:" .format (branch = branch ))
29
27
30
28
for fname , metric in val .items ():
31
- if isinstance (metric , dict ):
32
- lines = list (metric .values ())
33
- elif isinstance (metric , list ):
34
- lines = metric
35
- else :
36
- lines = metric .splitlines ()
29
+ if not isinstance (metric , dict ):
30
+ logger .info ("\t {}: {}" .format (fname , str (metric )))
31
+ continue
37
32
38
- if len (lines ) > 1 :
39
- logger .info ("\t {fname}:" .format (fname = fname ))
40
-
41
- for line in lines :
42
- logger .info ("\t \t {content}" .format (content = line ))
43
-
44
- else :
45
- logger .info ("\t {}: {}" .format (fname , metric ))
33
+ logger .info ("\t {}:" .format (fname ))
34
+ for key , value in flatten (format_dict (metric ), "." ).items ():
35
+ logger .info ("\t \t {}: {}" .format (key , value ))
46
36
47
37
if missing :
48
38
raise BadMetricError (missing )
@@ -53,35 +43,25 @@ def run(self):
53
43
try :
54
44
metrics = self .repo .metrics .show (
55
45
self .args .targets ,
56
- typ = self .args .type ,
57
- xpath = self .args .xpath ,
58
46
all_branches = self .args .all_branches ,
59
47
all_tags = self .args .all_tags ,
60
48
all_commits = self .args .all_commits ,
61
49
recursive = self .args .recursive ,
62
50
)
63
51
64
- show_metrics (
65
- metrics ,
66
- self .args .all_branches ,
67
- self .args .all_tags ,
68
- self .args .all_commits ,
69
- )
70
- except DvcException :
71
- logger .exception ("failed to show metrics" )
72
- return 1
73
-
74
- return 0
75
-
52
+ if self .args .show_json :
53
+ import json
76
54
77
- class CmdMetricsModify (CmdBase ):
78
- def run (self ):
79
- try :
80
- self .repo .metrics .modify (
81
- self .args .path , typ = self .args .type , xpath = self .args .xpath
82
- )
55
+ logger .info (json .dumps (metrics ))
56
+ else :
57
+ show_metrics (
58
+ metrics ,
59
+ self .args .all_branches ,
60
+ self .args .all_tags ,
61
+ self .args .all_commits ,
62
+ )
83
63
except DvcException :
84
- logger .exception ("failed to modify metric file settings " )
64
+ logger .exception ("failed to show metrics " )
85
65
return 1
86
66
87
67
return 0
@@ -90,9 +70,7 @@ def run(self):
90
70
class CmdMetricsAdd (CmdBase ):
91
71
def run (self ):
92
72
try :
93
- self .repo .metrics .add (
94
- self .args .path , self .args .type , self .args .xpath
95
- )
73
+ self .repo .metrics .add (self .args .path )
96
74
except DvcException :
97
75
msg = "failed to add metric file '{}'" .format (self .args .path )
98
76
logger .exception (msg )
@@ -114,11 +92,14 @@ def run(self):
114
92
115
93
116
94
def _show_diff (diff ):
95
+ from collections import OrderedDict
96
+
117
97
from dvc .utils .diff import table
118
98
119
99
rows = []
120
100
for fname , mdiff in diff .items ():
121
- for metric , change in mdiff .items ():
101
+ sorted_mdiff = OrderedDict (sorted (mdiff .items ()))
102
+ for metric , change in sorted_mdiff .items ():
122
103
rows .append (
123
104
[
124
105
fname ,
@@ -138,9 +119,8 @@ def run(self):
138
119
a_rev = self .args .a_rev ,
139
120
b_rev = self .args .b_rev ,
140
121
targets = self .args .targets ,
141
- typ = self .args .type ,
142
- xpath = self .args .xpath ,
143
122
recursive = self .args .recursive ,
123
+ all = self .args .all ,
144
124
)
145
125
146
126
if self .args .show_json :
@@ -185,32 +165,9 @@ def add_parser(subparsers, parent_parser):
185
165
help = METRICS_ADD_HELP ,
186
166
formatter_class = argparse .RawDescriptionHelpFormatter ,
187
167
)
188
- metrics_add_parser .add_argument (
189
- "-t" , "--type" , help = "Type of metrics (json/yaml)." , metavar = "<type>" ,
190
- )
191
- metrics_add_parser .add_argument (
192
- "-x" , "--xpath" , help = "json/yaml path." , metavar = "<path>" ,
193
- )
194
168
metrics_add_parser .add_argument ("path" , help = "Path to a metric file." )
195
169
metrics_add_parser .set_defaults (func = CmdMetricsAdd )
196
170
197
- METRICS_MODIFY_HELP = "Modify metric default formatting."
198
- metrics_modify_parser = metrics_subparsers .add_parser (
199
- "modify" ,
200
- parents = [parent_parser ],
201
- description = append_doc_link (METRICS_MODIFY_HELP , "metrics/modify" ),
202
- help = METRICS_MODIFY_HELP ,
203
- formatter_class = argparse .RawDescriptionHelpFormatter ,
204
- )
205
- metrics_modify_parser .add_argument (
206
- "-t" , "--type" , help = "Type of metrics (json/yaml)." , metavar = "<type>" ,
207
- )
208
- metrics_modify_parser .add_argument (
209
- "-x" , "--xpath" , help = "json/yaml path." , metavar = "<path>" ,
210
- )
211
- metrics_modify_parser .add_argument ("path" , help = "Path to a metric file." )
212
- metrics_modify_parser .set_defaults (func = CmdMetricsModify )
213
-
214
171
METRICS_SHOW_HELP = "Print metrics, with optional formatting."
215
172
metrics_show_parser = metrics_subparsers .add_parser (
216
173
"show" ,
@@ -224,19 +181,6 @@ def add_parser(subparsers, parent_parser):
224
181
nargs = "*" ,
225
182
help = "Metric files or directories (see -R) to show" ,
226
183
)
227
- metrics_show_parser .add_argument (
228
- "-t" ,
229
- "--type" ,
230
- help = (
231
- "Type of metrics (json/yaml). "
232
- "It can be detected by the file extension automatically. "
233
- "Unsupported types will be treated as raw."
234
- ),
235
- metavar = "<type>" ,
236
- )
237
- metrics_show_parser .add_argument (
238
- "-x" , "--xpath" , help = "json/yaml path." , metavar = "<path>" ,
239
- )
240
184
metrics_show_parser .add_argument (
241
185
"-a" ,
242
186
"--all-branches" ,
@@ -267,6 +211,12 @@ def add_parser(subparsers, parent_parser):
267
211
"metric files."
268
212
),
269
213
)
214
+ metrics_show_parser .add_argument (
215
+ "--show-json" ,
216
+ action = "store_true" ,
217
+ default = False ,
218
+ help = "Show output in JSON format." ,
219
+ )
270
220
metrics_show_parser .set_defaults (func = CmdMetricsShow )
271
221
272
222
METRICS_DIFF_HELP = "Show changes in metrics between commits"
@@ -295,19 +245,6 @@ def add_parser(subparsers, parent_parser):
295
245
),
296
246
metavar = "<paths>" ,
297
247
)
298
- metrics_diff_parser .add_argument (
299
- "-t" ,
300
- "--type" ,
301
- help = (
302
- "Type of metrics (json/yaml). "
303
- "It can be detected by the file extension automatically. "
304
- "Unsupported types will be treated as raw."
305
- ),
306
- metavar = "<type>" ,
307
- )
308
- metrics_diff_parser .add_argument (
309
- "-x" , "--xpath" , help = "json/yaml path." , metavar = "<path>" ,
310
- )
311
248
metrics_diff_parser .add_argument (
312
249
"-R" ,
313
250
"--recursive" ,
@@ -318,6 +255,12 @@ def add_parser(subparsers, parent_parser):
318
255
"metric files."
319
256
),
320
257
)
258
+ metrics_diff_parser .add_argument (
259
+ "--all" ,
260
+ action = "store_true" ,
261
+ default = False ,
262
+ help = "Show unchanged metrics as well." ,
263
+ )
321
264
metrics_diff_parser .add_argument (
322
265
"--show-json" ,
323
266
action = "store_true" ,
0 commit comments