1
1
from collections import defaultdict
2
2
import functools
3
- import json
4
3
import sys
5
4
import textwrap
6
5
from typing import (
7
6
Any ,
8
7
Dict ,
9
8
Mapping ,
10
- Optional ,
11
9
Sequence ,
12
10
)
13
11
import uuid
23
21
echo_via_pager ,
24
22
tabulate_items ,
25
23
)
24
+ from ..utils import format_multiline , format_stats
26
25
from ...exceptions import NoItems
27
26
28
27
29
28
SessionItem = Dict [str , Any ]
30
29
31
30
32
31
# Lets say formattable options are:
33
- format_options = {
32
+ field_names = {
34
33
'name' : ('Session Name' ,
35
34
lambda api_session : get_naming (api_session .api_version , 'name_gql_field' )),
36
35
'type' : ('Type' ,
39
38
'session_id' : ('Session ID' , 'session_id' ),
40
39
'status' : ('Status' , 'status' ),
41
40
'status_info' : ('Status Info' , 'status_info' ),
41
+ 'status_data' : ('Status Data' , 'status_data' ),
42
42
'created_at' : ('Created At' , 'created_at' ),
43
43
'terminated_at' : ('Terminated At' , 'terminated_at' ),
44
44
'last_updated' : ('Last updated' , 'status_changed' ),
51
51
'cluster_hostname' : ('Hostname' , 'cluster_hostname' ),
52
52
}
53
53
54
- format_options_legacy = {
54
+ field_names_legacy = {
55
55
'used_memory' : ('Used Memory (MiB)' , 'mem_cur_bytes' ),
56
56
'max_used_memory' : ('Max Used Memory (MiB)' , 'mem_max_bytes' ),
57
57
'cpu_using' : ('CPU Using (%)' , 'cpu_using' ),
58
58
}
59
59
60
60
61
- def transform_legacy_mem_fields (item : SessionItem ) -> SessionItem :
61
+ def transform_fields (item : SessionItem , * , in_row : bool = True ) -> SessionItem :
62
62
if 'mem_cur_bytes' in item :
63
63
item ['mem_cur_bytes' ] = round (item ['mem_cur_bytes' ] / 2 ** 20 , 1 )
64
64
if 'mem_max_bytes' in item :
@@ -97,9 +97,9 @@ def sessions(status, access_key, name_only, dead, running, detail, plain, format
97
97
is_admin = session .KeyPair (session .config .access_key ).info ()['is_admin' ]
98
98
try :
99
99
name_key = get_naming (session .api_version , 'name_gql_field' )
100
- fields .append (format_options ['name' ])
100
+ fields .append (field_names ['name' ])
101
101
if is_admin :
102
- fields .append (format_options ['owner' ])
102
+ fields .append (field_names ['owner' ])
103
103
except Exception as e :
104
104
print_error (e )
105
105
sys .exit (1 )
@@ -108,36 +108,36 @@ def sessions(status, access_key, name_only, dead, running, detail, plain, format
108
108
elif format is not None :
109
109
options = format .split (',' )
110
110
for opt in options :
111
- if opt not in format_options :
111
+ if opt not in field_names :
112
112
print_fail (f'There is no such format option: { opt } ' )
113
113
sys .exit (1 )
114
114
fields = [
115
- format_options [opt ] for opt in options
115
+ field_names [opt ] for opt in options
116
116
]
117
117
else :
118
118
if session .api_version [0 ] >= 6 :
119
- fields .append (format_options ['session_id' ])
119
+ fields .append (field_names ['session_id' ])
120
120
fields .extend ([
121
- format_options ['group' ],
122
- format_options ['kernel_id' ],
123
- format_options ['image' ],
124
- format_options ['type' ],
125
- format_options ['status' ],
126
- format_options ['status_info' ],
127
- format_options ['last_updated' ],
128
- format_options ['result' ],
121
+ field_names ['group' ],
122
+ field_names ['kernel_id' ],
123
+ field_names ['image' ],
124
+ field_names ['type' ],
125
+ field_names ['status' ],
126
+ field_names ['status_info' ],
127
+ field_names ['last_updated' ],
128
+ field_names ['result' ],
129
129
])
130
130
if detail :
131
131
fields .extend ([
132
- format_options ['tag' ],
133
- format_options ['created_at' ],
134
- format_options ['occupied_slots' ],
132
+ field_names ['tag' ],
133
+ field_names ['created_at' ],
134
+ field_names ['occupied_slots' ],
135
135
])
136
136
if session .api_version [0 ] < 5 :
137
137
fields .extend ([
138
- format_options_legacy ['used_memory' ],
139
- format_options_legacy ['max_used_memory' ],
140
- format_options_legacy ['cpu_using' ],
138
+ field_names_legacy ['used_memory' ],
139
+ field_names_legacy ['max_used_memory' ],
140
+ field_names_legacy ['cpu_using' ],
141
141
])
142
142
143
143
no_match_name = None
@@ -175,7 +175,7 @@ def sessions(status, access_key, name_only, dead, running, detail, plain, format
175
175
else :
176
176
echo_via_pager (
177
177
tabulate_items (items , fields ,
178
- item_formatter = transform_legacy_mem_fields )
178
+ item_formatter = transform_fields )
179
179
)
180
180
except NoItems :
181
181
print ("There are no matching sessions." )
@@ -184,23 +184,16 @@ def sessions(status, access_key, name_only, dead, running, detail, plain, format
184
184
sys .exit (1 )
185
185
186
186
187
- def format_stats (raw_stats : Optional [str ], indent = '' ) -> str :
188
- if raw_stats is None :
189
- return "(unavailable)"
190
- stats = json .loads (raw_stats )
191
- text = "\n " .join (f"- { k + ': ' :18s} { v } " for k , v in stats .items ())
192
- return "\n " + textwrap .indent (text , indent )
193
-
194
-
195
187
def format_containers (containers : Sequence [Mapping [str , Any ]], indent = '' ) -> str :
188
+
196
189
if len (containers ) == 0 :
197
190
text = "- (There are no sub-containers belonging to the session)"
198
191
else :
199
192
text = ""
200
193
for cinfo in containers :
201
194
text += "\n " .join ((
202
195
f"+ { cinfo ['id' ]} " ,
203
- * (f" - { k + ': ' :18s} { v } "
196
+ * (f" - { k + ': ' :18s} { format_multiline ( v , 22 ) } "
204
197
for k , v in cinfo .items ()
205
198
if k not in ('id' , 'live_stat' , 'last_stat' )),
206
199
f" + live_stat: { format_stats (cinfo ['live_stat' ], indent = ' ' )} " ,
@@ -235,19 +228,20 @@ def session(id_or_name):
235
228
)),
236
229
]
237
230
if session_ .api_version [0 ] >= 6 :
238
- fields .append (format_options ['session_id' ])
239
- fields .append (format_options ['kernel_id' ])
231
+ fields .append (field_names ['session_id' ])
232
+ fields .append (field_names ['kernel_id' ])
240
233
fields .extend ([
241
- format_options ['image' ],
234
+ field_names ['image' ],
242
235
])
243
236
if session_ .api_version >= (4 , '20181215' ):
244
- fields .append (format_options ['tag' ])
237
+ fields .append (field_names ['tag' ])
245
238
fields .extend ([
246
- format_options ['created_at' ],
247
- format_options ['terminated_at' ],
248
- format_options ['status' ],
249
- format_options ['status_info' ],
250
- format_options ['occupied_slots' ],
239
+ field_names ['created_at' ],
240
+ field_names ['terminated_at' ],
241
+ field_names ['status' ],
242
+ field_names ['status_info' ],
243
+ field_names ['status_data' ],
244
+ field_names ['occupied_slots' ],
251
245
])
252
246
fields = apply_version_aware_fields (session_ , fields )
253
247
field_formatters = defaultdict (lambda : str )
@@ -271,7 +265,7 @@ def session(id_or_name):
271
265
'Containers' ,
272
266
'containers {'
273
267
' id cluster_role cluster_idx cluster_hostname '
274
- ' agent status status_info status_changed '
268
+ ' agent status status_info status_data status_changed '
275
269
' occupied_slots live_stat last_stat '
276
270
'}' ,
277
271
))
@@ -310,7 +304,7 @@ def session(id_or_name):
310
304
else :
311
305
print_fail ('There is no such compute session.' )
312
306
sys .exit (1 )
313
- transform_legacy_mem_fields (resp ['compute_session' ])
307
+ transform_fields (resp ['compute_session' ], in_row = False )
314
308
for i , (key , value ) in enumerate (resp ['compute_session' ].items ()):
315
309
fmt = field_formatters [key ]
316
310
print (f"{ fields [i ][0 ] + ': ' :20s} { fmt (value )} " )
0 commit comments