88import json
99from typing import Tuple
1010from decimal import Decimal
11- import nerc_rates
11+ from nerc_rates import rates , outages
1212
1313from openshift_metrics import utils , invoice
1414from openshift_metrics .metrics_processor import MetricsProcessor
@@ -44,14 +44,14 @@ def parse_timestamp_range(timestamp_range: str) -> Tuple[datetime, datetime]:
4444
4545def get_su_definitions (report_month ) -> dict :
4646 su_definitions = {}
47- nerc_data = nerc_rates .load_from_url ()
47+ rates_data = rates .load_from_url ()
4848 su_names = ["GPUV100" , "GPUA100" , "GPUA100SXM4" , "GPUH100" , "CPU" ]
4949 resource_names = ["vCPUs" , "RAM" , "GPUs" ]
5050 for su_name in su_names :
5151 su_definitions .setdefault (f"OpenShift { su_name } " , {})
5252 for resource_name in resource_names :
5353 su_definitions [f"OpenShift { su_name } " ][resource_name ] = (
54- nerc_data .get_value_at (
54+ rates_data .get_value_at (
5555 f"{ resource_name } in { su_name } SU" , report_month , Decimal
5656 )
5757 )
@@ -102,7 +102,6 @@ def main():
102102
103103 args = parser .parse_args ()
104104 files = args .files
105- ignore_hours = args .ignore_hours
106105
107106 report_start_date = None
108107 report_end_date = None
@@ -138,35 +137,36 @@ def main():
138137 logger .info (
139138 f"Generating report from { report_start_date } to { report_end_date } for { cluster_name } "
140139 )
141- if ignore_hours :
142- for start_time , end_time in ignore_hours :
143- logger .info (f"{ start_time } to { end_time } will be excluded from the invoice" )
144-
145- report_start_date = datetime .strptime (report_start_date , "%Y-%m-%d" )
146- report_end_date = datetime .strptime (report_end_date , "%Y-%m-%d" )
147140
148- report_month = datetime .strftime (report_start_date , "%Y-%m" )
141+ report_month = datetime .strftime (datetime . strptime ( report_start_date , "%Y-%m-%d" ) , "%Y-%m" )
149142
150143 if args .use_nerc_rates :
151- logger .info ("Using nerc rates. " )
152- nerc_data = nerc_rates .load_from_url ()
153- rates = invoice .Rates (
154- cpu = nerc_data .get_value_at ("CPU SU Rate" , report_month , Decimal ),
155- gpu_a100 = nerc_data .get_value_at ("GPUA100 SU Rate" , report_month , Decimal ),
156- gpu_a100sxm4 = nerc_data .get_value_at (
144+ logger .info ("Using nerc rates for rates and outages " )
145+ rates_data = rates .load_from_url ()
146+ invoice_rates = invoice .Rates (
147+ cpu = rates_data .get_value_at ("CPU SU Rate" , report_month , Decimal ),
148+ gpu_a100 = rates_data .get_value_at ("GPUA100 SU Rate" , report_month , Decimal ),
149+ gpu_a100sxm4 = rates_data .get_value_at (
157150 "GPUA100SXM4 SU Rate" , report_month , Decimal
158151 ),
159- gpu_v100 = nerc_data .get_value_at ("GPUV100 SU Rate" , report_month , Decimal ),
160- gpu_h100 = nerc_data .get_value_at ("GPUH100 SU Rate" , report_month , Decimal ),
152+ gpu_v100 = rates_data .get_value_at ("GPUV100 SU Rate" , report_month , Decimal ),
153+ gpu_h100 = rates_data .get_value_at ("GPUH100 SU Rate" , report_month , Decimal ),
161154 )
155+ outage_data = outages .load_from_url ()
156+ ignore_hours = outage_data .get_outages_during (report_start_date , report_end_date , cluster_name )
162157 else :
163- rates = invoice .Rates (
158+ invoice_rates = invoice .Rates (
164159 cpu = Decimal (args .rate_cpu_su ),
165160 gpu_a100 = Decimal (args .rate_gpu_a100_su ),
166161 gpu_a100sxm4 = Decimal (args .rate_gpu_a100sxm4_su ),
167162 gpu_v100 = Decimal (args .rate_gpu_v100_su ),
168163 gpu_h100 = Decimal (args .rate_gpu_h100_su ),
169164 )
165+ ignore_hours = args .ignore_hours
166+
167+ if bool (ignore_hours ): # could be None or []
168+ for start_time , end_time in ignore_hours :
169+ logger .info (f"{ start_time } to { end_time } will be excluded from the invoice" )
170170
171171 if args .invoice_file :
172172 invoice_file = args .invoice_file
@@ -183,6 +183,9 @@ def main():
183183 else :
184184 pod_report_file = f"Pod NERC OpenShift { report_month } .csv"
185185
186+ report_start_date = datetime .strptime (report_start_date , "%Y-%m-%d" )
187+ report_end_date = datetime .strptime (report_end_date , "%Y-%m-%d" )
188+
186189 if report_start_date .month != report_end_date .month :
187190 logger .warning ("The report spans multiple months" )
188191 report_month += " to " + datetime .strftime (report_end_date , "%Y-%m" )
@@ -196,7 +199,7 @@ def main():
196199 condensed_metrics_dict = condensed_metrics_dict ,
197200 file_name = invoice_file ,
198201 report_month = report_month ,
199- rates = rates ,
202+ rates = invoice_rates ,
200203 su_definitions = su_definitions ,
201204 cluster_name = cluster_name ,
202205 ignore_hours = ignore_hours ,
@@ -205,7 +208,7 @@ def main():
205208 condensed_metrics_dict = condensed_metrics_dict ,
206209 file_name = class_invoice_file ,
207210 report_month = report_month ,
208- rates = rates ,
211+ rates = invoice_rates ,
209212 su_definitions = su_definitions ,
210213 cluster_name = cluster_name ,
211214 namespaces_with_classes = ["rhods-notebooks" ],
0 commit comments