feat(initialize): 优化权限和健康监听服务的初始化流程,增加延迟和错误处理
fix(version): 更新应用版本号至1.0.26 feat(sleep): 增加睡眠阶段统计的准确性,优化日志输出
This commit is contained in:
@@ -11,6 +11,14 @@ export enum SleepStage {
|
||||
REM = 'REM'
|
||||
}
|
||||
|
||||
// 阶段中被视为真正睡着的类型
|
||||
const ACTIVE_SLEEP_STAGE_SET = new Set<SleepStage>([
|
||||
SleepStage.Asleep,
|
||||
SleepStage.Core,
|
||||
SleepStage.Deep,
|
||||
SleepStage.REM
|
||||
]);
|
||||
|
||||
// 睡眠质量评级
|
||||
export enum SleepQuality {
|
||||
Poor = 'poor',
|
||||
@@ -219,12 +227,13 @@ export const calculateSleepStageStats = (samples: SleepSample[]): SleepStageStat
|
||||
samples.forEach(sample => {
|
||||
const startTime = dayjs(sample.startDate);
|
||||
const endTime = dayjs(sample.endDate);
|
||||
const duration = endTime.diff(startTime, 'minute');
|
||||
const durationMinutes = endTime.diff(startTime, 'minute', true);
|
||||
const roundedDuration = Math.round(durationMinutes);
|
||||
|
||||
console.log(`[Sleep] 阶段: ${sample.value}, 持续时间: ${duration}分钟`);
|
||||
console.log(`[Sleep] 阶段: ${sample.value}, 持续时间: ${roundedDuration}分钟`);
|
||||
|
||||
const currentDuration = stageMap.get(sample.value) || 0;
|
||||
stageMap.set(sample.value, currentDuration + duration);
|
||||
stageMap.set(sample.value, currentDuration + durationMinutes);
|
||||
});
|
||||
|
||||
console.log('[Sleep] 阶段时间统计:', Array.from(stageMap.entries()));
|
||||
@@ -234,7 +243,7 @@ export const calculateSleepStageStats = (samples: SleepSample[]): SleepStageStat
|
||||
.filter(([stage]) => stage !== SleepStage.InBed)
|
||||
.reduce((total, [, duration]) => total + duration, 0);
|
||||
|
||||
console.log('[Sleep] 实际睡眠时间(包含醒来):', actualSleepTime, '分钟');
|
||||
console.log('[Sleep] 实际睡眠时间(包含醒来):', Math.round(actualSleepTime), '分钟');
|
||||
|
||||
const stats: SleepStageStats[] = [];
|
||||
|
||||
@@ -243,6 +252,7 @@ export const calculateSleepStageStats = (samples: SleepSample[]): SleepStageStat
|
||||
if (stage === SleepStage.InBed) return;
|
||||
|
||||
const percentage = actualSleepTime > 0 ? (duration / actualSleepTime) * 100 : 0;
|
||||
const roundedDuration = Math.round(duration);
|
||||
let quality: SleepQuality;
|
||||
|
||||
// 根据不同阶段的标准百分比评估质量
|
||||
@@ -278,12 +288,14 @@ export const calculateSleepStageStats = (samples: SleepSample[]): SleepStageStat
|
||||
|
||||
stats.push({
|
||||
stage,
|
||||
duration,
|
||||
duration: roundedDuration,
|
||||
percentage: Math.round(percentage),
|
||||
quality
|
||||
});
|
||||
|
||||
console.log(`[Sleep] 阶段统计: ${stage}, 时长: ${duration}分钟, 百分比: ${Math.round(percentage)}%, 质量: ${quality}`);
|
||||
console.log(
|
||||
`[Sleep] 阶段统计: ${stage}, 时长: ${roundedDuration}分钟, 百分比: ${Math.round(percentage)}%, 质量: ${quality}`
|
||||
);
|
||||
});
|
||||
|
||||
const sortedStats = stats.sort((a, b) => b.duration - a.duration);
|
||||
@@ -410,10 +422,15 @@ export const fetchCompleteSleepData = async (date: Date): Promise<CompleteSleepD
|
||||
const sleepStages = calculateSleepStageStats(sleepSamples);
|
||||
|
||||
// 计算总睡眠时间(排除在床时间和醒来时间)
|
||||
const actualSleepStages = sleepStages.filter(stage =>
|
||||
stage.stage !== SleepStage.InBed
|
||||
);
|
||||
const totalSleepTime = actualSleepStages.reduce((total, stage) => total + stage.duration, 0);
|
||||
const totalSleepMinutes = sleepSamples.reduce((total, sample) => {
|
||||
if (!ACTIVE_SLEEP_STAGE_SET.has(sample.value)) {
|
||||
return total;
|
||||
}
|
||||
const start = dayjs(sample.startDate);
|
||||
const end = dayjs(sample.endDate);
|
||||
return total + end.diff(start, 'minute', true);
|
||||
}, 0);
|
||||
const totalSleepTime = Math.round(totalSleepMinutes);
|
||||
|
||||
// 重新计算睡眠效率
|
||||
const sleepEfficiency = timeInBed > 0 ? Math.round((totalSleepTime / timeInBed) * 100) : 0;
|
||||
@@ -501,4 +518,4 @@ export const getSleepStageColor = (stage: SleepStage): string => {
|
||||
default:
|
||||
return '#9CA3AF';
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user