feat: 添加睡眠阶段时间轴组件,优化睡眠数据可视化
This commit is contained in:
@@ -177,7 +177,7 @@ export const fetchSleepSamples = async (date: Date): Promise<SleepSample[]> => {
|
||||
value: sample.value,
|
||||
sourceName: sample.sourceName
|
||||
});
|
||||
|
||||
|
||||
return {
|
||||
startDate: sample.startDate,
|
||||
endDate: sample.endDate,
|
||||
@@ -251,7 +251,7 @@ export const fetchSleepHeartRateData = async (bedtime: string, wakeupTime: strin
|
||||
*/
|
||||
export const calculateSleepStageStats = (samples: SleepSample[]): SleepStageStats[] => {
|
||||
console.log('[Sleep] 开始计算睡眠阶段统计,原始样本数:', samples.length);
|
||||
|
||||
|
||||
const stageMap = new Map<SleepStage, number>();
|
||||
|
||||
// 统计各阶段持续时间
|
||||
@@ -327,7 +327,7 @@ export const calculateSleepStageStats = (samples: SleepSample[]): SleepStageStat
|
||||
|
||||
const sortedStats = stats.sort((a, b) => b.duration - a.duration);
|
||||
console.log('[Sleep] 最终睡眠阶段统计:', sortedStats);
|
||||
|
||||
|
||||
return sortedStats;
|
||||
};
|
||||
|
||||
@@ -441,16 +441,16 @@ export const fetchCompleteSleepData = async (date: Date): Promise<CompleteSleepD
|
||||
|
||||
// 计算睡眠阶段统计
|
||||
const sleepStages = calculateSleepStageStats(sleepSamples);
|
||||
|
||||
|
||||
// 计算总睡眠时间(排除在床时间和醒来时间)
|
||||
const actualSleepStages = sleepStages.filter(stage =>
|
||||
stage.stage !== SleepStage.InBed && stage.stage !== SleepStage.Awake
|
||||
const actualSleepStages = sleepStages.filter(stage =>
|
||||
stage.stage !== SleepStage.InBed
|
||||
);
|
||||
const totalSleepTime = actualSleepStages.reduce((total, stage) => total + stage.duration, 0);
|
||||
|
||||
|
||||
// 重新计算睡眠效率
|
||||
const sleepEfficiency = timeInBed > 0 ? Math.round((totalSleepTime / timeInBed) * 100) : 0;
|
||||
|
||||
|
||||
console.log('[Sleep] 睡眠效率计算:');
|
||||
console.log('- 总睡眠时间(不含醒来):', totalSleepTime, '分钟');
|
||||
console.log('- 在床时间:', timeInBed, '分钟');
|
||||
|
||||
Reference in New Issue
Block a user