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,40 @@ 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" )
144140
145- report_start_date = datetime .strptime (report_start_date , "%Y-%m-%d" )
146- report_end_date = datetime .strptime (report_end_date , "%Y-%m-%d" )
147-
148- report_month = datetime .strftime (report_start_date , "%Y-%m" )
141+ report_month = datetime .strftime (
142+ datetime .strptime (report_start_date , "%Y-%m-%d" ), "%Y-%m"
143+ )
149144
150145 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 (
146+ logger .info ("Using nerc rates for rates and outages " )
147+ rates_data = rates .load_from_url ()
148+ invoice_rates = invoice .Rates (
149+ cpu = rates_data .get_value_at ("CPU SU Rate" , report_month , Decimal ),
150+ gpu_a100 = rates_data .get_value_at ("GPUA100 SU Rate" , report_month , Decimal ),
151+ gpu_a100sxm4 = rates_data .get_value_at (
157152 "GPUA100SXM4 SU Rate" , report_month , Decimal
158153 ),
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 ),
154+ gpu_v100 = rates_data .get_value_at ("GPUV100 SU Rate" , report_month , Decimal ),
155+ gpu_h100 = rates_data .get_value_at ("GPUH100 SU Rate" , report_month , Decimal ),
156+ )
157+ outage_data = outages .load_from_url ()
158+ ignore_hours = outage_data .get_outages_during (
159+ report_start_date , report_end_date , cluster_name
161160 )
162161 else :
163- rates = invoice .Rates (
162+ invoice_rates = invoice .Rates (
164163 cpu = Decimal (args .rate_cpu_su ),
165164 gpu_a100 = Decimal (args .rate_gpu_a100_su ),
166165 gpu_a100sxm4 = Decimal (args .rate_gpu_a100sxm4_su ),
167166 gpu_v100 = Decimal (args .rate_gpu_v100_su ),
168167 gpu_h100 = Decimal (args .rate_gpu_h100_su ),
169168 )
169+ ignore_hours = args .ignore_hours
170+
171+ if bool (ignore_hours ): # could be None or []
172+ for start_time , end_time in ignore_hours :
173+ logger .info (f"{ start_time } to { end_time } will be excluded from the invoice" )
170174
171175 if args .invoice_file :
172176 invoice_file = args .invoice_file
@@ -183,6 +187,9 @@ def main():
183187 else :
184188 pod_report_file = f"Pod NERC OpenShift { report_month } .csv"
185189
190+ report_start_date = datetime .strptime (report_start_date , "%Y-%m-%d" )
191+ report_end_date = datetime .strptime (report_end_date , "%Y-%m-%d" )
192+
186193 if report_start_date .month != report_end_date .month :
187194 logger .warning ("The report spans multiple months" )
188195 report_month += " to " + datetime .strftime (report_end_date , "%Y-%m" )
@@ -196,7 +203,7 @@ def main():
196203 condensed_metrics_dict = condensed_metrics_dict ,
197204 file_name = invoice_file ,
198205 report_month = report_month ,
199- rates = rates ,
206+ rates = invoice_rates ,
200207 su_definitions = su_definitions ,
201208 cluster_name = cluster_name ,
202209 ignore_hours = ignore_hours ,
@@ -205,7 +212,7 @@ def main():
205212 condensed_metrics_dict = condensed_metrics_dict ,
206213 file_name = class_invoice_file ,
207214 report_month = report_month ,
208- rates = rates ,
215+ rates = invoice_rates ,
209216 su_definitions = su_definitions ,
210217 cluster_name = cluster_name ,
211218 namespaces_with_classes = ["rhods-notebooks" ],
0 commit comments