Sign in
agent:

AWS Service Cost Breakdown

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This task efficiently categorizes AWS expenses, providing a visual breakdown through a bar chart. By analyzing unblended costs, it highlights the most expensive AWS services.

import boto3 import pandas as pd from datetime import datetime, timedelta # Define last_n_days parameter #last_n_days = 30 # You can change this value as needed if df is not None: # Filter out negative values filter_condition = (df['lineItem/UnblendedCost'] >= 0) #| (df['lineItem/UnblendedCost'] <= -0.001) df = df[filter_condition] # Convert the 'lineItem/UsageStartDate' to a datetime object df['lineItem/UsageStartDate'] = pd.to_datetime(df['lineItem/UsageStartDate']) # Extract the day of the month and create a new column 'day' df['day'] = df['lineItem/UsageStartDate'].dt.date # Get the latest date in the dataset latest_date = df['day'].max() # Calculate the start date based on the latest date and last_n_days start_date = latest_date - timedelta(days=int(last_n_days)) # Filter data to include only dates greater than or equal to the start date filtered_df = df[df['day'] >= start_date] # Group by 'lineItem/ProductCode' and 'day', then sum 'lineItem/UnblendedCost' for each group result = filtered_df.groupby(['lineItem/ProductCode', 'day'])['lineItem/UnblendedCost'].sum().reset_index() # Get the top_n_services based on total cost top_services = result.groupby('lineItem/ProductCode')['lineItem/UnblendedCost'].sum().nlargest(int(top_n_services)).index.tolist() # Filter the result DataFrame to include only the top_n_services top_result = result[result['lineItem/ProductCode'].isin(top_services)] # Set the properties for your plot context.plot.xlabel = 'Date' context.plot.ylabel = 'Cost ($)' context.plot.title = f'Daily AWS Costs by Top {top_n_services} Services (Last {last_n_days} Days)' # Iterate over each top service in the filtered result DataFrame for service in top_result['lineItem/ProductCode'].unique(): service_data = top_result[top_result['lineItem/ProductCode'] == service] service_data = service_data.sort_values(by='day') x = service_data['day'].tolist() y = service_data['lineItem/UnblendedCost'].tolist() context.plot.add_trace(name=service, xpts=x, ypts=y, tracetype='lines') else: print("Failed to fetch data from dataframe. Exiting.")
copied