From 4a24a47175209ab471f0c3f145e14bf4085a383f Mon Sep 17 00:00:00 2001 From: Alfredo Oliviero Date: Thu, 11 Jul 2024 14:24:32 +0200 Subject: [PATCH] arg parse --- analyze_startup.py | 69 ++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/analyze_startup.py b/analyze_startup.py index 555e346..a146485 100644 --- a/analyze_startup.py +++ b/analyze_startup.py @@ -4,18 +4,7 @@ import re import os from datetime import datetime import json - -# Optional variable to define the directory of logs -logs_dir = None # Example: "./logs" - -# Explicit list of files and labels -files_and_labels = [ - # ("./logs/startup_lr62-pre-02.log", "lr62-pre-02"), - # ("./logs/startup_lr62-pre-01.log", "lr62-pre-01"), - # ("./logs/startup_lr62-dev.log", "lr62-dev"), - ("./logs/startup_lr62-prod-02.log", "lr62-prod-02"), - ("./logs/startup_lr62-prod-01.log", "lr62-prod-01"), -] +import argparse # Function to load data from files def load_data(file_path): @@ -43,9 +32,45 @@ def find_files_and_labels(logs_dir): files_and_labels.append((file_path, label)) return files_and_labels -# If logs_dir is defined, populate files_and_labels from the directory -if logs_dir: - files_and_labels = find_files_and_labels(logs_dir) +# Parsing command-line arguments +parser = argparse.ArgumentParser( + description='Process server startup logs.', + epilog=''' +Examples of usage: + +1. Use log directory and results directory with a specific date range: + python analyze_startup.py --log-dir ./logs --results-dir ./results --min-date 2023-07-01 --max-date 2023-12-31 + +2. Use specific log files with automatic labels: + python analyze_startup.py -f ./logs/startup_lr62-prod-01.log -f ./logs/startup_lr62-prod-02.log + +3. Use specific log files with custom labels: + python analyze_startup.py -f ./logs/startup_lr62-prod-01.log -l prod01 -f ./logs/startup_lr62-prod-02.log -l prod02 + +4. Combine log directory and specific files: + python analyze_startup.py --log-dir ./logs -f ./logs/startup_lr62-dev.log -l dev --results-dir ./results --min-date 2023-07-01 + ''', + formatter_class=argparse.RawTextHelpFormatter +) +parser.add_argument('--log-dir', type=str, default=None, help='Directory containing log files') +parser.add_argument('--results-dir', type=str, default='./results', help='Directory to save results') +parser.add_argument('--max-date', type=str, default=None, help='Maximum date for filtering logs (YYYY-MM-DD)') +parser.add_argument('--min-date', type=str, default='2023-07-01', help='Minimum date for filtering logs (YYYY-MM-DD)') +parser.add_argument('-f', '--file', type=str, action='append', help='Explicit log file') +parser.add_argument('-l', '--label', type=str, action='append', help='Label for explicit log file') + +args = parser.parse_args() + +# Handling explicit files and labels +files_and_labels = [] +if args.file: + for i, file in enumerate(args.file): + label = args.label[i] if args.label and len(args.label) > i else re.search(r'startup_(.+)\.log', file).group(1) + files_and_labels.append((file, label)) + +# If log_dir is defined, populate files_and_labels from the directory +if args.log_dir: + files_and_labels += find_files_and_labels(args.log_dir) # Print the list of calculated files and labels in a readable format print("files_and_labels = [") @@ -53,20 +78,16 @@ for file_path, label in files_and_labels: print(f' ("{file_path}", "{label}"),') print("]") -min_date_str = "2023-07-01" # Default start date -max_date_str = None # Default end date - # Convert date strings to datetime objects -min_date = pd.to_datetime(min_date_str) if min_date_str else None -max_date = pd.to_datetime(max_date_str) if max_date_str else None +min_date = pd.to_datetime(args.min_date) if args.min_date else None +max_date = pd.to_datetime(args.max_date) if args.max_date else None # Use the current date if max_date_str is None current_date_str = datetime.now().strftime("%Y-%m-%d") -date_range_str = f"{min_date_str}_to_{max_date_str if max_date_str else current_date_str}" +date_range_str = f"{args.min_date}_to_{args.max_date if args.max_date else current_date_str}" # Ensure the results directory exists -results_dir = "./results" -os.makedirs(results_dir, exist_ok=True) +os.makedirs(args.results_dir, exist_ok=True) # Plot setup plt.figure(figsize=(14, 7)) @@ -94,7 +115,7 @@ for file_path, label in files_and_labels: # Create the filename base using the date range and labels labels_str = "_".join([label for _, label in files_and_labels]) -filename_base = f"{results_dir}/server_startup_{labels_str}_{date_range_str}" +filename_base = f"{args.results_dir}/server_startup_{labels_str}_{date_range_str}" png_filename = f"{filename_base}.png" json_filename = f"{filename_base}.json"