Skip to content

Commit

Permalink
[COST-4155] - use datetimes for args instead of dates (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
maskarb authored Sep 1, 2023
1 parent ac3edd4 commit e1d5050
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 183 deletions.
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
include nise/aws-template-manifest.json
include nise/ocp-template-manifest.json
include nise/yaml_generators/static/*
2 changes: 1 addition & 1 deletion nise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "4.3.3"
__version__ = "4.4.0"

VERSION = __version__.split(".")
41 changes: 32 additions & 9 deletions nise/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import os
import sys
import time
from datetime import timezone
from pathlib import Path
from pprint import pformat

from dateutil import parser as date_parser
from dateutil.parser import ParserError
from dateutil.relativedelta import relativedelta
from nise import __version__
from nise.report import aws_create_marketplace_report
Expand All @@ -50,11 +52,13 @@ class NiseError(Exception):

def valid_date(date_string):
"""Create date from date string."""
if "T" in date_string and not date_string.endswith("+0000"):
date_string += " +0000"
try:
valid = datetime.datetime.strptime(date_string, "%Y-%m-%d")
except ValueError:
valid = date_parser.parse(date_string)
except ParserError as e:
msg = f"{date_string} is an unsupported date format."
raise argparse.ArgumentTypeError(msg)
raise argparse.ArgumentTypeError(msg) from e
return valid


Expand Down Expand Up @@ -85,7 +89,7 @@ def valid_currency(currency):

def today():
"""Create the date of today."""
return datetime.datetime.now().replace(microsecond=0, second=0, minute=0)
return datetime.datetime.now(tz=timezone.utc).replace(microsecond=0, second=0, minute=0)


def add_aws_parser_args(parser):
Expand Down Expand Up @@ -274,6 +278,13 @@ def add_ocp_parser_args(parser):
action="store_true",
help="Generate ROS for Openshift data",
)
parser.add_argument(
"--daily-reports",
dest="daily_reports",
required=False,
action="store_true",
help="Flag used to add the `daily_reports` marker to manifests.",
)


def add_oci_parser_args(parser):
Expand Down Expand Up @@ -324,21 +335,21 @@ def create_parser():
parent_parser.add_argument(
"-s",
"--start-date",
metavar="YYYY-MM-DD",
metavar="YYYY-MM-DD[THH:MM:SS +0000]",
dest="start_date",
required=False,
type=valid_date,
help="Date to start generating data (YYYY-MM-DD)",
help="Date to start generating data (YYYY-MM-DD[THH:MM:SS +0000])",
)
parent_parser.add_argument(
"-e",
"--end-date",
metavar="YYYY-MM-DD",
metavar="YYYY-MM-DD[THH:MM:SS +0000]",
dest="end_date",
required=False,
type=valid_date,
default=today(),
help="Date to end generating data (YYYY-MM-DD). Default is today.",
help="Date to end generating data (YYYY-MM-DD[THH:MM:SS +0000]). Default is today.",
)
parent_parser.add_argument(
"--file-row-limit",
Expand Down Expand Up @@ -654,7 +665,6 @@ def _load_static_report_data(options):
start_date = get_start_date(attributes, options)
generated_start_date = calculate_start_date(start_date)
start_dates.append(generated_start_date)

if attributes.get("end_date"):
generated_end_date = calculate_end_date(generated_start_date, attributes.get("end_date"))
elif options.get("end_date") and options.get("end_date").date() != today().date():
Expand Down Expand Up @@ -705,6 +715,8 @@ def calculate_start_date(start_date):
generated_start_date = date_parser.parse(start_date)
else:
generated_start_date = today().replace(day=1, hour=0, minute=0, second=0)
if generated_start_date.tzinfo is None:
generated_start_date = generated_start_date.replace(tzinfo=timezone.utc)
return generated_start_date


Expand All @@ -726,6 +738,8 @@ def calculate_end_date(start_date, end_date):
generated_end_date = offset_date
else:
generated_end_date = min(start_date + relativedelta(days=offset), today())
if generated_end_date.tzinfo is None:
generated_end_date = generated_end_date.replace(tzinfo=timezone.utc)
if generated_end_date < start_date:
raise ValueError("Static yaml error: End date must be after start date.")
return generated_end_date
Expand All @@ -734,9 +748,16 @@ def calculate_end_date(start_date, end_date):
def fix_dates(options, provider_type):
"""Correct any unique dates."""
# Azure end_date is always the following day
if options["start_date"].tzinfo is None:
options["start_date"] = options["start_date"].replace(tzinfo=timezone.utc)
if options["end_date"].tzinfo is None:
options["end_date"] = options["end_date"].replace(tzinfo=timezone.utc)
if provider_type == "azure":
options["end_date"] += relativedelta(days=1)

if options["end_date"] < options["start_date"]:
raise ValueError("End date must be after start date.")


def run(provider_type, options):
"""Run nise."""
Expand All @@ -746,6 +767,8 @@ def run(provider_type, options):
if not static_data_bool:
fix_dates(options, provider_type)

LOG.debug("Options are: %s", pformat(options))

LOG.info("Creating reports...")
if provider_type == "aws":
aws_create_report(options)
Expand Down
6 changes: 3 additions & 3 deletions nise/generators/gcp/cloud_storage_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _update_data(self, row): # noqa: C901
credit, credit_total = self._gen_credit(self.credit_total, self._credit_amount)
self.credit_total = credit_total
row["credits"] = credit
usage_date = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S")
usage_date = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m")
row["invoice.month"] = f"{usage_date.year}{usage_date.month:02d}"

if self.attributes:
Expand Down Expand Up @@ -151,8 +151,8 @@ def _update_data(self, row): # noqa: C901
row["cost_type"] = "regular"
row["currency_conversion_rate"] = 1
invoice = {}
year = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S").year
month = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S").month
year = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m").year
month = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m").month
invoice["month"] = f"{year}{month:02d}"
row["invoice"] = invoice
if self.resource_level:
Expand Down
6 changes: 3 additions & 3 deletions nise/generators/gcp/compute_engine_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _update_data(self, row): # noqa: C901
credit, credit_total = self._gen_credit(self.credit_total, self._credit_amount)
self.credit_total = credit_total
row["credits"] = credit
usage_date = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S")
usage_date = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m")
row["invoice.month"] = f"{usage_date.year}{usage_date.month:02d}"

if self.attributes:
Expand Down Expand Up @@ -170,8 +170,8 @@ def _update_data(self, row): # noqa: C901
row["cost_type"] = "regular"
row["currency_conversion_rate"] = 1
invoice = {}
year = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S").year
month = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S").month
year = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m").year
month = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m").month
invoice["month"] = f"{year}{month:02d}"
row["invoice"] = invoice
if self.resource_level:
Expand Down
6 changes: 3 additions & 3 deletions nise/generators/gcp/gcp_database_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _update_data(self, row): # noqa: C901
credit, credit_total = self._gen_credit(self.credit_total, self._credit_amount)
self.credit_total = credit_total
row["credits"] = credit
usage_date = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S")
usage_date = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m")
row["invoice.month"] = f"{usage_date.year}{usage_date.month:02d}"

if self.attributes:
Expand Down Expand Up @@ -157,9 +157,9 @@ def _update_data(self, row): # noqa: C901
credit, credit_total = self._gen_credit(self.credit_total, self._credit_amount, True)
self.credit_total = credit_total
row["credits"] = credit
usage_date = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S")
usage_date = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m")
invoice = {}
usage_date = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S")
usage_date = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m")
invoice["month"] = f"{usage_date.year}{usage_date.month:02d}"
row["invoice"] = invoice
if self.resource_level:
Expand Down
6 changes: 3 additions & 3 deletions nise/generators/gcp/gcp_network_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _update_data(self, row): # noqa: C901
credit, credit_total = self._gen_credit(self.credit_total, self._credit_amount)
self.credit_total = credit_total
row["credits"] = credit
usage_date = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S")
usage_date = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m")
row["invoice.month"] = f"{usage_date.year}{usage_date.month:02d}"

if self.attributes:
Expand Down Expand Up @@ -162,9 +162,9 @@ def _update_data(self, row): # noqa: C901
credit, credit_total = self._gen_credit(self.credit_total, self._credit_amount, True)
self.credit_total = credit_total
row["credits"] = credit
usage_date = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S")
usage_date = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m")
invoice = {}
usage_date = datetime.strptime(row.get("usage_start_time"), "%Y-%m-%dT%H:%M:%S")
usage_date = datetime.strptime(row.get("usage_start_time")[:7], "%Y-%m")
invoice["month"] = f"{usage_date.year}{usage_date.month:02d}"
row["invoice"] = invoice
if self.resource_level:
Expand Down
10 changes: 5 additions & 5 deletions nise/generators/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
class AbstractGenerator(ABC):
"""Defines a abstract class for generators."""

def __init__(self, start_date, end_date):
def __init__(self, start_date, end_date, hour_delta=datetime.timedelta(minutes=60)):
"""Initialize the generator."""
self.start_date = start_date
self.end_date = end_date
self.hour_delta = hour_delta
self.hours = self._set_hours()
self.quarter_hours = self._set_quarter_hours()
self.days = self._set_days()
Expand All @@ -49,12 +50,11 @@ def _set_hours(self):
if self.end_date < self.start_date:
raise ValueError("start_date must be a date object less than end_date.")

one_hour = datetime.timedelta(minutes=60)
cur_date = self.start_date
while (cur_date + one_hour) <= self.end_date:
cur_hours = {"start": cur_date, "end": cur_date + one_hour}
while (cur_date + self.hour_delta) <= self.end_date:
cur_hours = {"start": cur_date, "end": cur_date + self.hour_delta}
hours.append(cur_hours)
cur_date = cur_date + one_hour
cur_date = cur_date + datetime.timedelta(minutes=60)
return hours

def _set_quarter_hours(self):
Expand Down
2 changes: 1 addition & 1 deletion nise/generators/ocp/ocp_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def __init__(self, start_date, end_date, attributes, ros_ocp_info=False):
if attributes:
self._nodes = attributes.get("nodes")

super().__init__(start_date, end_date)
super().__init__(start_date, end_date, hour_delta=datetime.timedelta(minutes=59, seconds=59))
self.apps = [
self.fake.word(),
self.fake.word(),
Expand Down
10 changes: 3 additions & 7 deletions nise/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

TEMPLATE_DIR = os.path.dirname(__file__)
AWS_TEMPLATE_FILE = "aws-template-manifest.json"
OCP_TEMPLATE_FILE = "ocp-template-manifest.json"


def _manifest_datetime_str(date_time):
Expand All @@ -51,7 +50,7 @@ def _manifest_datetime_range(start, end):
"""
start_str = start.strftime("%Y%m%d")
end_str = end.strftime("%Y%m%d")
return start_str + "-" + end_str
return f"{start_str}-{end_str}"


def aws_generate_manifest(fake, template_data):
Expand Down Expand Up @@ -111,8 +110,5 @@ def ocp_generate_manifest(template_data):
(String): Rendered template data
"""
template_loader = jinja2.FileSystemLoader(searchpath=TEMPLATE_DIR)
template_env = jinja2.Environment(loader=template_loader)
template = template_env.get_template(OCP_TEMPLATE_FILE)
output = template.render(template_data)
return output
# template_data["cr_status"] = json.dumps(template_data["cr_status"], indent=2)
return json.dumps(template_data, indent=2)
12 changes: 0 additions & 12 deletions nise/ocp-template-manifest.json

This file was deleted.

Loading

0 comments on commit e1d5050

Please sign in to comment.