6
6
from roboflow .config import load_roboflow_api_key
7
7
8
8
9
+ def is_valid_ISO8601_timestamp (ts ):
10
+ try :
11
+ datetime .fromisoformat (ts )
12
+ return True
13
+ except :
14
+ return False
15
+
16
+
17
+ def check_from_to_timestamp (from_timestamp , to_timestamp , default_timedelta ):
18
+ if from_timestamp and not is_valid_ISO8601_timestamp (from_timestamp ):
19
+ print ("Please provide a valid from_timestamp in ISO8601 format" )
20
+ exit (1 )
21
+
22
+ if to_timestamp and not is_valid_ISO8601_timestamp (to_timestamp ):
23
+ print ("Please provide a valid to_timestamp in ISO8601 format" )
24
+ exit (1 )
25
+
26
+ time_now = datetime .now ().replace (tzinfo = None )
27
+ if from_timestamp is None and to_timestamp is None :
28
+ from_timestamp = time_now - default_timedelta
29
+ to_timestamp = time_now
30
+ elif from_timestamp is not None and to_timestamp is None :
31
+ from_timestamp = datetime .fromisoformat (from_timestamp ).replace (tzinfo = None )
32
+ to_timestamp = from_timestamp + default_timedelta
33
+ elif from_timestamp is None and to_timestamp is not None :
34
+ to_timestamp = datetime .fromisoformat (to_timestamp ).replace (tzinfo = None )
35
+ from_timestamp = to_timestamp - default_timedelta
36
+ else :
37
+ from_timestamp = datetime .fromisoformat (from_timestamp ).replace (tzinfo = None )
38
+ to_timestamp = datetime .fromisoformat (to_timestamp ).replace (tzinfo = None )
39
+ if from_timestamp >= to_timestamp :
40
+ print ("from_timestamp should be earlier than to_timestamp" )
41
+ exit (1 )
42
+
43
+ return from_timestamp , to_timestamp
44
+
45
+
9
46
def add_deployment_parser (subparsers ):
10
47
deployment_parser = subparsers .add_parser (
11
48
"deployment" ,
@@ -18,6 +55,12 @@ def add_deployment_parser(subparsers):
18
55
"get" , help = "show detailed info for a dedicated deployment"
19
56
)
20
57
deployment_list_parser = deployment_subparsers .add_parser ("list" , help = "list dedicated deployments in a workspace" )
58
+ deployment_usage_workspace_parser = deployment_subparsers .add_parser (
59
+ "usage_workspace" , help = "get all dedicated deployments usage in a workspace"
60
+ )
61
+ deployment_usage_deployment_parser = deployment_subparsers .add_parser (
62
+ "usage_deployment" , help = "get usage of a specific dedicated deployments"
63
+ )
21
64
deployment_delete_parser = deployment_subparsers .add_parser ("delete" , help = "delete a dedicated deployment" )
22
65
deployment_log_parser = deployment_subparsers .add_parser ("log" , help = "show log info for a dedicated deployment" )
23
66
@@ -66,6 +109,25 @@ def add_deployment_parser(subparsers):
66
109
deployment_list_parser .set_defaults (func = list_deployment )
67
110
deployment_list_parser .add_argument ("-a" , "--api_key" , help = "api key" )
68
111
112
+ deployment_usage_workspace_parser .set_defaults (func = get_workspace_usage )
113
+ deployment_usage_workspace_parser .add_argument ("-a" , "--api_key" , help = "api key" )
114
+ deployment_usage_workspace_parser .add_argument (
115
+ "-f" , "--from_timestamp" , help = "begin time stamp in ISO8601 format (YYYY-MM-DD HH:MM:SS)" , default = None
116
+ )
117
+ deployment_usage_workspace_parser .add_argument (
118
+ "-t" , "--to_timestamp" , help = "end time stamp in ISO8601 format (YYYY-MM-DD HH:MM:SS)" , default = None
119
+ )
120
+
121
+ deployment_usage_deployment_parser .set_defaults (func = get_deployment_usage )
122
+ deployment_usage_deployment_parser .add_argument ("-a" , "--api_key" , help = "api key" )
123
+ deployment_usage_deployment_parser .add_argument ("deployment_name" , help = "deployment name" )
124
+ deployment_usage_deployment_parser .add_argument (
125
+ "-f" , "--from_timestamp" , help = "begin time stamp in ISO8601 format (YYYY-MM-DD HH:MM:SS)" , default = None
126
+ )
127
+ deployment_usage_deployment_parser .add_argument (
128
+ "-t" , "--to_timestamp" , help = "end time stamp in ISO8601 format (YYYY-MM-DD HH:MM:SS)" , default = None
129
+ )
130
+
69
131
deployment_delete_parser .set_defaults (func = delete_deployment )
70
132
deployment_delete_parser .add_argument ("-a" , "--api_key" , help = "api key" )
71
133
deployment_delete_parser .add_argument ("deployment_name" , help = "deployment name" )
@@ -151,6 +213,34 @@ def list_deployment(args):
151
213
print (json .dumps (msg , indent = 2 ))
152
214
153
215
216
+ def get_workspace_usage (args ):
217
+ api_key = args .api_key or load_roboflow_api_key (None )
218
+ if api_key is None :
219
+ print ("Please provide an api key" )
220
+ exit (1 )
221
+
222
+ from_timestamp , to_timestamp = check_from_to_timestamp (args .from_timestamp , args .to_timestamp , timedelta (days = 1 ))
223
+ status_code , msg = deploymentapi .get_workspace_usage (api_key , from_timestamp , to_timestamp )
224
+ if status_code != 200 :
225
+ print (f"{ status_code } : { msg } " )
226
+ exit (status_code )
227
+ print (json .dumps (msg , indent = 2 ))
228
+
229
+
230
+ def get_deployment_usage (args ):
231
+ api_key = args .api_key or load_roboflow_api_key (None )
232
+ if api_key is None :
233
+ print ("Please provide an api key" )
234
+ exit (1 )
235
+
236
+ from_timestamp , to_timestamp = check_from_to_timestamp (args .from_timestamp , args .to_timestamp , timedelta (days = 1 ))
237
+ status_code , msg = deploymentapi .get_deployment_usage (api_key , args .deployment_name , from_timestamp , to_timestamp )
238
+ if status_code != 200 :
239
+ print (f"{ status_code } : { msg } " )
240
+ exit (status_code )
241
+ print (json .dumps (msg , indent = 2 ))
242
+
243
+
154
244
def delete_deployment (args ):
155
245
api_key = args .api_key or load_roboflow_api_key (None )
156
246
if api_key is None :
0 commit comments