Advertisement
dhniceday

Personal Goals

Nov 30th, 2022 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---
  2. aliases:
  3. tags:
  4. title: Personal Goals
  5. date-created: 2022-11-24
  6. date-modified: 2022-11-25
  7.  
  8. cssClass:
  9. - wide-view
  10. ---
  11. %% [[2022-11-24]] %%
  12. >[!INFO] Ziele
  13. >🥤 *4* | 🦶 *5000* | 😴-🕗 *8* | 😴-📈 *100* 
  14.  
  15. ``` dataviewjs
  16. /*
  17. Frontmatter structure:
  18. ---
  19. gesundheit:
  20.   schritte: 693
  21.   wasser: 5
  22.   schlafdauer: 401
  23.   schlafquali: 87
  24. ---
  25. */
  26. async function getGoalAverage(nDays) {
  27.     const kpis = ['Wasser', 'Schritte', 'Schlaf-Dauer)', 'Schlaf-Index']
  28.     const kpiIcons = ['🥤', '🦶', '😴-🕗', '😴-📈']
  29.     const goals = [4, 5000, 480, 100];
  30.    
  31.     const qryWeekAverage = await dv.query(`
  32.     TABLE WITHOUT ID
  33.         round(sum(rows.G.wasser) / `+nDays+`, 2) as "Wasser",
  34.         round(sum(rows.G.schritte) / `+nDays+`) as "Schritte",
  35.         round(sum(rows.G.schlafdauer) / `+nDays+`) as "SchlafDauer",
  36.         round(sum(rows.G.schlafquali) / `+nDays+`) as "SchlafIndex"
  37.     FROM "10 - Daily Notes"
  38.     WHERE gesundheit.wasser
  39.     AND gesundheit.wasser
  40.     AND gesundheit.schritte
  41.     AND gesundheit.schlafdauer
  42.     AND gesundheit.schlafquali
  43.     AND date(today) - file.day <= dur(`+nDays+` days)
  44.     FLATTEN gesundheit AS G
  45.     GROUP BY true
  46.     `)
  47.    
  48.     let wAvg = qryWeekAverage.value.values;
  49.     let wAvgTable = [];
  50.     let tmpTable = [];
  51.    
  52.     for (let r of wAvg) {
  53.         for (var i = 0; i < goals.length; i++) {
  54.             const stat = r[i];
  55.             const goal = goals[i];
  56.             const kpiName = kpiIcons[i];
  57.            
  58.             const percentOfGoal = Math.round(stat * 100 / goal);
  59.             let progressBar = '<progress value="' + percentOfGoal + '" max="100"></progress>'
  60.  
  61.             tmpTable.push(kpiName);
  62.             tmpTable.push(stat);
  63.             tmpTable.push(goal);
  64.             tmpTable.push(percentOfGoal);
  65.             tmpTable.push(progressBar);
  66.    
  67.             wAvgTable.push(tmpTable);
  68.             tmpTable = [];
  69.         }
  70.     }
  71.    
  72.     // === Output ===
  73.     var out = '';
  74.  
  75.     // Table
  76.  
  77.     out = out + '|*' + nDays + ' Tage*||\n';
  78.     out = out + '|---|---|\n';
  79.     for (let r of wAvgTable) {
  80.             let kpi = r[0];
  81.             let actual = r[1];
  82.             let target = r[2];
  83.             let percent = r[3];
  84.             let bar = r[4];
  85.  
  86.             out = out + '|' + kpi + '|' + bar + '|\n';
  87.     }
  88.    
  89.     dv.paragraph(out);
  90.    
  91. } // end: function
  92.  
  93. // Execute main function
  94. getGoalAverage(7);
  95.  
  96. this.container.style.margin = "auto";
  97.  
  98. ```
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement