Advertisement
dhniceday

Untitled

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