A comprehensive Python tool for analyzing API performance logs from CSV files. Track success rates, response times, and performance metrics across different time periods with clean, organized output and optional data visualizations.
- Multi-file Processing: Analyze multiple CSV files at once using glob patterns
- Time-based Analysis: Get metrics for today, this week, this month, last 7/30 days, and all-time
- Comprehensive Metrics: Success rates, response times, percentiles, and failure analysis
- Multiple Output Formats: Pretty console output, JSON, or raw dictionary
- Data Visualization: Optional chart generation with detailed performance graphs
- Robust Data Handling: Automatic data cleaning and validation
- Performance Insights: Identify bottlenecks and track API health over time
pip install pandas numpy matplotlib seabornYour CSV files should contain the following columns:
timestamp: Request timestamp (ISO format recommended)api_call: API endpoint URLlatency_ms: Response time in millisecondsstatus: Request status (successorfailure)
Example CSV structure:
timestamp,api_call,latency_ms,status
"2025-09-15T16:02:40.057164","https://api.example.com/endpoint",7575,success
"2025-09-15T16:03:10.784079","https://api.example.com/endpoint",4512,success
"2025-09-15T16:08:59.289937","https://api.example.com/endpoint",7986,failure
from api_log_analyzer import analyze_api_logs
# Analyze all CSV files in current directory with pretty output
results = analyze_api_logs("*.csv", "pretty")
# Generate charts automatically
results = analyze_api_logs("*.csv", "pretty", generate_charts=True)# Analyze specific files with JSON output
results = analyze_api_logs("api_logs_*.csv", "json")
# Analyze files in a specific directory with charts
results = analyze_api_logs("logs/*.csv", "pretty", generate_charts=True)
# Process files matching a specific pattern
results = analyze_api_logs("charging_station_logs_2025*.csv", "dict")When running the script directly, it will prompt you after showing results:
Would you like to generate visualization charts? (y/N): y
"pretty": Formatted console output with clear sections"json": JSON formatted output for external processing"dict": Raw Python dictionary for programmatic use
================================================================================
API LOG ANALYSIS RESULTS
================================================================================
ANALYSIS SUMMARY
Analysis Date: 2025-09-17 15:30:45
Files Analyzed: 3
Date Range: 2025-09-15 16:02:40 to 2025-09-17 11:26:58
Unique Endpoints: 1
Total Records: 42
TODAY
--------------------------------------------------
Total Requests: 15
Success: 13 (86.7%)
Failures: 2 (13.3%)
RESPONSE TIME METRICS (ms)
Average: 9,234
Median: 7,155
Min: 3,950
Max: 91,273
95th percentile: 20,001
99th percentile: 91,273
SUCCESS RESPONSE TIMES (ms)
Average: 6,847
Min: 3,950
Max: 18,768
FAILURE RESPONSE TIMES (ms)
Average: 30,871
The tool generates a comprehensive dashboard with four charts:
- Success Rate Over Time: Line chart showing success percentage by hour
- Response Time Distribution: Histogram comparing success vs failure response times
- Average Response Time Over Time: Trend line of response times by hour
- Request Counts by Time Period: Bar chart showing success/failure counts across different periods
Charts are automatically saved as PNG files with timestamps (e.g., api_log_analysis_20250917_153045.png).
- Total Requests: Total number of API calls
- Success Count/Rate: Number and percentage of successful requests
- Failure Count/Rate: Number and percentage of failed requests
- Average: Mean response time across all requests
- Median: 50th percentile response time
- Min/Max: Fastest and slowest response times
- 95th/99th Percentile: Response times for 95% and 99% of requests
- Success/Failure Averages: Separate averages for successful and failed requests
- Today: From midnight of current day
- This Week: From Monday of current week
- This Month: From 1st day of current month
- Last 7 Days: Rolling 7-day window
- Last 30 Days: Rolling 30-day window
- All Time: Complete dataset
analyze_api_logs(csv_files_pattern="*.csv", output_format="json", generate_charts=False)csv_files_pattern(str): Glob pattern to match CSV files- Examples:
"*.csv","logs/*.csv","api_logs_2025*.csv"
- Examples:
output_format(str): Output format"pretty": Formatted console output"json": JSON string output"dict": Raw dictionary (default)
generate_charts(bool): Whether to create visualization chartsTrue: Generate and save charts automaticallyFalse: No chart generation (default)
- Dictionary: Contains analysis results for all time periods plus summary information
api-log-analyzer/
├── api_log_analyzer.py # Main analysis function
├── README.md # This file
├── requirements.txt # Python dependencies
└── sample_logs/ # Example CSV files
├── api_logs_2025-09-15.csv
├── api_logs_2025-09-16.csv
└── api_logs_2025-09-17.csv
The analyzer handles common issues automatically:
- Missing files: Warns when no files match the pattern
- Invalid timestamps: Skips rows with unparseable dates
- Missing data: Handles null/empty values gracefully
- File read errors: Reports and skips problematic files
- Empty datasets: Returns zero-filled metrics for empty time periods
- Chart generation errors: Continues analysis even if visualization fails
import json
from datetime import datetime
# Analyze logs and generate charts
results = analyze_api_logs("production_logs_*.csv", "dict", generate_charts=True)
# Extract key metrics
today_success_rate = results['today']['success_rate']
avg_response_time = results['today']['avg_response_time_ms']
# Save results to file
with open(f'api_analysis_{datetime.now().strftime("%Y%m%d")}.json', 'w') as f:
json.dump({k: v for k, v in results.items() if k != '_dataframe'}, f, indent=2, default=str)
# Print summary
print(f"Today's Success Rate: {today_success_rate:.1f}%")
print(f"Average Response Time: {avg_response_time:.0f}ms")
# Check if charts were generated
chart_files = glob.glob("api_log_analysis_*.png")
if chart_files:
print(f"Charts saved: {', '.join(chart_files)}")Create a requirements.txt file with:
pandas>=1.3.0
numpy>=1.20.0
matplotlib>=3.3.0
seaborn>=0.11.0
Install with:
pip install -r requirements.txtRun directly from command line:
python api_log_analyzer.pyThe script will:
- Analyze all CSV files in the current directory
- Display results in the terminal
- Prompt if you want to generate charts
- Save visualization files if requested
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT License - feel free to use this in your projects!
If you encounter any issues or have feature requests, please:
- Check existing issues first
- Provide sample CSV data (anonymized)
- Include error messages and Python version
- Describe expected vs actual behavior
Happy API Monitoring!