|
| 1 | +import csv |
| 2 | +import sys |
| 3 | +from dateutil.parser import parse as parse_date |
| 4 | +from plotman.log_parser import PlotLogParser |
| 5 | +from plotman.plotinfo import PlotInfo |
| 6 | + |
| 7 | +def export(logfilenames, save_to = None): |
| 8 | + if save_to is None: |
| 9 | + send_to_stdout(logfilenames) |
| 10 | + else: |
| 11 | + save_to_file(logfilenames, save_to) |
| 12 | + |
| 13 | +def save_to_file(logfilenames, filename: str): |
| 14 | + with open(filename, 'w') as file: |
| 15 | + generate(logfilenames, file) |
| 16 | + |
| 17 | +def send_to_stdout(logfilenames): |
| 18 | + generate(logfilenames, sys.stdout) |
| 19 | + |
| 20 | +def header(writer): |
| 21 | + writer.writerow([ |
| 22 | + 'Plot ID', |
| 23 | + 'Started at', |
| 24 | + 'Date', |
| 25 | + 'Size', |
| 26 | + 'Buffer', |
| 27 | + 'Buckets', |
| 28 | + 'Threads', |
| 29 | + 'Tmp dir 1', |
| 30 | + 'Tmp dir 2', |
| 31 | + 'Phase 1 duration (raw)', |
| 32 | + 'Phase 1 duration', |
| 33 | + 'Phase 1 duration (minutes)', |
| 34 | + 'Phase 1 duration (hours)', |
| 35 | + 'Phase 2 duration (raw)', |
| 36 | + 'Phase 2 duration', |
| 37 | + 'Phase 2 duration (minutes)', |
| 38 | + 'Phase 2 duration (hours)', |
| 39 | + 'Phase 3 duration (raw)', |
| 40 | + 'Phase 3 duration', |
| 41 | + 'Phase 3 duration (minutes)', |
| 42 | + 'Phase 3 duration (hours)', |
| 43 | + 'Phase 4 duration (raw)', |
| 44 | + 'Phase 4 duration', |
| 45 | + 'Phase 4 duration (minutes)', |
| 46 | + 'Phase 4 duration (hours)', |
| 47 | + 'Total time (raw)', |
| 48 | + 'Total time', |
| 49 | + 'Total time (minutes)', |
| 50 | + 'Total time (hours)', |
| 51 | + 'Copy time (raw)', |
| 52 | + 'Copy time', |
| 53 | + 'Copy time (minutes)', |
| 54 | + 'Copy time (hours)', |
| 55 | + 'Filename' |
| 56 | + ]) |
| 57 | + |
| 58 | +def parse_logs(logfilenames): |
| 59 | + parser = PlotLogParser() |
| 60 | + result = [] |
| 61 | + |
| 62 | + for filename in logfilenames: |
| 63 | + info = parser.parse(filename) |
| 64 | + |
| 65 | + if not info.is_empty(): |
| 66 | + result.append(info) |
| 67 | + |
| 68 | + result.sort(key=log_sort_key) |
| 69 | + return result |
| 70 | + |
| 71 | +def log_sort_key(element: PlotInfo): |
| 72 | + return parse_date(element.started_at).replace(microsecond=0).isoformat() |
| 73 | + |
| 74 | +def generate(logfilenames, out): |
| 75 | + writer = csv.writer(out) |
| 76 | + logs = parse_logs(logfilenames) |
| 77 | + |
| 78 | + for info in logs: |
| 79 | + writer.writerow([ |
| 80 | + info.plot_id, |
| 81 | + info.started_at, |
| 82 | + parse_date(info.started_at).strftime("%b %d"), |
| 83 | + info.plot_size, |
| 84 | + info.buffer, |
| 85 | + info.buckets, |
| 86 | + info.threads, |
| 87 | + info.tmp_dir1, |
| 88 | + info.tmp_dir2, |
| 89 | + info.phase1_duration_raw, |
| 90 | + info.phase1_duration, |
| 91 | + info.phase1_duration_minutes, |
| 92 | + info.phase1_duration_hours, |
| 93 | + info.phase2_duration_raw, |
| 94 | + info.phase2_duration, |
| 95 | + info.phase2_duration_minutes, |
| 96 | + info.phase2_duration_hours, |
| 97 | + info.phase3_duration_raw, |
| 98 | + info.phase3_duration, |
| 99 | + info.phase3_duration_minutes, |
| 100 | + info.phase3_duration_hours, |
| 101 | + info.phase4_duration_raw, |
| 102 | + info.phase4_duration, |
| 103 | + info.phase4_duration_minutes, |
| 104 | + info.phase4_duration_hours, |
| 105 | + info.total_time_raw, |
| 106 | + info.total_time, |
| 107 | + info.total_time_minutes, |
| 108 | + info.total_time_hours, |
| 109 | + info.copy_time_raw, |
| 110 | + info.copy_time, |
| 111 | + info.copy_time_minutes, |
| 112 | + info.copy_time_hours, |
| 113 | + info.filename |
| 114 | + ]) |
0 commit comments