66from roboflow .config import load_roboflow_api_key
77
88
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+
946def add_deployment_parser (subparsers ):
1047 deployment_parser = subparsers .add_parser (
1148 "deployment" ,
@@ -18,6 +55,12 @@ def add_deployment_parser(subparsers):
1855 "get" , help = "show detailed info for a dedicated deployment"
1956 )
2057 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+ )
2164 deployment_delete_parser = deployment_subparsers .add_parser ("delete" , help = "delete a dedicated deployment" )
2265 deployment_log_parser = deployment_subparsers .add_parser ("log" , help = "show log info for a dedicated deployment" )
2366
@@ -66,6 +109,25 @@ def add_deployment_parser(subparsers):
66109 deployment_list_parser .set_defaults (func = list_deployment )
67110 deployment_list_parser .add_argument ("-a" , "--api_key" , help = "api key" )
68111
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+
69131 deployment_delete_parser .set_defaults (func = delete_deployment )
70132 deployment_delete_parser .add_argument ("-a" , "--api_key" , help = "api key" )
71133 deployment_delete_parser .add_argument ("deployment_name" , help = "deployment name" )
@@ -151,6 +213,34 @@ def list_deployment(args):
151213 print (json .dumps (msg , indent = 2 ))
152214
153215
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+
154244def delete_deployment (args ):
155245 api_key = args .api_key or load_roboflow_api_key (None )
156246 if api_key is None :
0 commit comments