summaryrefslogtreecommitdiffstats
path: root/plot.py
blob: f4a7dc64d14c408a38ab089da2e5212b5276ea79 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python3

# based on https://github.com/cnvogelg/ardu/blob/master/datalog/tools/plot_log.py

import sys
import subprocess
import os

if len(sys.argv) != 3:
  print("Usage: plot.py <log> <png>", file=sys.stderr)
  sys.exit(1)

log_file = sys.argv[1]
png_file = sys.argv[2]
rrd_file = "tmp.rrd"

create = [
  'rrdtool','create',rrd_file,'--step','30','--start',0,
  'DS:temp:GAUGE:50:U:U',
  'RRA:AVERAGE:0.5:1:10000',
  'RRA:MIN:0.5:1:10000',
  'RRA:MAX:0.5:1:10000'
]

graph = [
  'rrdtool','graph',png_file,
  '-s',None,'-e',None,
  '--height=300','--width=750',
  '--color=BACK#FFFFFF',
  '--vertical-label', '°C',
  '--title','Temperature',
  '-l','25','-u','30',
  'DEF:temp='+rrd_file+':temp:AVERAGE',
  'LINE1:temp#3366CC'
]

samples = []
f = open(log_file,"r")
for line in f:
  elem = line.split(' ')
  temp = float(elem[1].split("=")[1])
  ts = int(elem[0])
  samples.append((ts,temp))
f.close()

# create rrd
start_ts = samples[0][0]
end_ts = samples[-1][0]
create[6] = str(start_ts - 1)
print("creating rrdb:",create)
ret = subprocess.call(create)
if ret != 0:
  print("ERROR calling: {}".format(" ".join(create)))
  sys.exit(1)

# update rrd
n = len(samples)
for i in range(n):
  print(i,n,end="\r")
  sys.stdout.flush()
  s = samples[i]
  entry = "{}:{}".format(s[0],s[1])
  cmd = ['rrdupdate',rrd_file,'--',entry]
  ret = subprocess.call(cmd)
  if ret != 0:
    print("ERROR calling: {}".format(" ".join(cmd)))
    sys.exit(1)

# graph
print("plotting rrdb:",graph)
graph[4] = str(start_ts)
graph[6] = str(end_ts)
ret = subprocess.call(graph)
if ret != 0:
  print("ERROR calling: {}".format(" ".join(cmd)))
  sys.exit(1)

os.unlink(rrd_file)
print("done")