Skip to content

Commit

Permalink
feat: add default for wake up and go to sleep hour
Browse files Browse the repository at this point in the history
  • Loading branch information
shalev007 committed Aug 19, 2023
1 parent a2a9de3 commit 92b0586
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
28 changes: 14 additions & 14 deletions src/Entities/GoogleFitDay.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { fitness_v1 } from "googleapis";
import {
customDateFormat,
millisecondsToHours,
nanosecondsToMilliseconds,
} from "../util";
import { millisecondsToHours, nanosecondsToMilliseconds } from "../util";
import { DataSourceId, SleepStage } from "../GoogleFit/enums";
import { formatInTimeZone } from "date-fns-tz";

export const UNKNOWN_DATE = "UNKNOWN_DATE";
export class GoogleFitDay {
from!: Date;
to!: Date;
wentToSleepAt!: Date;
wokeUpAt!: Date;
wentToSleepAt: Date | typeof UNKNOWN_DATE = UNKNOWN_DATE;
wokeUpAt: Date | typeof UNKNOWN_DATE = UNKNOWN_DATE;
stepCount: number = 0;
averageHeartRate: number = 0;
maxHeartRate: number = 0;
Expand Down Expand Up @@ -91,13 +87,17 @@ export class GoogleFitDay {
(ds) => ds.dataSourceId === DataSourceId.SleepSegment
)?.point ?? [];

const wentToSleepAt = parseInt(dataPoint?.[0].startTimeNanos ?? "");
const wokeUpAt = parseInt(
dataPoint?.[dataPoint.length - 1]?.endTimeNanos ?? ""
);
// TODO: need to validate sleep hours
if (dataPoint.length) {
const wentToSleepAt = parseInt(dataPoint?.[0].startTimeNanos ?? "");
const wokeUpAt = parseInt(
dataPoint?.[dataPoint.length - 1]?.endTimeNanos ?? ""
);

this.wentToSleepAt = new Date(nanosecondsToMilliseconds(wentToSleepAt));
this.wokeUpAt = new Date(nanosecondsToMilliseconds(wokeUpAt));
}

this.wentToSleepAt = new Date(nanosecondsToMilliseconds(wentToSleepAt));
this.wokeUpAt = new Date(nanosecondsToMilliseconds(wokeUpAt));
this.sleepHours = millisecondsToHours(totalSleepTime);
this.sleepAwakeHours = millisecondsToHours(totalAwakeTime);
this.sleepDeepHours = millisecondsToHours(totalDeepSleepTime);
Expand Down
14 changes: 9 additions & 5 deletions src/GoogleSheets/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GoogleFitDay } from "../Entities/GoogleFitDay";
import { customDateFormat } from "../util";
import { GoogleFitDay, UNKNOWN_DATE } from "../Entities/GoogleFitDay";
import { formatDateWithTimezome } from "../util";

export const SHEET_HEADERS = [
"Date",
Expand All @@ -17,7 +17,7 @@ export const SHEET_HEADERS = [

export function googleFitDayToSheetRow(googleFitDay: GoogleFitDay) {
return [
customDateFormat(googleFitDay.from),
formatDateWithTimezome(googleFitDay.from),
googleFitDay.stepCount,
googleFitDay.averageHeartRate,
googleFitDay.maxHeartRate,
Expand All @@ -28,7 +28,11 @@ export function googleFitDayToSheetRow(googleFitDay: GoogleFitDay) {
googleFitDay.sleepAwakeHours,
googleFitDay.sleepDeepHours,
googleFitDay.sleepRemHours,
customDateFormat(googleFitDay.wentToSleepAt),
customDateFormat(googleFitDay.wokeUpAt),
googleFitDay.wentToSleepAt !== UNKNOWN_DATE
? formatDateWithTimezome(googleFitDay.wentToSleepAt, "HH:mm:ss")
: UNKNOWN_DATE,
googleFitDay.wokeUpAt !== UNKNOWN_DATE
? formatDateWithTimezome(googleFitDay.wokeUpAt, "HH:mm:ss")
: UNKNOWN_DATE,
];
}
7 changes: 5 additions & 2 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export function millisecondsToHours(milliseconds: number) {
return milliseconds / 1000 / 60 / 60;
}

export function customDateFormat(date: Date) {
return formatInTimeZone(date, "Asia/Jerusalem", "yyyy-MM-dd HH:mm:ss");
export function formatDateWithTimezome(
date: Date,
format: string = "yyyy-MM-dd HH:mm:ss"
) {
return formatInTimeZone(date, "Asia/Jerusalem", format);
}

0 comments on commit 92b0586

Please sign in to comment.