agent: |
xp9aAsSwUKwYtnJLMAhArds costs by instance id
rds costs by instance id
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
inputs
outputs
import pandas as pd
from datetime import datetime, timedelta, timezone
# Assuming df is your DataFrame and last_n_days is defined
# Example: last_n_days = 30
if df is not None:
print("DataFrame Columns:", list(df.columns))
# Convert 'lineItem/UsageStartDate' to datetime
df['lineItem/UsageStartDate'] = pd.to_datetime(df['lineItem/UsageStartDate'])
# Filter out negative costs
df = df[df['lineItem/UnblendedCost'] >= 0]
# Filter rows for the last N days for RDS
cutoff_date = datetime.utcnow().replace(tzinfo=timezone.utc) - timedelta(days=last_n_days)
rds_df = df[(df['lineItem/UsageStartDate'] > cutoff_date) &
(df['lineItem/ProductCode'] == 'AmazonRDS') &
(df['lineItem/ResourceId'].notna())]
if not rds_df.empty:
# Group by 'lineItem/UsageAccountId', 'lineItem/ResourceId', and 'lineItem/UsageStartDate'
grouped_rds = rds_df.groupby(['lineItem/UsageAccountId', 'lineItem/ResourceId', rds_df['lineItem/UsageStartDate'].dt.date]).agg(
total_usage=('lineItem/UsageAmount', 'sum'),
total_cost=('lineItem/UnblendedCost', 'sum')
).reset_index()
print("Number of rows in result DataFrame:", len(grouped_rds))
# Plotting the RDS cost data for each instance ID
context.plot.xlabel = 'Date'
context.plot.ylabel = 'RDS Instance Usage Cost($)'
context.plot.title = f'RDS Instance Cost Usage (Last {last_n_days} Days)'
for instance_id in grouped_rds['lineItem/ResourceId'].unique():
instance_data = grouped_rds[grouped_rds['lineItem/ResourceId'] == instance_id]
x = instance_data['lineItem/UsageStartDate'].tolist()
y = instance_data['total_cost'].tolist()
print(f"Instance ID: {instance_id}")
print(f"Dates (x values): {x}")
print(f"Costs (y values): {y}")
context.plot.add_trace(name=f"RDS Instance - {instance_id}", xpts=x, ypts=y, tracetype="line")
else:
print("No RDS data available in the specified time frame.")
else:
print("DataFrame is empty. Exiting.")
copied