We currently capture stress levels over time (i.e. multiple point in time levels), but those don't make it particularly easy to chart out the proportion of time spent at each stress level.
In zepp_to_influxdb
I was able to build a stream of points showing stress level - this was possible because each stress entry gave a start + end date.
It was then possible to iterate over those points with a Flux query to build a piechart
import "date"
from(bucket: "health")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "zepp")
|> filter(fn: (r) => r["_field"] == "current_stress_level")
|> filter(fn: (r) => r["stress"] == "point_in_time")
// Exclude sleeping hours
|> filter(fn: (r) => not contains(value: date.hour(t: r._time),
set: [0,1,2,3,4,5,6]))
// Skip missed reads
|> filter(fn: (r) => r._value > 0)
// Turn the reads into thresholds and counts
// the thresholds used come from the description
// in the apps UI
|> map(fn: (r) => ({
_time: r._time,
_value: 1,
_field: if r._value <= 39
then "relaxed"
else if r._value >= 40 and r._value <= 59
then "normal"
else if r._value >= 60 and r._value <= 79
then "medium"
else if r._value > 80
then "high"
else "unknown"
}))
// Sum the values so we can get the relative proportions
|> sum()
|> keep(columns: ["_field", "_value"])
I'd actually quite like to be able to cut Flux out of the loop on this, but even if that's not (immediately) possible, we need to find a way to generate 1 minute readings indicating stress level so that they can be consumed by something like that query.
Activity
27-Aug-23 13:24
assigned to @btasker
27-Aug-23 13:29
The Gadgetbridge DB doesn't have a start/end field for stress - we have a timestamp for it being measured and the stress reading.
However, we can generate an end time for each point by using
LEAD()
to fetch the timestamp of the next row:(We filter for type_num=1 because that's the type used for the periodic readings).
With that result set, we can iterate between
TIMESTAMP
andNEXT_TS
to create 1 minute points withSTRESS
as a value.To remove the need for Flux when processing, we'll need to also write a counter field (with a value of 1) per stress level so that they can then be summed when generating a pie chart.
27-Aug-23 13:46
mentioned in commit 5e1c7cb20964ee915dfd690a927e80436037d3cd
Message
Generate 1 minute values indicating stress level (utilities/gadgetbridge_to_influxdb#13)
This adjusts the stress query so that we can iterate between timestamps generating a point per minute detailing the current stress level.
We also write a counter value (
1
) into a stress level specific fields for easy pie chart generation27-Aug-23 13:47
The stats can now be visualised with
27-Aug-23 13:48
It'd probably be wise to do similar for the non-sleeping hours stress levels (as that's what led to the development of that Flux query in the first place)
27-Aug-23 13:58
mentioned in commit 51491089f333284ce2aba6dca6a53e6af223edef
Message
Create a counter field for non-sleeping hours (utilities/gadgetbridge_to_influxdb#13)
27-Aug-23 13:58
We can now generate a Pie chart for each
27-Aug-23 14:03
The fields listing on the wiki has been updated, closing this out as Done.
27-Aug-23 16:52
mentioned in issue #14