Skip to content

Commit

Permalink
Add logging module, and verbose output flag
Browse files Browse the repository at this point in the history
  • Loading branch information
manics committed Jul 25, 2024
1 parent fccace4 commit 47f7d72
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions aws_project_costs/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from argparse import ArgumentParser

import yaml
Expand All @@ -13,9 +14,12 @@ def main() -> None:
)
parser.add_argument("--costs", required=True, help="Project costs CSV")
parser.add_argument("--output", help="Output file")
parser.add_argument("--verbose", "-v", action="store_true", help="Verbose output")

args = parser.parse_args()

logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)

with open(args.config) as f:
config = yaml.safe_load(f)

Expand Down
12 changes: 10 additions & 2 deletions aws_project_costs/project_costs.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import json
import logging
from enum import Enum
from operator import itemgetter
from typing import Any, Hashable, Optional

import pandas as pd

logger = logging.getLogger(__name__)

PROJECT_TAG = "Proj"


Expand Down Expand Up @@ -86,6 +89,9 @@ def _shared_account(
project_name = proj_tag_names_map[project_tag]
elif "*" in proj_tag_names_map:
project_name = proj_tag_names_map["*"]
logger.warn(
"Project tag %s not found, using %s", project_tag, project_name
)
else:
raise ValueError(f"{project_tag} is not in proj-tag-names")
rows.append(
Expand Down Expand Up @@ -180,7 +186,7 @@ def analyse_costs_csv(
for start in sorted(df["START"].unique()):
month_costs = df[df["START"] == start]
for accountname in month_costs["ACCOUNTNAME"].unique():
# print(f"Processing {accountname} {start}")
logger.debug(f"Processing {accountname} {start}")
try:
acc_itemised_rows = allocate_costs(
accountname=accountname,
Expand All @@ -190,7 +196,9 @@ def analyse_costs_csv(
)
itemised_rows.extend(sorted(acc_itemised_rows, key=itemgetter(1, 2, 0)))
except Exception as e:
print(f"ERROR: Failed to analyse account {accountname} {start} {e}")
logger.error(
f"ERROR: Failed to analyse account {accountname} {start} {e}"
)
raise

if output_csv_filename:
Expand Down

0 comments on commit 47f7d72

Please sign in to comment.