Current Ranger Logging data with Redis and Grafana

Started by mayor, March 30, 2021, 01:53:41 PM

mayor

I have a Grafana stack, and decided to add the RedisTimeSeries data source for CurrentRanger data.

Anyone interested, let me know, I'll clean up and share if so.



Felix

I'm interested, and I am sure others would be too  ;)

mayor

#2
Step by step, then

1. Install and start Grafana https://grafana.com/docs/grafana/latest/installation/
2. Install Redis Time Series https://oss.redislabs.com/redistimeseries/
3. Install Redis Time Series as data source in Grafana https://github.com/RedisGrafana/grafana-redis-datasource
4. Set up your Grafana dashboard along the lines of:



5. Copy the following to a directory. It's not trying to be clever, and should easily be modifiable to suit your needs:

import io
import time
import math
import serial
from redistimeseries.client import Client as RedisTimeSeries
#from redis import Redis

redis_host = 'ubuntu'
redis_port = 6379
redis_key = 'energy'
redis_downsample = redis_key + '_sub'
serialdev = '/dev/cu.usbmodem301'
baud = 230400

rts = RedisTimeSeries(port=redis_port,host=redis_host)
rts.redis.flushdb()
rts.create(key=redis_key, retention_msecs=600000) # keep 10 minutes' worth
rts.create(key=redis_downsample, retention_msecs=3600000) # keep downsamples for 1 hour
rts.createrule(\
source_key=redis_key,\
dest_key=redis_downsample,\
aggregation_type='avg',\
bucket_size_msec=100) # aggregate average over 100 ms for downsamples

ser = serial.Serial(serialdev, baud, timeout=1)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser), None, None, newline='\r\n')
sio.write('u')
sio.flush()

# read last few lines of output to determine if usb logging is enabled
i = 0
while i < 4:
    i += 1
    l = sio.readline()
if not l:
    sio.write('u')
    sio.flush()

# log only one data point per millisecond
lastTime = 0

while True:
    t = math.floor(time.time()*1000)
    if t == lastTime:
        continue
    try:
        rts.add(key=redis_key, timestamp=t, value=format(float(sio.readline())*1000,'f'))
        lastTime = t
    except ValueError:
        print('non-data output')


6. pip install redis
7. pip install redistimeseries
8. pip install pyserial

Run the script with python, it should connect to the CurrentRanger and start logging to Redis, and should be viewable in Grafana

Felix


mayor

Added some useful stats... Very happy with the CurrentRanger so far!




mayor

Alternate install steps for Grafana/Redis:

1. Install Docker
2.
git clone https://github.com/RedisGrafana/grafana-redis-datasource

3.
docker-compose up



willemb

exactly what I was looking for.  Thanks for sharing.  Very helpful.