|
| 1 | +import csv |
| 2 | +import re |
| 3 | +import os |
| 4 | + |
| 5 | +def convert_time_to_seconds(time_str): |
| 6 | + h, m, s = time_str.split(':') |
| 7 | + return int(h) * 3600 + int(m) * 60 + float(s) |
| 8 | + |
| 9 | +def extract_log_info_and_generate_csv(log_file_path, output_csv_path, target_directory): |
| 10 | + data = [] |
| 11 | + |
| 12 | + unwanted_chars = re.compile(r'(\x1B\[[0-9;]*m)|([^\x00-\x7F])') |
| 13 | + |
| 14 | + with open(log_file_path, 'r') as file: |
| 15 | + lines = file.readlines() |
| 16 | + |
| 17 | + previous_task = None |
| 18 | + |
| 19 | + for i in range(len(lines)): |
| 20 | + if "TASK [" in lines[i]: |
| 21 | + task_name = lines[i].strip().split('TASK [')[1].split(']')[0] |
| 22 | + |
| 23 | + full_task_path = lines[i + 1].strip().split('task path: ')[1] |
| 24 | + if target_directory in full_task_path: |
| 25 | + start_index = full_task_path.find(target_directory) + len(target_directory) |
| 26 | + partial_task_path = full_task_path[start_index:] |
| 27 | + else: |
| 28 | + partial_task_path = full_task_path |
| 29 | + |
| 30 | + partial_task_path = unwanted_chars.sub('', partial_task_path).strip() |
| 31 | + |
| 32 | + time_to_complete = lines[i + 2].strip().split('(')[1].split(')')[0] |
| 33 | + |
| 34 | + if previous_task: |
| 35 | + previous_task[2] = time_to_complete # Shift the time to the previous task |
| 36 | + data.append(previous_task) |
| 37 | + |
| 38 | + previous_task = [task_name, partial_task_path, None] # Placeholder for the next time_to_complete |
| 39 | + |
| 40 | + # Ensure the last task is also included |
| 41 | + if previous_task: |
| 42 | + previous_task[2] = time_to_complete if time_to_complete else 'N/A' |
| 43 | + data.append(previous_task) |
| 44 | + |
| 45 | + # Convert time strings to seconds for sorting |
| 46 | + for row in data: |
| 47 | + if row[2] != 'N/A': |
| 48 | + row[2] = convert_time_to_seconds(row[2]) |
| 49 | + |
| 50 | + # Sort the data by time (now in seconds) |
| 51 | + data.sort(key=lambda x: x[2], reverse=True) |
| 52 | + |
| 53 | + # Convert times back to original string format |
| 54 | + for row in data: |
| 55 | + if isinstance(row[2], float): |
| 56 | + row[2] = f'{int(row[2] // 3600):02}:{int((row[2] % 3600) // 60):02}:{row[2] % 60:.3f}' |
| 57 | + |
| 58 | + # Write the sorted data to a CSV file |
| 59 | + with open(output_csv_path, 'w', newline='') as csvfile: |
| 60 | + csvwriter = csv.writer(csvfile) |
| 61 | + csvwriter.writerow(['Task Name', 'Task Path', 'Time to Complete']) |
| 62 | + csvwriter.writerows(data) |
| 63 | + |
| 64 | + print(f"Data extracted, sorted, and saved to {output_csv_path}") |
| 65 | + |
| 66 | +# File paths |
| 67 | +log_file_path = './RL9-ofed-fatimage-177.txt' |
| 68 | +output_csv_path = 'RL9-ofed-fatimage-177.csv' |
| 69 | +target_directory = '/ansible/' |
| 70 | + |
| 71 | +# Run the function |
| 72 | +extract_log_info_and_generate_csv(log_file_path, output_csv_path, target_directory) |
0 commit comments