1
1
#!/usr/bin/env python
2
- # -*- coding: utf-8; -*-
3
-
4
- # Copyright (c) 2024 Oracle and/or its affiliates.
2
+ # Copyright (c) 2024, 2025 Oracle and/or its affiliates.
5
3
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
6
4
7
5
import logging
12
10
import oci
13
11
from oci import Signer
14
12
from tqdm .auto import tqdm
13
+
15
14
from ads .common .oci_datascience import OCIDataScienceMixin
16
15
17
16
logger = logging .getLogger (__name__ )
20
19
DEFAULT_WAIT_TIME = 1200
21
20
DEFAULT_POLL_INTERVAL = 10
22
21
WORK_REQUEST_PERCENTAGE = 100
23
- # default tqdm progress bar format:
22
+ # default tqdm progress bar format:
24
23
# {l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, ' '{rate_fmt}{postfix}]
25
24
# customize the bar format to remove the {n_fmt}/{total_fmt} from the right side
26
- DEFAULT_BAR_FORMAT = ' {l_bar}{bar}| [{elapsed}<{remaining}, ' ' {rate_fmt}{postfix}]'
25
+ DEFAULT_BAR_FORMAT = " {l_bar}{bar}| [{elapsed}<{remaining}, " " {rate_fmt}{postfix}]"
27
26
28
27
29
28
class DataScienceWorkRequest (OCIDataScienceMixin ):
@@ -32,13 +31,13 @@ class DataScienceWorkRequest(OCIDataScienceMixin):
32
31
"""
33
32
34
33
def __init__ (
35
- self ,
36
- id : str ,
34
+ self ,
35
+ id : str ,
37
36
description : str = "Processing" ,
38
- config : dict = None ,
39
- signer : Signer = None ,
37
+ config : dict = None ,
38
+ signer : Signer = None ,
40
39
client_kwargs : dict = None ,
41
- ** kwargs
40
+ ** kwargs ,
42
41
) -> None :
43
42
"""Initializes ADSWorkRequest object.
44
43
@@ -49,43 +48,43 @@ def __init__(
49
48
description: str
50
49
Progress bar initial step description (Defaults to `Processing`).
51
50
config : dict, optional
52
- OCI API key config dictionary to initialize
51
+ OCI API key config dictionary to initialize
53
52
oci.data_science.DataScienceClient (Defaults to None).
54
53
signer : oci.signer.Signer, optional
55
- OCI authentication signer to initialize
54
+ OCI authentication signer to initialize
56
55
oci.data_science.DataScienceClient (Defaults to None).
57
56
client_kwargs : dict, optional
58
- Additional client keyword arguments to initialize
57
+ Additional client keyword arguments to initialize
59
58
oci.data_science.DataScienceClient (Defaults to None).
60
59
kwargs:
61
- Additional keyword arguments to initialize
60
+ Additional keyword arguments to initialize
62
61
oci.data_science.DataScienceClient.
63
62
"""
64
63
self .id = id
65
64
self ._description = description
66
65
self ._percentage = 0
67
66
self ._status = None
68
- _error_message = None
67
+ self . _error_message = ""
69
68
super ().__init__ (config , signer , client_kwargs , ** kwargs )
70
-
71
69
72
70
def _sync (self ):
73
71
"""Fetches the latest work request information to ADSWorkRequest object."""
74
72
work_request = self .client .get_work_request (self .id ).data
75
- work_request_logs = self .client .list_work_request_logs (
76
- self .id
77
- ).data
73
+ work_request_logs = self .client .list_work_request_logs (self .id ).data
78
74
79
- self ._percentage = work_request .percent_complete
75
+ self ._percentage = work_request .percent_complete
80
76
self ._status = work_request .status
81
- self ._description = work_request_logs [- 1 ].message if work_request_logs else "Processing"
82
- if work_request .status == 'FAILED' : self ._error_message = self .client .list_work_request_errors
77
+ self ._description = (
78
+ work_request_logs [- 1 ].message if work_request_logs else "Processing"
79
+ )
80
+ if work_request .status == "FAILED" :
81
+ self ._error_message = self .client .list_work_request_errors (self .id ).data
83
82
84
83
def watch (
85
- self ,
84
+ self ,
86
85
progress_callback : Callable ,
87
- max_wait_time : int = DEFAULT_WAIT_TIME ,
88
- poll_interval : int = DEFAULT_POLL_INTERVAL ,
86
+ max_wait_time : int = DEFAULT_WAIT_TIME ,
87
+ poll_interval : int = DEFAULT_POLL_INTERVAL ,
89
88
):
90
89
"""Updates the progress bar with realtime message and percentage until the process is completed.
91
90
@@ -94,10 +93,10 @@ def watch(
94
93
progress_callback: Callable
95
94
Progress bar callback function.
96
95
It must accept `(percent_change, description)` where `percent_change` is the
97
- work request percent complete and `description` is the latest work request log message.
96
+ work request percent complete and `description` is the latest work request log message.
98
97
max_wait_time: int
99
98
Maximum amount of time to wait in seconds (Defaults to 1200).
100
- Negative implies infinite wait time.
99
+ Negative implies infinite wait time.
101
100
poll_interval: int
102
101
Poll interval in seconds (Defaults to 10).
103
102
@@ -109,7 +108,6 @@ def watch(
109
108
110
109
start_time = time .time ()
111
110
while self ._percentage < 100 :
112
-
113
111
seconds_since = time .time () - start_time
114
112
if max_wait_time > 0 and seconds_since >= max_wait_time :
115
113
logger .error (f"Exceeded max wait time of { max_wait_time } seconds." )
@@ -126,12 +124,14 @@ def watch(
126
124
percent_change = self ._percentage - previous_percent_complete
127
125
previous_percent_complete = self ._percentage
128
126
progress_callback (
129
- percent_change = percent_change ,
130
- description = self ._description
127
+ percent_change = percent_change , description = self ._description
131
128
)
132
129
133
130
if self ._status in WORK_REQUEST_STOP_STATE :
134
- if self ._status != oci .work_requests .models .WorkRequest .STATUS_SUCCEEDED :
131
+ if (
132
+ self ._status
133
+ != oci .work_requests .models .WorkRequest .STATUS_SUCCEEDED
134
+ ):
135
135
if self ._description :
136
136
raise Exception (self ._description )
137
137
else :
@@ -147,12 +147,12 @@ def watch(
147
147
148
148
def wait_work_request (
149
149
self ,
150
- progress_bar_description : str = "Processing" ,
151
- max_wait_time : int = DEFAULT_WAIT_TIME ,
152
- poll_interval : int = DEFAULT_POLL_INTERVAL
150
+ progress_bar_description : str = "Processing" ,
151
+ max_wait_time : int = DEFAULT_WAIT_TIME ,
152
+ poll_interval : int = DEFAULT_POLL_INTERVAL ,
153
153
):
154
154
"""Waits for the work request progress bar to be completed.
155
-
155
+
156
156
Parameters
157
157
----------
158
158
progress_bar_description: str
@@ -162,7 +162,7 @@ def wait_work_request(
162
162
Negative implies infinite wait time.
163
163
poll_interval: int
164
164
Poll interval in seconds (Defaults to 10).
165
-
165
+
166
166
Returns
167
167
-------
168
168
None
@@ -174,7 +174,7 @@ def wait_work_request(
174
174
mininterval = 0 ,
175
175
file = sys .stdout ,
176
176
desc = progress_bar_description ,
177
- bar_format = DEFAULT_BAR_FORMAT
177
+ bar_format = DEFAULT_BAR_FORMAT ,
178
178
) as pbar :
179
179
180
180
def progress_callback (percent_change , description ):
@@ -186,6 +186,5 @@ def progress_callback(percent_change, description):
186
186
self .watch (
187
187
progress_callback = progress_callback ,
188
188
max_wait_time = max_wait_time ,
189
- poll_interval = poll_interval
189
+ poll_interval = poll_interval ,
190
190
)
191
-
0 commit comments