test /spend/report

This commit is contained in:
Ishaan Jaff 2024-05-13 15:26:39 -07:00
parent 12cf9d71c7
commit 11d3c86cf1

View file

@ -138,6 +138,23 @@ async def get_predict_spend_logs(session):
return await response.json()
async def get_spend_report(session, start_date, end_date):
url = "http://0.0.0.0:4000/global/spend/report"
headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
async with session.get(
url, headers=headers, params={"start_date": start_date, "end_date": end_date}
) as response:
status = response.status
response_text = await response.text()
print(response_text)
print()
if status != 200:
raise Exception(f"Request did not return a 200 status code: {status}")
return await response.json()
@pytest.mark.asyncio
async def test_get_predicted_spend_logs():
"""
@ -205,3 +222,39 @@ async def test_spend_logs_high_traffic():
except:
print(n, time.time() - start, 0)
raise Exception("it worked!")
@pytest.mark.asyncio
async def test_spend_report_endpoint():
async with aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=600)
) as session:
import datetime
todays_date = datetime.date.today() + datetime.timedelta(days=1)
todays_date = todays_date.strftime("%Y-%m-%d")
print("todays_date", todays_date)
thirty_days_ago = (
datetime.date.today() - datetime.timedelta(days=30)
).strftime("%Y-%m-%d")
spend_report = await get_spend_report(
session=session, start_date=thirty_days_ago, end_date=todays_date
)
print("spend report", spend_report)
for row in spend_report:
date = row["group_by_day"]
teams = row["teams"]
for team in teams:
team_name = team["team_name"]
total_spend = team["total_spend"]
metadata = team["metadata"]
assert team_name is not None
print(f"Date: {date}")
print(f"Team: {team_name}")
print(f"Total Spend: {total_spend}")
print("Metadata: ", metadata)
print()