agent: |
d2yYbGXJOr9KoympigMSDaily Cost of EC2 Instance Types
Daily Cost of EC2 Instance Types
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task visualizes daily costs associated with various Amazon EC2 instance types over a chosen period, highlighting their unique cost structures. Given the diverse configurations of each EC2 instance type, understanding their individual cost dynamics is essential. The chart provides a daily cost breakdown for each type, helping users discern the most cost-intensive instances and their peak periods. This detailed view aids in optimizing cloud expenses, guiding users in selecting the most cost-effective instance types, reshaping usage habits, and allocating resources wisely.
inputs
outputs
import pandas as pd
from datetime import datetime, timedelta, timezone
if df is not None:
# Convert 'lineItem/UsageStartDate' to datetime
df['lineItem/UsageStartDate'] = pd.to_datetime(df['lineItem/UsageStartDate'])
# Filter out negative 'lineItem/UnblendedCost'
df = df[df['lineItem/UnblendedCost'] >= 0]
# Filter rows for the last N days
cutoff_date = datetime.utcnow().replace(tzinfo=timezone.utc) - timedelta(days=int(last_n_days))
last_n_days_df = df[df['lineItem/UsageStartDate'] > cutoff_date].copy()
# Further filter data based on product family and pricing unit
condition = (last_n_days_df['product/productFamily'] == 'Compute Instance') & (last_n_days_df['pricing/unit'].isin(['Hours', 'Hrs']))
filtered_df = last_n_days_df[condition].copy()
# Group by 'lineItem/UsageStartDate' and 'product/instanceType', then sum 'lineItem/UsageAmount' and 'lineItem/UnblendedCost'
result = filtered_df.groupby([filtered_df['lineItem/UsageStartDate'].dt.date, 'product/instanceType']).agg(
usage_hours=('lineItem/UsageAmount', 'sum'),
usage_cost=('lineItem/UnblendedCost', 'sum')
).reset_index()
# Set the properties for your plot
context.plot.xlabel = 'Date'
context.plot.ylabel = 'EC2 Usage Cost($)'
context.plot.title = f'EC2 Cost Usage (Last {last_n_days} Days)'
# Loop through each unique instance type and add a trace for each
for instance_type in result['product/instanceType'].unique():
instance_data = result[result['product/instanceType'] == instance_type]
x = instance_data['lineItem/UsageStartDate'].tolist() # Date on x-axis
y = instance_data['usage_cost'].tolist() # Usage cost on y-axis
context.plot.add_trace(name=f"EC2- {instance_type}", xpts=x, ypts=y, tracetype="line")
#print("Analysis complete.")
else:
print("Failed to fetch data. Exiting.")
copied