fix slack alerting allow setting custom spend report frequency

This commit is contained in:
Ishaan Jaff 2024-09-07 11:42:16 -07:00
parent 009a1f7f86
commit 1b732c485d

View file

@ -1688,53 +1688,56 @@ Model Info:
await asyncio.sleep(interval) await asyncio.sleep(interval)
return return
async def send_weekly_spend_report(self): async def send_weekly_spend_report(self, time_range: str = "7d"):
""" """ """
Send a spend report for a configurable time range.
:param time_range: A string specifying the time range, e.g., "1d", "7d", "30d"
"""
try: try:
from litellm.proxy.spend_tracking.spend_management_endpoints import ( from litellm.proxy.spend_tracking.spend_management_endpoints import (
_get_spend_report_for_time_range, _get_spend_report_for_time_range,
) )
todays_date = datetime.datetime.now().date() # Parse the time range
week_before = todays_date - datetime.timedelta(days=7) days = int(time_range[:-1])
if time_range[-1].lower() != "d":
raise ValueError("Time range must be specified in days, e.g., '7d'")
weekly_spend_per_team, weekly_spend_per_tag = ( todays_date = datetime.datetime.now().date()
await _get_spend_report_for_time_range( start_date = todays_date - datetime.timedelta(days=days)
start_date=week_before.strftime("%Y-%m-%d"),
end_date=todays_date.strftime("%Y-%m-%d"), spend_per_team, spend_per_tag = await _get_spend_report_for_time_range(
) start_date=start_date.strftime("%Y-%m-%d"),
end_date=todays_date.strftime("%Y-%m-%d"),
) )
_weekly_spend_message = f"*💸 Weekly Spend Report for `{week_before.strftime('%m-%d-%Y')} - {todays_date.strftime('%m-%d-%Y')}` *\n" _spend_message = f"*💸 Spend Report for `{start_date.strftime('%m-%d-%Y')} - {todays_date.strftime('%m-%d-%Y')}` ({days} days)*\n"
if weekly_spend_per_team is not None: if spend_per_team is not None:
_weekly_spend_message += "\n*Team Spend Report:*\n" _spend_message += "\n*Team Spend Report:*\n"
for spend in weekly_spend_per_team: for spend in spend_per_team:
_team_spend = spend["total_spend"] _team_spend = round(float(spend["total_spend"]), 4)
_team_spend = float(_team_spend) _spend_message += (
# round to 4 decimal places
_team_spend = round(_team_spend, 4)
_weekly_spend_message += (
f"Team: `{spend['team_alias']}` | Spend: `${_team_spend}`\n" f"Team: `{spend['team_alias']}` | Spend: `${_team_spend}`\n"
) )
if weekly_spend_per_tag is not None: if spend_per_tag is not None:
_weekly_spend_message += "\n*Tag Spend Report:*\n" _spend_message += "\n*Tag Spend Report:*\n"
for spend in weekly_spend_per_tag: for spend in spend_per_tag:
_tag_spend = spend["total_spend"] _tag_spend = round(float(spend["total_spend"]), 4)
_tag_spend = float(_tag_spend) _spend_message += f"Tag: `{spend['individual_request_tag']}` | Spend: `${_tag_spend}`\n"
# round to 4 decimal places
_tag_spend = round(_tag_spend, 4)
_weekly_spend_message += f"Tag: `{spend['individual_request_tag']}` | Spend: `${_tag_spend}`\n"
await self.send_alert( await self.send_alert(
message=_weekly_spend_message, message=_spend_message,
level="Low", level="Low",
alert_type="spend_reports", alert_type="spend_reports",
alerting_metadata={}, alerting_metadata={},
) )
except ValueError as ve:
verbose_proxy_logger.error(f"Invalid time range format: {ve}")
except Exception as e: except Exception as e:
verbose_proxy_logger.error("Error sending weekly spend report %s", e) verbose_proxy_logger.error(f"Error sending spend report: {e}")
async def send_monthly_spend_report(self): async def send_monthly_spend_report(self):
""" """ """ """