RRD: automatic colors selection.
Change-Id: I95f564c8a6b9802c896eb5495dbf28616ef50f43
This commit is contained in:
parent
c55b114d5b
commit
01fb3970c8
|
@ -18,6 +18,10 @@ rrd_dir = /var/lib/kwapi/kwapi-rrd
|
||||||
kwh_price = 0.125
|
kwh_price = 0.125
|
||||||
|
|
||||||
# Graphs
|
# Graphs
|
||||||
|
color = #BBFF99
|
||||||
|
hues = 10
|
||||||
|
min_brightness = 10
|
||||||
|
max_brightness = 60
|
||||||
max_watts = 200
|
max_watts = 200
|
||||||
refresh_interval = 5
|
refresh_interval = 5
|
||||||
|
|
||||||
|
|
|
@ -17,11 +17,12 @@
|
||||||
"""Defines functions to build and update rrd database and graph."""
|
"""Defines functions to build and update rrd database and graph."""
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
|
import colorsys
|
||||||
import errno
|
import errno
|
||||||
import itertools
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
import struct
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
@ -41,6 +42,15 @@ rrd_opts = [
|
||||||
cfg.FloatOpt('kwh_price',
|
cfg.FloatOpt('kwh_price',
|
||||||
required=True,
|
required=True,
|
||||||
),
|
),
|
||||||
|
cfg.IntOpt('hues',
|
||||||
|
required=True,
|
||||||
|
),
|
||||||
|
cfg.IntOpt('min_brightness',
|
||||||
|
required=True,
|
||||||
|
),
|
||||||
|
cfg.IntOpt('max_brightness',
|
||||||
|
required=True,
|
||||||
|
),
|
||||||
cfg.IntOpt('max_watts',
|
cfg.IntOpt('max_watts',
|
||||||
required=True,
|
required=True,
|
||||||
),
|
),
|
||||||
|
@ -50,6 +60,9 @@ rrd_opts = [
|
||||||
cfg.MultiStrOpt('watch_probe',
|
cfg.MultiStrOpt('watch_probe',
|
||||||
required=False,
|
required=False,
|
||||||
),
|
),
|
||||||
|
cfg.StrOpt('color',
|
||||||
|
required=True,
|
||||||
|
),
|
||||||
cfg.StrOpt('driver_metering_secret',
|
cfg.StrOpt('driver_metering_secret',
|
||||||
required=True,
|
required=True,
|
||||||
),
|
),
|
||||||
|
@ -77,13 +90,31 @@ scales['month'] = {'interval': 2678400, 'resolution': 21600, 'label': 'month'},
|
||||||
# Resolution = 1 week
|
# Resolution = 1 week
|
||||||
scales['year'] = {'interval': 31622400, 'resolution': 604800, 'label': 'year'},
|
scales['year'] = {'interval': 31622400, 'resolution': 604800, 'label': 'year'},
|
||||||
|
|
||||||
colors = ['#EA644A', '#EC9D48', '#ECD748', '#54EC48', '#48C4EC', '#7648EC',
|
|
||||||
'#DE48EC', '#8A8187']
|
|
||||||
probes = set()
|
probes = set()
|
||||||
probe_colors = {}
|
probe_colors = {}
|
||||||
lock = Lock()
|
lock = Lock()
|
||||||
|
|
||||||
|
|
||||||
|
def color_generator():
|
||||||
|
step = (cfg.CONF.max_brightness-cfg.CONF.min_brightness) / \
|
||||||
|
float(cfg.CONF.hues)
|
||||||
|
rgb_base = struct.unpack('BBB',
|
||||||
|
cfg.CONF.color.strip('#').decode('hex'))
|
||||||
|
i = cfg.CONF.min_brightness
|
||||||
|
while True:
|
||||||
|
hls = colorsys.rgb_to_hls(rgb_base[0]/255.0,
|
||||||
|
rgb_base[1]/255.0,
|
||||||
|
rgb_base[2]/255.0)
|
||||||
|
new_rgb = colorsys.hls_to_rgb(hls[0],
|
||||||
|
(i+step)/100.0,
|
||||||
|
hls[2])
|
||||||
|
new_rgb = tuple([int(x*255) for x in new_rgb])
|
||||||
|
i += step
|
||||||
|
if i > cfg.CONF.max_brightness:
|
||||||
|
i = cfg.CONF.min_brightness
|
||||||
|
yield '#' + struct.pack('BBB', *new_rgb).encode('hex')
|
||||||
|
|
||||||
|
|
||||||
def create_dirs():
|
def create_dirs():
|
||||||
"""Creates all required directories."""
|
"""Creates all required directories."""
|
||||||
directories = []
|
directories = []
|
||||||
|
@ -294,7 +325,7 @@ def listen():
|
||||||
LOG.error('Malformed message (missing required key)')
|
LOG.error('Malformed message (missing required key)')
|
||||||
else:
|
else:
|
||||||
if not probe in probes:
|
if not probe in probes:
|
||||||
color_seq = itertools.cycle(colors)
|
color_seq = color_generator()
|
||||||
lock.acquire()
|
lock.acquire()
|
||||||
probes.add(probe)
|
probes.add(probe)
|
||||||
for probe in sorted(probes):
|
for probe in sorted(probes):
|
||||||
|
|
Loading…
Reference in New Issue