1
+ from time import sleep
2
+
1
3
from okdata .sdk .status import Status
2
4
3
5
from okdata .cli .command import BaseCommand , BASE_COMMAND_OPTIONS
@@ -8,15 +10,17 @@ class StatusCommand(BaseCommand):
8
10
__doc__ = f"""Oslo :: Status
9
11
10
12
Usage:
11
- okdata status <trace_id> [options --history ]
13
+ okdata status <trace_id> [options]
12
14
13
15
Examples:
14
16
okdata status trace-id-from-system
15
- okdata status trace-id-from-system --format=json | jq ".done"
17
+ okdata status trace-id-from-system --watch
16
18
okdata status trace-id-from-system --history
19
+ okdata status trace-id-from-system --format=json | jq ".done"
17
20
18
21
Options:{ BASE_COMMAND_OPTIONS }
19
22
--history
23
+ --watch
20
24
"""
21
25
22
26
def __init__ (self ):
@@ -28,7 +32,7 @@ def login(self):
28
32
def handler (self ):
29
33
self .log .info ("StatusCommand.handler()" )
30
34
if self .arg ("trace_id" ):
31
- self .status_for_id ()
35
+ self .status_for_id (self . arg ( "trace_id" ) )
32
36
else :
33
37
self .print ("Invalid command" )
34
38
@@ -84,11 +88,35 @@ def full_history_for_status(self, trace_id, trace_events):
84
88
{"error" : 1 , "message" : "No trace events found" },
85
89
)
86
90
87
- def status_for_id (self ):
88
- trace_id = self .arg ("trace_id" )
91
+ def get_trace_events (self , trace_id ):
89
92
self .log .info (f"Looking up status: { trace_id } " )
90
93
trace_events = self .sdk .get_status (trace_id )
91
- trace_events = StatusCommand .get_error_messages (trace_events )
94
+ return StatusCommand .get_error_messages (trace_events )
95
+
96
+ def wait_until_done (self , trace_id ):
97
+ """Wait until status for `trace_id` is ready, then return the trace."""
98
+ trace_events = self .get_trace_events (trace_id )
99
+ trace_status = StatusCommand .find_latest_event (trace_events )["trace_status" ]
100
+
101
+ if trace_status != "FINISHED" :
102
+ print ("Waiting for upload to finish" , end = "" , flush = True )
103
+
104
+ while trace_status != "FINISHED" :
105
+ print ("." , end = "" , flush = True )
106
+ sleep (2 )
107
+ trace_events = self .get_trace_events (trace_id )
108
+ trace_status = StatusCommand .find_latest_event (trace_events )["trace_status" ]
109
+ print ()
110
+
111
+ return trace_events
112
+
113
+ def status_for_id (self , trace_id ):
114
+ trace_events = (
115
+ self .wait_until_done (trace_id )
116
+ if self .opt ("watch" )
117
+ else self .get_trace_events (trace_id )
118
+ )
119
+
92
120
if self .opt ("history" ):
93
121
self .full_history_for_status (trace_id , trace_events )
94
122
else :
0 commit comments