-
Notifications
You must be signed in to change notification settings - Fork 336
/
interval-merge.py
executable file
·56 lines (52 loc) · 1.72 KB
/
interval-merge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
# merge multiple --perf-output files. requires header
from __future__ import print_function
import csv
import argparse
from collections import OrderedDict, Counter
import sys
ap = argparse.ArgumentParser()
ap.add_argument('csvfiles', nargs='+', type=argparse.FileType('r'))
args = ap.parse_args()
def genkey(c, hdr, count):
k = [count]
if 'Timestamp' in hdr:
k.append(c[hdr['Timestamp']])
if 'Location' in hdr:
k.append(c[hdr['Location']])
k.append(c[hdr['Event']])
return tuple(k)
d = OrderedDict()
hdr = None
hdrl = None
prev = Counter()
for fh in args.csvfiles:
csvf = csv.reader(fh, delimiter=';')
for c in csvf:
if hdr is None:
hdrl = c
hdr = dict([(y,x) for x,y in enumerate(c)])
continue
if c[0] in ("Timestamp", "Location", "Value"):
continue
pkey = (fh, c[hdr['Timestamp']] if 'Timestamp' in hdr else None, c[hdr['Event']])
prev[pkey] += 1
key = genkey(c, hdr, prev[pkey])
try:
if key in d:
o = d[key]
o[hdr['Run-Time']] += float(c[hdr['Run-Time']])
o[hdr['Enabled']] = (float(o[hdr['Enabled']]) + o[hdr['Enabled']]) / 2
o[hdr['Value']] += float(c[hdr['Value']])
else:
d[key] = c
o = d[key]
o[hdr['Value']] = float(c[hdr['Value']])
o[hdr['Enabled']] = float(c[hdr['Enabled']])
o[hdr['Run-Time']] = float(c[hdr['Run-Time']])
except ValueError as e:
print("cannot parse", c, e, file=sys.stderr)
csvf = csv.writer(sys.stdout, delimiter=';')
csvf.writerow(hdrl)
for j in d.values():
csvf.writerow(j)