-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-data.js
More file actions
49 lines (42 loc) · 1.44 KB
/
update-data.js
File metadata and controls
49 lines (42 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const fs = require('fs');
const csv = require('csv-parser');
const dietLogPath = require('path').resolve(process.env.HOME, 'workspaces/bri/DIET_LOG.csv');
const outputPath = './docs/data.json';
const today = new Date().toISOString().slice(0, 10);
let totalCalories = 0;
const targetCalories = 1880; // Assuming a target of 1880 calories
fs.createReadStream(dietLogPath)
.pipe(csv())
.on('data', (row) => {
if (row.Date === today) {
totalCalories += parseInt(row.Calories, 10);
}
})
.on('end', () => {
const remaining = targetCalories - totalCalories;
const percent = Math.min(100, Math.round((totalCalories / targetCalories) * 100));
// Create share string
const blockCount = Math.round((percent / 100) * 10);
let blocks = '';
for (let i = 0; i < 10; i++) {
blocks += (i < blockCount) ? '🟩' : '⬜';
}
const shareString = `*FUEL MANIFEST: ${today}*\n\nCURRENT: ${totalCalories} / ${targetCalories} KCAL\nREMAINING: ${remaining}\n\n[ ${blocks} ]\n`;
const data = {
meta: {
date: today
},
stats: {
total: totalCalories,
target: targetCalories,
remaining: remaining,
percent: percent
},
shareString: shareString
};
if (!fs.existsSync('./docs')) {
fs.mkdirSync('./docs');
}
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2));
console.log(`Data updated successfully at ${outputPath}`);
});