Skip to content

Commit fbc3685

Browse files
♻️ Create nested Oura API
1 parent 1bbda92 commit fbc3685

File tree

1 file changed

+63
-50
lines changed

1 file changed

+63
-50
lines changed

src/api/oura-ring.ts

+63-50
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ dayjs.extend(isoWeeksInYear);
1414
dayjs.extend(isLeapYear);
1515
cosmicSync("stethoscope");
1616

17+
interface CategoryData {
18+
[index: string]: number;
19+
}
20+
1721
const updateOuraDailyData = async (date: Date) => {
1822
const formattedDate = dayjs(date).format("YYYY-MM-DD");
1923
if (integrationConfig("oura-ring", "weight")) {
@@ -147,92 +151,101 @@ export default class OuraRing implements Integration {
147151
console.log("Done!");
148152
}
149153
async summary() {
150-
for await (const key of [
151-
"steps",
152-
"total",
153-
"cal_active",
154-
"cal_total",
155-
"rem",
156-
"awake",
157-
"deep",
158-
"duration",
159-
"efficiency",
160-
"light",
161-
]) {
162-
for await (const category of ["oura-readiness", "oura-activity", "oura-weight", "oura-sleep"]) {
163-
// Find all items that have daily
164-
if (
165-
(await pathExists(join(".", "data", category, "daily"))) &&
166-
(await lstat(join(".", "data", category, "daily"))).isDirectory()
167-
) {
154+
for await (const category of ["oura-readiness", "oura-activity", "oura-weight", "oura-sleep"]) {
155+
if (
156+
(await pathExists(join(".", "data", category, "daily"))) &&
157+
(await lstat(join(".", "data", category, "daily"))).isDirectory()
158+
) {
159+
for await (const file of ["top-categories.json", "top-overview.json"]) {
168160
const years = (await readdir(join(".", "data", category, "daily"))).filter((i) => /^\d+$/.test(i));
169-
const yearData: { [index: string]: number } = {};
161+
const yearData: { [index: string]: CategoryData } = {};
170162
const weeklyData: {
171-
[index: string]: {
172-
[index: string]: { [index: string]: number };
173-
};
163+
[index: string]: { [index: string]: { [index: string]: CategoryData } };
174164
} = {};
175165
for await (const year of years) {
176-
let yearlySum = 0;
177-
const monthlyData: { [index: string]: number } = {};
178-
[...Array(13).keys()].slice(1).forEach((val) => (monthlyData[val.toString()] = 0));
166+
let yearlySum: CategoryData = {};
167+
const monthlyData: { [index: string]: CategoryData } = {};
168+
[...Array(13).keys()].slice(1).forEach((val) => (monthlyData[val.toString()] = {}));
179169
const months = (await readdir(join(".", "data", category, "daily", year))).filter((i) => /^\d+$/.test(i));
180170
for await (const month of months) {
181-
let monthlySum = 0;
182-
const dailyData: { [index: string]: number } = {};
171+
let monthlySum: CategoryData = {};
172+
const dailyData: { [index: string]: CategoryData } = {};
183173
[...Array(dayjs(`${year}-${month}-10`).daysInMonth()).keys()]
184174
.slice(1)
185-
.forEach((val) => (dailyData[val.toString()] = 0));
175+
.forEach((val) => (dailyData[val.toString()] = {}));
186176
const days = (await readdir(join(".", "data", category, "daily", year, month))).filter((i) =>
187177
/^\d+$/.test(i)
188178
);
189179
for await (const day of days) {
190-
let json = await readJson(join(".", "data", category, "daily", year, month, day, "sessions.json"));
191-
let dailySum = 0;
192-
if (Array.isArray(json)) {
180+
let json: any[] = [];
181+
try {
182+
json = await readJson(join(".", "data", category, "daily", year, month, day, file));
183+
} catch (error) {}
184+
let dailySum: CategoryData = {};
185+
if (Array.isArray(json) && json.length) {
193186
json.forEach((record: any) => {
194-
if (key in record) {
195-
dailySum += record[key];
196-
}
187+
[
188+
"steps",
189+
"total",
190+
"cal_active",
191+
"cal_total",
192+
"rem",
193+
"awake",
194+
"deep",
195+
"duration",
196+
"efficiency",
197+
"light",
198+
].forEach((dataType) => {
199+
if (typeof record[dataType] === "number") {
200+
dailySum[dataType] = dailySum[dataType] || 0;
201+
dailySum[dataType] += record[dataType];
202+
}
203+
});
197204
});
198205
}
199-
if (dailySum) dailyData[parseInt(day)] = dailySum;
200-
monthlySum += dailySum;
201-
yearlySum += dailySum;
202-
Object.keys(dailyData).forEach((key) => {
203-
const weekNumber = dayjs(`${year}-${month}-${key}`).week();
204-
weeklyData[year] = weeklyData[year] || {};
205-
weeklyData[year][weekNumber] = weeklyData[year][weekNumber] || {};
206-
weeklyData[year][weekNumber][`${year}-${month}-${key}`] = dailyData[key];
206+
if (Object.keys(dailySum).length) dailyData[parseInt(day)] = dailySum;
207+
Object.keys(dailySum).forEach((key) => {
208+
monthlySum[key] = monthlySum[key] || 0;
209+
monthlySum[key] += dailySum[key];
210+
yearlySum[key] = yearlySum[key] || 0;
211+
yearlySum[key] += dailySum[key];
207212
});
208213
}
209-
if (Object.keys(dailyData).length && Object.values(dailyData).reduce((a, b) => a + b, 0))
214+
215+
Object.keys(dailyData).forEach((key) => {
216+
const weekNumber = dayjs(`${year}-${month}-${key}`).week();
217+
weeklyData[year] = weeklyData[year] || {};
218+
weeklyData[year][weekNumber] = weeklyData[year][weekNumber] || {};
219+
weeklyData[year][weekNumber][`${year}-${month}-${key}`] = dailyData[key];
220+
});
221+
222+
if (Object.keys(dailyData).length)
210223
await write(
211-
join(".", "data", category, "summary", key.replace(/_/g, "-"), "days", year, `${month}.json`),
224+
join(".", "data", category, "summary", file.replace(".json", ""), "days", year, `${month}.json`),
212225
JSON.stringify(dailyData, null, 2)
213226
);
214227
if (monthlySum) monthlyData[parseInt(month)] = monthlySum;
215228
}
216-
if (Object.keys(monthlyData).length && Object.values(monthlyData).reduce((a, b) => a + b, 0))
229+
if (Object.keys(monthlyData).length)
217230
await write(
218-
join(".", "data", category, "summary", key.replace(/_/g, "-"), "months", `${year}.json`),
231+
join(".", "data", category, "summary", file.replace(".json", ""), "months", `${year}.json`),
219232
JSON.stringify(monthlyData, null, 2)
220233
);
221234
if (yearlySum) yearData[parseInt(year)] = yearlySum;
222235
}
223-
if (Object.keys(yearData).length && Object.values(yearData).reduce((a, b) => a + b, 0))
236+
if (Object.keys(yearData).length)
224237
await write(
225-
join(".", "data", category, "summary", key.replace(/_/g, "-"), "years.json"),
238+
join(".", "data", category, "summary", file.replace(".json", ""), "years.json"),
226239
JSON.stringify(yearData, null, 2)
227240
);
228241
for await (const year of Object.keys(weeklyData)) {
229242
for await (const week of Object.keys(weeklyData[year])) {
230243
if (
231244
Object.keys(weeklyData[year][week]).length &&
232-
Object.values(weeklyData[year][week]).reduce((a, b) => a + b, 0)
245+
Object.values(weeklyData[year][week]).reduce((a, b) => a + Object.keys(b).length, 0)
233246
)
234247
await write(
235-
join(".", "data", category, "summary", key.replace(/_/g, "-"), "weeks", year, `${week}.json`),
248+
join(".", "data", category, "summary", file.replace(".json", ""), "weeks", year, `${week}.json`),
236249
JSON.stringify(weeklyData[year][week], null, 2)
237250
);
238251
}

0 commit comments

Comments
 (0)