Skip to content

Commit f76b2d2

Browse files
committed
Restructure some things for a project invoice
* Remove attributes that we get from coldfront from the invoice. Still keep the CSV output consistent though. * Hold report metadata in a dataclass and pass that around to the helper functions to keep things better contained.
1 parent af8b460 commit f76b2d2

File tree

3 files changed

+39
-59
lines changed

3 files changed

+39
-59
lines changed

openshift_metrics/invoice.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,21 @@ class Rates:
192192
gpu_h100: Decimal
193193

194194

195+
@dataclass()
196+
class ReportMetadata:
197+
report_month: str
198+
cluster_name: str
199+
report_start_time: datetime.datetime
200+
report_end_time: datetime.datetime
201+
generated_at: datetime.datetime
202+
203+
195204
@dataclass
196205
class ProjectInvoce:
197206
"""Represents the invoicing data for a project."""
198207

199-
invoice_month: str
200208
project: str
201209
project_id: str
202-
pi: str
203-
cluster_name: str
204-
invoice_email: str
205-
invoice_address: str
206-
intitution: str
207-
institution_specific_code: str
208210
rates: Rates
209211
su_definitions: dict
210212
ignore_hours: Optional[List[Tuple[datetime.datetime, datetime.datetime]]] = None
@@ -240,30 +242,30 @@ def get_rate(self, su_type) -> Decimal:
240242
return self.rates.gpu_h100
241243
return Decimal(0)
242244

243-
def generate_invoice_rows(self, report_month, report_start_time, report_end_time, generated_at) -> List[str]:
245+
def generate_invoice_rows(self, metadata: ReportMetadata) -> List[str]:
244246
rows = []
245247
for su_type, hours in self.su_hours.items():
246248
if hours > 0:
247249
hours = math.ceil(hours)
248250
rate = self.get_rate(su_type)
249251
cost = (rate * hours).quantize(Decimal(".01"), rounding=ROUND_HALF_UP)
250252
row = [
251-
report_month,
252-
report_start_time,
253-
report_end_time,
253+
metadata.report_month,
254+
metadata.report_start_time.strftime("%Y-%m-%d%H:%M:%SZ"),
255+
metadata.report_end_time.strftime("%Y-%m-%d%H:%M:%SZ"),
254256
self.project,
255257
self.project_id,
256-
self.pi,
257-
self.cluster_name,
258-
self.invoice_email,
259-
self.invoice_address,
260-
self.intitution,
261-
self.institution_specific_code,
258+
"", # pi
259+
metadata.cluster_name,
260+
"", # invoice_email
261+
"", # invoice_address
262+
"", # institution
263+
"", # institution_specific_code
262264
hours,
263265
su_type,
264266
rate,
265267
cost,
266-
generated_at,
268+
metadata.generated_at.strftime("%Y-%m-%d%H:%M:%SZ"),
267269
]
268270
rows.append(row)
269271
return rows

openshift_metrics/merge.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,12 @@ def main():
210210
else:
211211
pod_report_file = f"Pod NERC OpenShift {report_month}.csv"
212212

213-
report_start_date = datetime.strptime(report_start_date, "%Y-%m-%d").replace(tzinfo=UTC)
214-
report_end_date = datetime.strptime(report_end_date, "%Y-%m-%d").replace(tzinfo=UTC) + timedelta(days=1)
213+
report_start_date = datetime.strptime(report_start_date, "%Y-%m-%d").replace(
214+
tzinfo=UTC
215+
)
216+
report_end_date = datetime.strptime(report_end_date, "%Y-%m-%d").replace(
217+
tzinfo=UTC
218+
) + timedelta(days=1)
215219

216220
logger.info(
217221
f"Generating report from {report_start_date} to {report_end_date} for {cluster_name}"
@@ -227,33 +231,29 @@ def main():
227231

228232
su_definitions = get_su_definitions(report_month)
229233

230-
generated_at = datetime.now(UTC).strftime("%Y-%m-%d%H:%M:%SZ")
231-
report_start_time = report_start_date.strftime("%Y-%m-%d%H:%M:%SZ")
232-
report_end_time = report_end_date.strftime("%Y-%m-%d%H:%M:%SZ")
234+
report_metadata = invoice.ReportMetadata(
235+
report_month=report_month,
236+
cluster_name=cluster_name,
237+
report_start_time=report_start_date,
238+
report_end_time=report_end_date,
239+
generated_at=datetime.now(UTC),
240+
)
233241

234242
utils.write_metrics_by_namespace(
235243
condensed_metrics_dict=condensed_metrics_dict,
236244
file_name=invoice_file,
237-
report_month=report_month,
245+
report_metadata=report_metadata,
238246
rates=invoice_rates,
239247
su_definitions=su_definitions,
240-
cluster_name=cluster_name,
241-
report_start_time=report_start_time,
242-
report_end_time=report_end_time,
243-
generated_at=generated_at,
244248
ignore_hours=ignore_hours,
245249
)
246250
utils.write_metrics_by_classes(
247251
condensed_metrics_dict=condensed_metrics_dict,
248252
file_name=class_invoice_file,
249-
report_month=report_month,
253+
report_metadata=report_metadata,
250254
rates=invoice_rates,
251255
su_definitions=su_definitions,
252-
cluster_name=cluster_name,
253256
namespaces_with_classes=["rhods-notebooks"],
254-
report_start_time=report_start_time,
255-
report_end_time=report_end_time,
256-
generated_at=generated_at,
257257
ignore_hours=ignore_hours,
258258
)
259259
utils.write_metrics_by_pod(

openshift_metrics/utils.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,9 @@ def csv_writer(rows, file_name):
6060
def write_metrics_by_namespace(
6161
condensed_metrics_dict,
6262
file_name,
63-
report_month,
63+
report_metadata: invoice.ReportMetadata,
6464
rates,
6565
su_definitions,
66-
cluster_name,
67-
report_start_time,
68-
report_end_time,
69-
generated_at,
7066
ignore_hours=None,
7167
):
7268
"""
@@ -98,15 +94,8 @@ def write_metrics_by_namespace(
9894
for namespace, pods in condensed_metrics_dict.items():
9995
if namespace not in invoices:
10096
project_invoice = invoice.ProjectInvoce(
101-
invoice_month=report_month,
10297
project=namespace,
10398
project_id=namespace,
104-
pi="",
105-
cluster_name=cluster_name,
106-
invoice_email="",
107-
invoice_address="",
108-
intitution="",
109-
institution_specific_code="",
11099
rates=rates,
111100
su_definitions=su_definitions,
112101
ignore_hours=ignore_hours,
@@ -134,7 +123,7 @@ def write_metrics_by_namespace(
134123
project_invoice.add_pod(pod_obj)
135124

136125
for project_invoice in invoices.values():
137-
rows.extend(project_invoice.generate_invoice_rows(report_month, report_start_time, report_end_time, generated_at))
126+
rows.extend(project_invoice.generate_invoice_rows(report_metadata))
138127

139128
csv_writer(rows, file_name)
140129

@@ -191,14 +180,10 @@ def write_metrics_by_pod(
191180
def write_metrics_by_classes(
192181
condensed_metrics_dict,
193182
file_name,
194-
report_month,
183+
report_metadata: invoice.ReportMetadata,
195184
rates,
196185
namespaces_with_classes,
197186
su_definitions,
198-
cluster_name,
199-
report_start_time,
200-
report_end_time,
201-
generated_at,
202187
ignore_hours=None,
203188
):
204189
"""
@@ -240,15 +225,8 @@ def write_metrics_by_classes(
240225

241226
if project_name not in invoices:
242227
project_invoice = invoice.ProjectInvoce(
243-
invoice_month=report_month,
244228
project=project_name,
245229
project_id=project_name,
246-
pi="",
247-
cluster_name=cluster_name,
248-
invoice_email="",
249-
invoice_address="",
250-
intitution="",
251-
institution_specific_code="",
252230
su_definitions=su_definitions,
253231
rates=rates,
254232
ignore_hours=ignore_hours,
@@ -274,6 +252,6 @@ def write_metrics_by_classes(
274252
project_invoice.add_pod(pod_obj)
275253

276254
for project_invoice in invoices.values():
277-
rows.extend(project_invoice.generate_invoice_rows(report_month, report_start_time, report_end_time, generated_at))
255+
rows.extend(project_invoice.generate_invoice_rows(report_metadata))
278256

279257
csv_writer(rows, file_name)

0 commit comments

Comments
 (0)