Skip to content

Commit

Permalink
Merge branch 'feat/sleep'
Browse files Browse the repository at this point in the history
  • Loading branch information
k0rventen committed Jan 26, 2024
2 parents 91108f5 + f6728c2 commit 7ff9909
Show file tree
Hide file tree
Showing 5 changed files with 1,814 additions and 9 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/branch_builds.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build an image for each non-main branch with tag :<branch_name>

permissions:
contents: write

on:
push:
branches:
- '**' # matches every branch
- '!main' # excludes master

jobs:
build-and-release:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Dockerhub login
uses: docker/login-action@v2
with:
username: ${{ github.repository_owner }}
password: ${{ secrets.DOCKER_TOKEN }}
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Inject slug/short variables
uses: rlespinasse/github-slug-action@v4
-
name: Docker build & push
uses: docker/build-push-action@v4
with:
context: ./ingester
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ github.repository }}-ingester:${{ env.GITHUB_REF_SLUG }}
17 changes: 12 additions & 5 deletions ingester/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from typing import Any, Union
import subprocess

from formatters import parse_date_as_timestamp, parse_float_with_try, AppleStandHourFormatter
from formatters import parse_date_as_timestamp, parse_float_with_try, AppleStandHourFormatter, SleepAnalysisFormatter

import gpxpy
from gpxpy.gpx import GPXTrackPoint
from influxdb import InfluxDBClient
Expand Down Expand Up @@ -56,18 +57,20 @@ def format_record(record: dict[str, Any]) -> dict[str, Any]:

if measurement == "AppleStandHour":
return AppleStandHourFormatter(record)
if measurement == "SleepAnalysis":
return SleepAnalysisFormatter(record)

date = parse_date_as_timestamp(record.get("startDate", 0))
value = parse_float_with_try(record.get("value", 1))
unit = record.get("unit", "unit")
device = record.get("sourceName", "unknown")

return {
return [{
"measurement": measurement,
"time": date,
"fields": {"value": value},
"tags": {"unit": unit, "device": device},
}
}]


def format_workout(record: dict[str, Any]) -> dict[str, Any]:
Expand Down Expand Up @@ -136,7 +139,11 @@ def process_health_data(client: InfluxDBClient) -> None:
context = etree.iterparse(export_file,recover=True)
for _, elem in context:
if elem.tag == "Record":
records.append(format_record(elem))
rec = format_record(elem)
if isinstance(rec,list):
records += rec
else:
records.append(format_record(elem))
elem.clear()
elif elem.tag == "Workout":
records.append(format_workout(elem))
Expand Down Expand Up @@ -180,4 +187,4 @@ def process_health_data(client: InfluxDBClient) -> None:

process_workout_routes(client)
process_health_data(client)
print("All done! You can now check grafana.")
print("All done! You can now check grafana.")
50 changes: 49 additions & 1 deletion ingester/formatters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from datetime import datetime as dt
from datetime import timedelta

from typing import Any, Union


Expand Down Expand Up @@ -28,4 +30,50 @@ def AppleStandHourFormatter(record: dict) -> dict:
"time": date,
"fields": {"value": value},
"tags": {"unit": unit, "device": device},
}
}


sleep_states_lookup={
"HKCategoryValueSleepAnalysisAsleepDeep":0,
"HKCategoryValueSleepAnalysisAsleepCore":1,
"HKCategoryValueSleepAnalysisAsleepREM":2,
#"HKCategoryValueSleepAnalysisAsleepUnspecified":3,
"HKCategoryValueSleepAnalysisInBed": 3,
"HKCategoryValueSleepAnalysisAwake":4,
}

sleep_states_short_lookup={
"HKCategoryValueSleepAnalysisAsleepDeep":"Deep",
"HKCategoryValueSleepAnalysisAsleepCore":"Core",
"HKCategoryValueSleepAnalysisAsleepREM":"REM",
#"HKCategoryValueSleepAnalysisAsleepUnspecified":"Asleep",
"HKCategoryValueSleepAnalysisInBed": "Asleep",
"HKCategoryValueSleepAnalysisAwake":"Awake",
}

def SleepAnalysisFormatter(record: dict) -> dict:
start_date = dt.fromisoformat(record.get("startDate"))
start_date.replace(second=0)
end_date = dt.fromisoformat(record.get("endDate"))
device = record.get("sourceName", "unknown")
state = sleep_states_lookup.get(record.get("value"),5)
if "Apple Watch" in device:
print(start_date,end_date,device,state)

minutes_in_bed = []
while start_date <= end_date:
minutes_in_bed.append({
'measurement':"SleepAnalysisTimes-{}".format(device),
"time":int(start_date.timestamp()),
"fields": {"value":state},
"tags": {}
})
start_date += timedelta(minutes=1)

minutes_in_bed.append({
'measurement':'SleepAnalysis',
"time":start_date,
"fields": {"start": int(dt.fromisoformat(record.get("startDate")).timestamp()),"stop":int(dt.fromisoformat(record.get("endDate")).timestamp())},
"tags": {"unit": 'seconds', "device": device,'state':sleep_states_short_lookup.get(record.get("value"),"Unspecified")}
})
return minutes_in_bed

0 comments on commit 7ff9909

Please sign in to comment.