5
5
from roboflow .adapters import deploymentapi
6
6
from roboflow .config import load_roboflow_api_key
7
7
8
+ def is_valid_ISO8601_timestamp (ts ):
9
+ try :
10
+ datetime .fromisoformat (ts )
11
+ return True
12
+ except :
13
+ return False
14
+
15
+ def check_from_to_timestamp (from_timestamp , to_timestamp , default_timedelta ):
16
+ if from_timestamp and not is_valid_ISO8601_timestamp (from_timestamp ):
17
+ print ("Please provide a valid from_timestamp in ISO8601 format" )
18
+ exit (1 )
19
+
20
+ if to_timestamp and not is_valid_ISO8601_timestamp (to_timestamp ):
21
+ print ("Please provide a valid to_timestamp in ISO8601 format" )
22
+ exit (1 )
23
+
24
+ time_now = datetime .now ().replace (tzinfo = None )
25
+ if from_timestamp is None and to_timestamp is None :
26
+ from_timestamp = time_now - default_timedelta
27
+ to_timestamp = time_now
28
+ elif from_timestamp is not None and to_timestamp is None :
29
+ from_timestamp = datetime .fromisoformat (from_timestamp ).replace (tzinfo = None )
30
+ to_timestamp = from_timestamp + default_timedelta
31
+ elif from_timestamp is None and to_timestamp is not None :
32
+ to_timestamp = datetime .fromisoformat (to_timestamp ).replace (tzinfo = None )
33
+ from_timestamp = to_timestamp - default_timedelta
34
+ else :
35
+ from_timestamp = datetime .fromisoformat (from_timestamp ).replace (tzinfo = None )
36
+ to_timestamp = datetime .fromisoformat (to_timestamp ).replace (tzinfo = None )
37
+ if from_timestamp >= to_timestamp :
38
+ print ("from_timestamp should be earlier than to_timestamp" )
39
+ exit (1 )
40
+
41
+ return from_timestamp , to_timestamp
42
+
8
43
9
44
def add_deployment_parser (subparsers ):
10
45
deployment_parser = subparsers .add_parser (
@@ -74,12 +109,14 @@ def add_deployment_parser(subparsers):
74
109
75
110
deployment_usage_workspace_parser .set_defaults (func = get_workspace_usage )
76
111
deployment_usage_workspace_parser .add_argument ("-a" , "--api_key" , help = "api key" )
77
- deployment_usage_workspace_parser .add_argument ("target_month" , help = "target month (format: YYYYMM)" , nargs = "?" )
112
+ deployment_usage_workspace_parser .add_argument ("-f" , "--from_timestamp" , help = "begin time stamp in ISO8601 format (YYYY-MM-DD HH:MM:SS)" , default = None )
113
+ deployment_usage_workspace_parser .add_argument ("-t" , "--to_timestamp" , help = "end time stamp in ISO8601 format (YYYY-MM-DD HH:MM:SS)" , default = None )
78
114
79
115
deployment_usage_deployment_parser .set_defaults (func = get_deployment_usage )
80
116
deployment_usage_deployment_parser .add_argument ("-a" , "--api_key" , help = "api key" )
81
117
deployment_usage_deployment_parser .add_argument ("deployment_name" , help = "deployment name" )
82
- deployment_usage_deployment_parser .add_argument ("target_month" , help = "target month (format: YYYYMM)" , nargs = "?" )
118
+ deployment_usage_deployment_parser .add_argument ("-f" , "--from_timestamp" , help = "begin time stamp in ISO8601 format (YYYY-MM-DD HH:MM:SS)" , default = None )
119
+ deployment_usage_deployment_parser .add_argument ("-t" , "--to_timestamp" , help = "end time stamp in ISO8601 format (YYYY-MM-DD HH:MM:SS)" , default = None )
83
120
84
121
deployment_delete_parser .set_defaults (func = delete_deployment )
85
122
deployment_delete_parser .add_argument ("-a" , "--api_key" , help = "api key" )
@@ -171,7 +208,9 @@ def get_workspace_usage(args):
171
208
if api_key is None :
172
209
print ("Please provide an api key" )
173
210
exit (1 )
174
- status_code , msg = deploymentapi .get_workspace_usage (api_key , args .target_month )
211
+
212
+ from_timestamp , to_timestamp = check_from_to_timestamp (args .from_timestamp , args .to_timestamp , timedelta (days = 1 ))
213
+ status_code , msg = deploymentapi .get_workspace_usage (api_key , from_timestamp , to_timestamp )
175
214
if status_code != 200 :
176
215
print (f"{ status_code } : { msg } " )
177
216
exit (status_code )
@@ -183,7 +222,9 @@ def get_deployment_usage(args):
183
222
if api_key is None :
184
223
print ("Please provide an api key" )
185
224
exit (1 )
186
- status_code , msg = deploymentapi .get_deployment_usage (api_key , args .deployment_name , args .target_month )
225
+
226
+ from_timestamp , to_timestamp = check_from_to_timestamp (args .from_timestamp , args .to_timestamp , timedelta (days = 1 ))
227
+ status_code , msg = deploymentapi .get_deployment_usage (api_key , args .deployment_name , from_timestamp , to_timestamp )
187
228
if status_code != 200 :
188
229
print (f"{ status_code } : { msg } " )
189
230
exit (status_code )
0 commit comments