Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept absolute paths for filenames #85

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions openshift_metrics/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ def main():
"""Reads the metrics from files and generates the reports"""
parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="+")
parser.add_argument("--output-file")
parser.add_argument(
"--invoice-file",
help = "Name of the invoice file. Defaults to NERC OpenShift <report_month>.csv"
)
parser.add_argument(
"--pod-report-file",
help = "Name of the pod report file. Defaults to Pod NERC OpenShift <report_month>.csv"
)
parser.add_argument(
"--upload-to-s3",
action="store_true"
Expand All @@ -49,11 +56,6 @@ def main():

args = parser.parse_args()
files = args.files

if args.output_file:
output_file = args.output_file
else:
output_file = f"{datetime.today().strftime('%Y-%m-%d')}.csv"
ignore_hours = args.ignore_hours

report_start_date = None
Expand Down Expand Up @@ -89,6 +91,16 @@ def main():

report_month = datetime.strftime(report_start_date, "%Y-%m")

if args.invoice_file:
invoice_file = args.invoice_file
else:
invoice_file = f"NERC OpenShift {report_month}.csv"

if args.pod_report_file:
pod_report_file = args.pod_report_file
else:
pod_report_file = f"Pod NERC OpenShift {report_month}.csv"

if report_start_date.month != report_end_date.month:
print("Warning: The report spans multiple months")
report_month += " to " + datetime.strftime(report_end_date, "%Y-%m")
Expand All @@ -98,30 +110,30 @@ def main():
)
utils.write_metrics_by_namespace(
condensed_metrics_dict,
output_file,
invoice_file,
report_month,
ignore_hours,
)
utils.write_metrics_by_pod(condensed_metrics_dict, "pod-" + output_file, ignore_hours)
utils.write_metrics_by_pod(condensed_metrics_dict, pod_report_file, ignore_hours)

if args.upload_to_s3:
primary_location = (
f"Invoices/{report_month}/"
f"Service Invoices/NERC OpenShift {report_month}.csv"
)
utils.upload_to_s3(output_file, "nerc-invoicing", primary_location)
utils.upload_to_s3(invoice_file, "nerc-invoicing", primary_location)

timestamp = datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
secondary_location = (
f"Invoices/{report_month}/"
f"Archive/NERC OpenShift {report_month} {timestamp}.csv"
)
utils.upload_to_s3(output_file, "nerc-invoicing", secondary_location)
pod_report = (
utils.upload_to_s3(invoice_file, "nerc-invoicing", secondary_location)
pod_report_location = (
f"Invoices/{report_month}/"
f"Archive/Pod-NERC OpenShift {report_month} {timestamp}.csv"
)
utils.upload_to_s3("pod-" + output_file, "nerc-invoicing", pod_report)
utils.upload_to_s3(pod_report_file, "nerc-invoicing", pod_report_location)

if __name__ == "__main__":
main()
11 changes: 5 additions & 6 deletions openshift_metrics/openshift_prometheus_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,17 @@ def main():
pass

month_year = datetime.strptime(report_start_date, "%Y-%m-%d").strftime("%Y-%m")
directory_name = f"data_{month_year}"

if not os.path.exists(directory_name):
os.makedirs(directory_name)

output_file = os.path.join(directory_name, output_file)
if report_start_date == report_end_date:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err, sorry, need more coffee.

s3_location = f"data_{month_year}/metrics-{report_start_date}.json"
else:
s3_location = f"data_{month_year}/metrics-{report_start_date}-to-{report_end_date}.json"

with open(output_file, "w") as file:
json.dump(metrics_dict, file)

if args.upload_to_s3:
utils.upload_to_s3(output_file, "openshift-metrics", output_file)
utils.upload_to_s3(output_file, "openshift-metrics", s3_location)


if __name__ == "__main__":
Expand Down
Loading