Skip to content

Commit

Permalink
feat: tests and __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nataliagranato committed Mar 20, 2024
1 parent 97cea06 commit 582997b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests/__pycache__
.vscode
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,18 @@ Para executar o script, você pode usar o seguinte comando:
python3 analise-de-custos.py
```

Ou

```
python3 -m analise_de_custos
```

O script irá solicitar que você insira as datas de início e término para a análise de custos. As datas devem ser inseridas no formato AAAA-MM-DD. Em seguida, o script irá listar os custos dos serviços AWS para o período de tempo especificado.

## Extras

- 1. **Executando o teste**:

```
python3 -m unittest tests.test_analise_de_custos
```
9 changes: 9 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from analise_de_custos import get_custom_date_range, list_costs_by_service

def main():
start_date, end_date = get_custom_date_range()
if start_date and end_date:
list_costs_by_service(start_date, end_date)

if __name__ == '__main__':
main()
Binary file added __pycache__/analise_de_custos.cpython-311.pyc
Binary file not shown.
25 changes: 22 additions & 3 deletions analise-de-custos.py → analise_de_custos.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
def get_custom_date_range():
"""
Obtém o período desejado de forma interativa.
Retorna:
tuple: Tupla contendo as datas de início e término do período.
"""

print("Digite o período desejado para análise de custos:")
start_date_str = input("Data de início (AAAA-MM-DD): ")
end_date_str = input("Data de término (AAAA-MM-DD): ")
Expand All @@ -23,12 +27,26 @@ def get_custom_date_range():
except ValueError:
print("Formato de data inválido. Use o formato AAAA-MM-DD.")
return get_custom_date_range()
except KeyboardInterrupt:
print("\nExecução interrompida pelo usuário.")
return None

# Função para obter e listar os serviços e valores de gastos
def list_costs_by_service(start_date, end_date):
"""
Obtém e lista os serviços e valores de gastos.
Args:
start_date (date): Data de início do período.
end_date (date): Data de término do período.
Retorna:
None: Sem retorno.
"""

if not start_date or not end_date:
return

response = ce_client.get_cost_and_usage(
TimePeriod={
'Start': start_date.strftime('%Y-%m-%d'),
Expand All @@ -48,9 +66,10 @@ def list_costs_by_service(start_date, end_date):
for group in result['Groups']:
service_name = group['Keys'][0]
cost = float(group['Metrics']['BlendedCost']['Amount'])
cost_brl = cost * CURRENCY_CONVERSION_RATE
print(f"Serviço: {service_name}, Custo em USD: ${cost:.2f}, Custo em BRL: R${cost_brl:.2f}")
cost_brl = f"{cost * CURRENCY_CONVERSION_RATE:.2f}"
print(f"Serviço: {service_name}, Custo em USD: ${cost:.2f}, Custo em BRL: R${cost_brl}")

if __name__ == '__main__':
start_date, end_date = get_custom_date_range()
list_costs_by_service(start_date, end_date)
if start_date and end_date:
list_costs_by_service(start_date, end_date)
6 changes: 6 additions & 0 deletions tests/test_analise_de_custos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import unittest
from analise_de_custos import get_custom_date_range, list_costs_by_service
class TestAnaliseDeCustos(unittest.TestCase):

def test_get_custom_date_range_valid_input(self):
start_date, end_date = get_custom_date_range()

0 comments on commit 582997b

Please sign in to comment.