Skip to content

Commit

Permalink
Merge pull request #27 from TomPlum/develop
Browse files Browse the repository at this point in the history
Sleep stage breakdown chart improvements
  • Loading branch information
TomPlum authored Nov 17, 2024
2 parents d52b826 + 0237445 commit c6a41eb
Show file tree
Hide file tree
Showing 48 changed files with 1,274 additions and 166 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A simple 2D line chart visualisation of my sleep data as recorded by my Apple Wa
<!-- TOC -->

# Examples
- [Recent sleep quality vs awake time stacked charts](https://tomplum.github.io/sleep?metric=awake_time&start=1722911767000&end=1728182167000&lng=en&stacked=false&metrics=quality%2Cawake_time)
- [Recent sleep quality vs awake time stacked charts](https://tomplum.github.io/sleep?metric=quality&start=1720652400000&end=1731283200000&lng=en&stacked=true&metrics=quality%2Cawake_time)
- [Sleep quality over time across all recorded sessions](https://tomplum.github.io/sleep?metric=quality&start=1534457817000&end=1728199961000&lng=en&stacked=false&metrics=quality%2Cdeep_sleep)

# Metrics
Expand Down
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default tseslint.config(
'@typescript-eslint/semi': 'off',
'object-curly-spacing': ['error', 'always'],
'quotes': ['error', 'single'],
'quote-props': ['error', 'as-needed'],
'import/extensions': ['error', 'never', { 'json': 'always' }],
'react-refresh/only-export-components': [
'warn',
Expand Down
1 change: 1 addition & 0 deletions src/context/SleepContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const SleepContext = createContext<SleepContextBag>({
latestSession: new Date()
},
sleepStageData: {},
sleepSoundData: {},
isSleepDataLoading: true,
rangeStart: new Date(),
setRangeStart: (start: Date) => {
Expand Down
5 changes: 3 additions & 2 deletions src/context/SleepContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useRawSleepData } from 'data/useRawSleepData'

export const SleepContextProvider = ({ children }: PropsWithChildren) => {
const { i18n } = useTranslation()
const { sleepData, sessionStages, loading } = useRawSleepData()
const { sleepData, sessionStages, sessionSounds, loading } = useRawSleepData()

const {
currentMetric,
Expand Down Expand Up @@ -49,6 +49,7 @@ export const SleepContextProvider = ({ children }: PropsWithChildren) => {
const value = useMemo<SleepContextBag>(() => ({
sleepData,
sleepStageData: sessionStages,
sleepSoundData: sessionSounds,
isSleepDataLoading: loading,
rangeStart: rangeStart ?? new Date(),
setRangeStart,
Expand All @@ -63,7 +64,7 @@ export const SleepContextProvider = ({ children }: PropsWithChildren) => {
setStackedView,
stackedMetrics: stackedMetrics ?? [],
setStackedMetrics: handleSetStackedMetrics
}), [currentMetric, handleSetStackedMetrics, improvementDate, loading, rangeEnd, rangeStart, sessionStages, setCurrentMetric, setRangeEnd, setRangeStart, setStackedView, sleepData, sleepGraphData2d, stackedMetrics, stackedView])
}), [currentMetric, handleSetStackedMetrics, improvementDate, loading, rangeEnd, rangeStart, sessionSounds, sessionStages, setCurrentMetric, setRangeEnd, setRangeStart, setStackedView, sleepData, sleepGraphData2d, stackedMetrics, stackedView])

return (
<SleepContext.Provider value={value}>
Expand Down
8 changes: 7 additions & 1 deletion src/context/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PillowSleepData } from 'data/useSleepData'
import { SleepMetric } from 'modules/controls/MetricConfiguration'
import { SleepGraph2DDataResponse } from 'modules/graph/hooks/useSleepGraph2DData'
import { Dispatch, SetStateAction } from 'react'
import { RawSleepSessionStages } from 'data/useRawSleepData/types'
import { RawSleepSessionSounds, RawSleepSessionStages } from 'data/useRawSleepData/types'


export interface SleepContextBag {
Expand All @@ -20,6 +20,12 @@ export interface SleepContextBag {
*/
sleepStageData: RawSleepSessionStages

/**
* A map of sleep sound data for a given sleep
* session ID.
*/
sleepSoundData: RawSleepSessionSounds

/**
* Whether the read IO or conversion process of
* the data is in progress.
Expand Down
16 changes: 8 additions & 8 deletions src/data/useLinearRegression/useLinearRegression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ describe('Linear Regression Hook', () => {

expect(result.current.regressionLineDeltaHorizontal).toStrictEqual([
{
'xDate': 1724454000000,
'y': 59.99999932863284
xDate: 1724454000000,
y: 59.99999932863284
},
{
'xDate': 1724540400000,
'y': 59.99999932863284
xDate: 1724540400000,
y: 59.99999932863284
}
])
})
Expand All @@ -107,12 +107,12 @@ describe('Linear Regression Hook', () => {

expect(result.current.regressionLineDeltaVertical).toStrictEqual([
{
'xDate': 1724540400000,
'y': 59.99999932863284
xDate: 1724540400000,
y: 59.99999932863284
},
{
'xDate': 1724540400000,
'y': 19.99999932863284
xDate: 1724540400000,
y: 19.99999932863284
}
])
})
Expand Down
134 changes: 67 additions & 67 deletions src/data/useSleepData/useSleepData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,88 +40,88 @@ describe('Sleep Data (CSV) Parsing Hook', () => {
loading: false,
error: null,
sleepData: {
'earliestSession': new Date('2018-08-16T22:16:57.000Z'),
'latestSession': new Date('2018-08-21T06:00:21.000Z'),
earliestSession: new Date('2018-08-16T22:16:57.000Z'),
latestSession: new Date('2018-08-21T06:00:21.000Z'),
sessions: [
{
'audioRecordings': 65,
'duration': {
'awake': 90,
'deep': 130,
'light': 170,
'rem': 50,
'total': 440,
audioRecordings: 65,
duration: {
awake: 90,
deep: 130,
light: 170,
rem: 50,
total: 440,
},
'endTime': new Date('2018-08-17T05:36:42.000Z'),
'id': 'session-0',
'isNap': false,
'mood': SleepMood.OK,
'sleepQuality': 56,
'startTime': new Date('2018-08-16T22:16:57.000Z'),
endTime: new Date('2018-08-17T05:36:42.000Z'),
id: 'session-0',
isNap: false,
mood: SleepMood.OK,
sleepQuality: 56,
startTime: new Date('2018-08-16T22:16:57.000Z'),
},
{
'audioRecordings': 69,
'duration': {
'awake': 420,
'deep': 330,
'light': 190,
'rem': 90,
'total': 513,
audioRecordings: 69,
duration: {
awake: 420,
deep: 330,
light: 190,
rem: 90,
total: 513,
},
'endTime': new Date('2018-08-18T06:31:27.000Z'),
'id': 'session-1',
'isNap': false,
'mood': SleepMood.OK,
'sleepQuality': 50,
'startTime': new Date('2018-08-17T21:57:59.000Z'),
endTime: new Date('2018-08-18T06:31:27.000Z'),
id: 'session-1',
isNap: false,
mood: SleepMood.OK,
sleepQuality: 50,
startTime: new Date('2018-08-17T21:57:59.000Z'),
},
{
'audioRecordings': 12,
'duration': {
'awake': 67,
'deep': 190,
'light': 190,
'rem': 70,
'total': 517,
audioRecordings: 12,
duration: {
awake: 67,
deep: 190,
light: 190,
rem: 70,
total: 517,
},
'endTime': new Date('2018-08-19T07:30:35.000Z'),
'id': 'session-2',
'isNap': false,
'mood': SleepMood.GOOD,
'sleepQuality': 72,
'startTime': new Date('2018-08-18T22:53:27.000Z'),
endTime: new Date('2018-08-19T07:30:35.000Z'),
id: 'session-2',
isNap: false,
mood: SleepMood.GOOD,
sleepQuality: 72,
startTime: new Date('2018-08-18T22:53:27.000Z'),
},
{
'audioRecordings': 5,
'duration': {
'awake': 102,
'deep': 150,
'light': 160,
'rem': 30,
'total': 442,
audioRecordings: 5,
duration: {
awake: 102,
deep: 150,
light: 160,
rem: 30,
total: 442,
},
'endTime': new Date('2018-08-20T06:00:39.000Z'),
'id': 'session-3',
'isNap': false,
'mood': SleepMood.UNKNOWN,
'sleepQuality': 56,
'startTime': new Date('2018-08-19T22:38:42.000Z'),
endTime: new Date('2018-08-20T06:00:39.000Z'),
id: 'session-3',
isNap: false,
mood: SleepMood.UNKNOWN,
sleepQuality: 56,
startTime: new Date('2018-08-19T22:38:42.000Z'),
},
{
'audioRecordings': 9,
'duration': {
'awake': 45,
'deep': 150,
'light': 240,
'rem': 70,
'total': 505,
audioRecordings: 9,
duration: {
awake: 45,
deep: 150,
light: 240,
rem: 70,
total: 505,
},
'endTime': new Date('2018-08-21T06:00:21.000Z'),
'id': 'session-4',
'isNap': false,
'mood': SleepMood.GOOD,
'sleepQuality': 76,
'startTime': new Date('2018-08-20T21:35:34.000Z'),
endTime: new Date('2018-08-21T06:00:21.000Z'),
id: 'session-4',
isNap: false,
mood: SleepMood.GOOD,
sleepQuality: 76,
startTime: new Date('2018-08-20T21:35:34.000Z'),
}
]
}
Expand Down
16 changes: 16 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* A utility function to return the current
* Vite mode. Mostly introduced to be able
* to mock the mode in Vitest tests.
*/
export const env = () => {
return import.meta.env.MODE
}

/**
* Checks if the current Vite mode is production.
* @returns true if the current mode is production, else false.
*/
export const isProduction = (): boolean => {
return env() === 'production'
}
1 change: 1 addition & 0 deletions src/hooks/useQueryParams/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface SleepQueryParams {
end?: Date
lng?: string
stacked?: boolean
selected?: number
}

export interface QueryParamsResponse {
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useQueryParams/useQueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const useQueryParams = (): QueryParamsResponse => {
end: searchParams.has('end') ? new Date(Number(searchParams.get('end'))) : undefined,
lng: searchParams.get('lng') ?? 'en',
stacked: searchParams.has('stacked') ? searchParams.get('stacked') === 'true' : undefined,
metrics: searchParams.has('metrics') ? searchParams.get('metrics')?.split(',') as SleepMetric[] : undefined
metrics: searchParams.has('metrics') ? searchParams.get('metrics')?.split(',') as SleepMetric[] : undefined,
selected: searchParams.get('selected') ? Number(searchParams.get('selected')) : undefined,
}
}, [searchParams])

Expand Down
3 changes: 0 additions & 3 deletions src/modules/graph/SleepSessionsGraph3D.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ interface LinkConfig {
export const SleepSessionsGraph3D = () => {
const graphRef = useRef<ForceGraphMethods>()
const { sleepData, loading } = useSleepData()
console.log(sleepData)

useEffect(() => {
if (graphRef.current) {
Expand Down Expand Up @@ -94,11 +93,9 @@ export const SleepSessionsGraph3D = () => {
links
}
}, [sleepData?.sessions])
console.log('Graph Data', graphData)

// @ts-expect-error to fix later if I come back
const linkColor = useCallback(link => {
console.log(link.source)
switch(link.source.group) {
case 'sleepQuality': {
return '#9a30fe'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const DurationBreakdownPie = ({ data }: DurationBreakdownPieProps) => {
outerRadius={80}
stroke='#0e0e0e'
labelLine={false}
animationBegin={0}
animationDuration={500}
label={renderCustomizedLabel}
>
{pieData.map(({ metric }, index) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.info {
width: 15%;
display: flex;
flex-direction: column;
padding: 15px;
}
}
Loading

0 comments on commit c6a41eb

Please sign in to comment.