Skip to content

Commit 4432750

Browse files
author
Rafael Steil
committed
Add command to export log files to CSV
1 parent 1f95717 commit 4432750

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

src/plotman/csv_exporter.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import csv
2+
import sys
3+
from dateutil.parser import parse as parse_date
4+
from plotman.log_parser import PlotLogParser
5+
6+
def export(logfilenames, save_to = None):
7+
if save_to is None:
8+
__send_to_stdout(logfilenames)
9+
else:
10+
__save_to_file(logfilenames, save_to)
11+
12+
def __save_to_file(logfilenames, filename: str):
13+
with open(filename, 'w') as file:
14+
__generate(logfilenames, file)
15+
16+
def __send_to_stdout(logfilenames):
17+
__generate(logfilenames, sys.stdout)
18+
19+
def __generate(logfilenames, out):
20+
writer = csv.writer(out)
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+
parser = PlotLogParser()
59+
60+
for filename in logfilenames:
61+
info = parser.parse(filename)
62+
63+
if info.is_empty():
64+
continue
65+
66+
writer.writerow([
67+
info.plot_id,
68+
info.started_at,
69+
parse_date(info.started_at).strftime("%b %d"),
70+
info.plot_size,
71+
info.buffer,
72+
info.buckets,
73+
info.threads,
74+
info.tmp_dir1,
75+
info.tmp_dir2,
76+
info.phase1_duration_raw,
77+
info.phase1_duration,
78+
info.phase1_duration_minutes,
79+
info.phase1_duration_hours,
80+
info.phase2_duration_raw,
81+
info.phase2_duration,
82+
info.phase2_duration_minutes,
83+
info.phase2_duration_hours,
84+
info.phase3_duration_raw,
85+
info.phase3_duration,
86+
info.phase3_duration_minutes,
87+
info.phase3_duration_hours,
88+
info.phase4_duration_raw,
89+
info.phase4_duration,
90+
info.phase4_duration_minutes,
91+
info.phase4_duration_hours,
92+
info.total_time_raw,
93+
info.total_time,
94+
info.total_time_minutes,
95+
info.total_time_hours,
96+
info.copy_time_raw,
97+
info.copy_time,
98+
info.copy_time_minutes,
99+
info.copy_time_hours,
100+
info.filename
101+
])

src/plotman/plotman.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import importlib
33
import importlib.resources
44
import os
5+
import glob
56
import random
67
from shutil import copyfile
78
import time
89
import datetime
910

1011
# Plotman libraries
11-
from plotman import analyzer, archive, configuration, interactive, manager, plot_util, reporting
12+
from plotman import analyzer, archive, configuration, interactive, manager, plot_util, reporting, csv_exporter
1213
from plotman import resources as plotman_resources
1314
from plotman.job import Job
1415

@@ -39,6 +40,9 @@ def parse_args(self):
3940

4041
sp.add_parser('archive', help='move completed plots to farming location')
4142

43+
p_export_csv = sp.add_parser('export_csv', help='exports metadata from the plot logs as CSV')
44+
p_export_csv.add_argument('-o', dest='save_to', default=None, type=str, help='save to file. Optional, prints to stdout by default')
45+
4246
p_config = sp.add_parser('config', help='display or generate plotman.yaml configuration')
4347
sp_config = p_config.add_subparsers(dest='config_subcommand')
4448
sp_config.add_parser('generate', help='generate a default plotman.yaml file and print path')
@@ -159,8 +163,15 @@ def main():
159163
analyzer.analyze(args.logfile, args.clipterminals,
160164
args.bytmp, args.bybitfield)
161165

166+
#
167+
# Exports log metadata to CSV
168+
#
169+
elif args.cmd == 'export_csv':
170+
logfilenames = glob.glob(os.path.join(cfg.directories.log, '*'))
171+
csv_exporter.export(logfilenames, args.save_to)
172+
162173
else:
163-
jobs = Job.get_running_jobs(cfg.directories.log)
174+
jobs = Job.get_running_jobs(cfg.directories.log())
164175

165176
# Status report
166177
if args.cmd == 'status':

0 commit comments

Comments
 (0)