import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates # Read the CSV file def read_csv_data(file_path): """ Read CSV file with request data Parameters: ----------- file_path : str Path to the CSV file Returns: -------- pandas.DataFrame DataFrame with parsed request data """ # Read the CSV file df = pd.read_csv(file_path, parse_dates=['Timestamp']) # Ensure data types are correct df['Duration (ms)'] = df['Duration (ms)'].astype(int) df['Status Code'] = df['Status Code'].astype(int) return df # Visualize the data def visualize_request_data(df): """ Create a dual-axis plot of request durations and status codes Parameters: ----------- df : pandas.DataFrame DataFrame containing request data """ # Create the figure and the first axis fig, ax1 = plt.subplots(figsize=(12, 6)) # Plot Duration on the left axis color1 = 'blue' ax1.set_xlabel('Timestamp') ax1.set_ylabel('Duration (ms)', color=color1) ax1.plot(df['Timestamp'], df['Duration (ms)'], color=color1, label='Duration (ms)') ax1.tick_params(axis='y', labelcolor=color1) # Format x-axis to show timestamps nicely plt.gcf().autofmt_xdate() ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) # Title and legend plt.title('Request Data over Time') # Create a legend lines1, labels1 = ax1.get_legend_handles_labels() # Add grid ax1.grid(True, linestyle='--', alpha=0.7) # Tight layout to prevent cutting off labels plt.tight_layout() # Show the plot plt.show() # Main execution def main(): # Path to your CSV file file_path = 'client_metrics.csv' try: # Read the data df = read_csv_data(file_path) # Visualize the data visualize_request_data(df) except Exception as e: print(f"An error occurred: {e}") # Demonstrate data summary def print_data_summary(df): """ Print a summary of the request data Parameters: ----------- df : pandas.DataFrame DataFrame containing request data """ print("\nData Summary:") print("-" * 20) print(f"Total Requests: {len(df)}") print(f"Unique Request Types: {df['Request Type'].unique()}") print("\nDuration Statistics:") print(df['Duration (ms)'].describe()) if __name__ == '__main__': main()