utilities/gadgetbridge_to_influxdb#13: Time spent at stress levels



Issue Information

Issue Type: issue
Status: closed
Reported By: btasker
Assigned To: btasker

Milestone: v0.4
Created: 27-Aug-23 13:24



Description

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.



Toggle State Changes

Activity


assigned to @btasker

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:

SELECT
    TIMESTAMP,
    DEVICE_ID,
    USER_ID,
    TYPE_NUM,
    STRESS,
    LEAD (TIMESTAMP, 1) OVER (
        PARTITION BY 
            DEVICE_ID, 
            USER_ID, 
            TYPE_NUM
        ORDER BY TIMESTAMP
    ) NEXT_TS
FROM HUAMI_STRESS_SAMPLE
WHERE type_num=1;

(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 and NEXT_TS to create 1 minute points with STRESS 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.

verified

mentioned in commit 5e1c7cb20964ee915dfd690a927e80436037d3cd

Commit: 5e1c7cb20964ee915dfd690a927e80436037d3cd 
Author: B Tasker                            
                            
Date: 2023-08-27T14:44:56.000+01:00 

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 generation

+43 -1 (44 lines changed)

The stats can now be visualised with

SELECT sum("stress_level_counter_unknown") AS "unknown",  sum("stress_level_counter_relaxed") AS "relaxed", sum("stress_level_counter_normal") AS "normal",  sum("stress_level_counter_medium") AS "medium", sum("stress_level_counter_high") AS "high" FROM "testing_db"."autogen"."gadgetbridge" WHERE  $timeFilter 

Screenshot_20230827_144735

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)

verified

mentioned in commit 51491089f333284ce2aba6dca6a53e6af223edef

Commit: 51491089f333284ce2aba6dca6a53e6af223edef 
Author: B Tasker                            
                            
Date: 2023-08-27T14:57:55.000+01:00 

Message

Create a counter field for non-sleeping hours (utilities/gadgetbridge_to_influxdb#13)

+7 -1 (8 lines changed)

We can now generate a Pie chart for each

Screenshot_20230827_145726

The fields listing on the wiki has been updated, closing this out as Done.

mentioned in issue #14