Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ``` dataviewjs
- /*
- Frontmatter structure:
- ---
- gesundheit:
- schritte: 693
- wasser: 5
- schlafdauer: 401
- schlafquali: 87
- ---
- */
- async function getGoalAverage(nDays) {
- const kpis = ['Wasser', 'Schritte', 'Schlaf Dauer', 'Schlaf Index']
- const goals = [6, 6000, 480, 100];
- const qryWeekAverage = await dv.query(`
- TABLE WITHOUT ID
- round(sum(rows.G.wasser) / `+nDays+`, 2) as "Wasser",
- round(sum(rows.G.schritte) / `+nDays+`) as "Schritte",
- round(sum(rows.G.schlafdauer) / `+nDays+`) as "SchlafDauer",
- round(sum(rows.G.schlafquali) / `+nDays+`) as "SchlafIndex"
- FROM "10 - Daily Notes"
- WHERE gesundheit.wasser
- AND gesundheit.wasser
- AND gesundheit.schritte
- AND gesundheit.schlafdauer
- AND gesundheit.schlafquali
- AND date(today) - file.day <= dur(`+nDays+` days)
- FLATTEN gesundheit AS G
- GROUP BY true
- `)
- let wAvg = qryWeekAverage.value.values;
- let wAvgTable = [];
- let tmpTable = [];
- for (let r of wAvg) {
- for (var i = 0; i < goals.length; i++) {
- const stat = r[i];
- const goal = goals[i];
- const kpiName = kpis[i];
- const percentOfGoal = Math.round(stat * 100 / goal);
- let progressBar = '<progress value="' + percentOfGoal + '" max="100"></progress>'
- tmpTable.push(kpiName);
- tmpTable.push(stat);
- tmpTable.push(goal);
- tmpTable.push(percentOfGoal);
- tmpTable.push(progressBar);
- wAvgTable.push(tmpTable);
- tmpTable = [];
- }
- }
- // Output
- var out = ''
- dv.header(6, nDays + ' Tage');
- out = '|*Kategorie*|*Ziel*|\n';
- out = out + '|---|---|\n';
- for (let r of wAvgTable) {
- let kpi = r[0];
- let actual = r[1];
- let target = r[2];
- let percent = r[3];
- let bar = r[4];
- out = out + '|' + kpi + '|' + bar + '|\n';
- }
- dv.span(out);
- } // end: function
- // Execute main function
- getGoalAverage(7)
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement