Skip to content

Commit

Permalink
Merge pull request #18 from naved001/prepare-for-automation
Browse files Browse the repository at this point in the history
gather metrics from previous day when script is run without any dates
  • Loading branch information
naved001 authored Dec 13, 2023
2 parents 7e99d58 + 7def32e commit b01e7e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion openshift_metrics/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def main():
args = parser.parse_args()
files = args.files
merged_dictionary = {}
output_file = f"{datetime.today().strftime('%Y-%m-%d')}.log"
output_file = f"{datetime.today().strftime('%Y-%m-%d')}.csv"

report_start_date = None
report_end_date = None
Expand Down
23 changes: 19 additions & 4 deletions openshift_metrics/openshift_prometheus_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""Collect and save metrics from prometheus"""

import argparse
from datetime import datetime
from datetime import datetime, timedelta
import os
import sys
import json
Expand All @@ -41,12 +41,12 @@ def main():
parser.add_argument(
"--report-start-date",
help="report date (ex: 2022-03-14)",
default=(datetime.today()).strftime("%Y-%m-%d"),
default=(datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d')
)
parser.add_argument(
"--report-end-date",
help="report date (ex: 2022-03-14)",
default=(datetime.today()).strftime("%Y-%m-%d"),
default=(datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d')
)
parser.add_argument("--output-file")

Expand All @@ -63,12 +63,19 @@ def main():

if args.output_file:
output_file = args.output_file
elif report_start_date == report_end_date:
output_file = f"metrics-{report_start_date}.json"
else:
output_file = f"metrics-{report_start_date}-to-{report_end_date}.json"

print(f"Generating report starting {report_start_date} and ending {report_end_date} in {output_file}")

token = openshift.get_auth_token()

token = os.environ.get("OPENSHIFT_TOKEN")

if token is None:
token = openshift.get_auth_token()

metrics_dict = {}
metrics_dict["start_date"] = report_start_date
metrics_dict["end_date"] = report_end_date
Expand All @@ -91,6 +98,14 @@ def main():
except utils.EmptyResultError:
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)

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

Expand Down

0 comments on commit b01e7e1

Please sign in to comment.