feat: 添加原始睡眠数据列表,优化睡眠详情数据处理逻辑,确保完整的睡眠周期计算
This commit is contained in:
@@ -320,37 +320,27 @@ export async function fetchSleepDetailForDate(date: Date): Promise<SleepDetailDa
|
||||
}
|
||||
|
||||
// 找到入睡时间和起床时间
|
||||
// 过滤出实际睡眠阶段(排除在床时间和清醒时间)
|
||||
const actualSleepSamples = sleepSamples.filter(sample =>
|
||||
sample.value !== SleepStage.InBed && sample.value !== SleepStage.Awake
|
||||
);
|
||||
|
||||
// 入睡时间:第一个实际睡眠阶段的开始时间
|
||||
// 起床时间:最后一个实际睡眠阶段的结束时间
|
||||
// 使用所有样本数据来确定完整的睡眠周期(包含清醒时间)
|
||||
let bedtime: string;
|
||||
let wakeupTime: string;
|
||||
|
||||
if (actualSleepSamples.length > 0) {
|
||||
if (sleepSamples.length > 0) {
|
||||
// 按开始时间排序
|
||||
const sortedSleepSamples = actualSleepSamples.sort((a, b) =>
|
||||
const sortedSamples = sleepSamples.sort((a, b) =>
|
||||
new Date(a.startDate).getTime() - new Date(b.startDate).getTime()
|
||||
);
|
||||
|
||||
bedtime = sortedSleepSamples[0].startDate;
|
||||
wakeupTime = sortedSleepSamples[sortedSleepSamples.length - 1].endDate;
|
||||
// 入睡时间:第一个样本的开始时间(包含清醒时间,确保完整性)
|
||||
bedtime = sortedSamples[0].startDate;
|
||||
// 起床时间:最后一个样本的结束时间
|
||||
wakeupTime = sortedSamples[sortedSamples.length - 1].endDate;
|
||||
|
||||
console.log('计算入睡和起床时间:');
|
||||
console.log('- 入睡时间:', dayjs(bedtime).format('YYYY-MM-DD HH:mm:ss'));
|
||||
console.log('- 起床时间:', dayjs(wakeupTime).format('YYYY-MM-DD HH:mm:ss'));
|
||||
} else {
|
||||
// 如果没有实际睡眠数据,回退到使用所有样本数据
|
||||
console.warn('没有找到实际睡眠阶段数据,使用所有样本数据');
|
||||
const sortedAllSamples = sleepSamples.sort((a, b) =>
|
||||
new Date(a.startDate).getTime() - new Date(b.startDate).getTime()
|
||||
);
|
||||
|
||||
bedtime = sortedAllSamples[0].startDate;
|
||||
wakeupTime = sortedAllSamples[sortedAllSamples.length - 1].endDate;
|
||||
console.warn('没有找到睡眠样本数据');
|
||||
return null;
|
||||
}
|
||||
|
||||
// 计算在床时间 - 使用 INBED 样本数据
|
||||
@@ -380,7 +370,6 @@ export async function fetchSleepDetailForDate(date: Date): Promise<SleepDetailDa
|
||||
// 计算睡眠阶段统计
|
||||
const sleepStages = calculateSleepStageStats(sleepSamples);
|
||||
|
||||
// 计算总睡眠时间
|
||||
const totalSleepTime = sleepStages
|
||||
.reduce((total, stage) => total + stage.duration, 0);
|
||||
|
||||
@@ -401,6 +390,19 @@ export async function fetchSleepDetailForDate(date: Date): Promise<SleepDetailDa
|
||||
// 获取质量描述和建议
|
||||
const qualityInfo = getSleepQualityInfo(sleepScore);
|
||||
|
||||
// 详细的调试信息
|
||||
console.log('=== 睡眠数据处理结果 ===');
|
||||
console.log('时间范围:', dayjs(bedtime).format('HH:mm'), '-', dayjs(wakeupTime).format('HH:mm'));
|
||||
console.log('在床时间:', timeInBed, '分钟');
|
||||
console.log('总睡眠时间:', totalSleepTime, '分钟');
|
||||
console.log('睡眠效率:', sleepEfficiency, '%');
|
||||
console.log('睡眠阶段统计:');
|
||||
sleepStages.forEach(stage => {
|
||||
console.log(` ${getSleepStageDisplayName(stage.stage)}: ${stage.duration}分钟 (${stage.percentage}%)`);
|
||||
});
|
||||
|
||||
console.log('========================');
|
||||
|
||||
const sleepDetailData: SleepDetailData = {
|
||||
sleepScore,
|
||||
totalSleepTime,
|
||||
@@ -417,7 +419,7 @@ export async function fetchSleepDetailForDate(date: Date): Promise<SleepDetailDa
|
||||
recommendation: qualityInfo.recommendation
|
||||
};
|
||||
|
||||
console.log('睡眠详情数据获取完成:', sleepDetailData);
|
||||
console.log('睡眠详情数据获取完成,睡眠得分:', sleepScore);
|
||||
return sleepDetailData;
|
||||
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user