agent: |
T0jor5KG4bxHfxkBMVyprds cost analysis by instance id
rds cost analysis 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/UsageStartDate' and 'lineItem/ResourceId'
rds_result = rds_df.groupby([rds_df['lineItem/UsageStartDate'].dt.date, 'lineItem/ResourceId']).agg(
usage_hours=('lineItem/UsageAmount', 'sum'),
usage_cost=('lineItem/UnblendedCost', 'sum')
).reset_index()
print("Number of rows in result DataFrame:", len(rds_result))
# Plotting the RDS cost data by instance ID
context.plot.xlabel = 'Date'
context.plot.ylabel = 'RDS Usage Cost($)'
context.plot.title = f'RDS Cost by Instance ID (Last {last_n_days} Days)'
for instance_id in rds_result['lineItem/ResourceId'].unique():
instance_data = rds_result[rds_result['lineItem/ResourceId'] == instance_id]
x = instance_data['lineItem/UsageStartDate'].tolist()
y = instance_data['usage_cost'].tolist()
context.plot.add_trace(name=f"RDS Instance - {instance_id}", xpts=x, ypts=y, tracetype="line")
else:
print("No RDS data available for the specified time frame.")
else:
print("DataFrame is empty. Exiting.")
copied