Advertisement
dhniceday

Untitled

Nov 24th, 2022
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.81 KB | Software | 0 0
  1. ``` dataviewjs
  2. /*
  3. Frontmatter structure:
  4. ---
  5. gesundheit:
  6.   schritte: 693
  7.   wasser: 5
  8.   schlafdauer: 401
  9.   schlafquali: 87
  10. ---
  11. */
  12. async function getGoalAverage(nDays) {
  13.     const kpis = ['Wasser', 'Schritte', 'Schlaf Dauer', 'Schlaf Index']
  14.     const goals = [6, 6000, 480, 100];
  15.    
  16.     const qryWeekAverage = await dv.query(`
  17.     TABLE WITHOUT ID
  18.         round(sum(rows.G.wasser) / `+nDays+`, 2) as "Wasser",
  19.         round(sum(rows.G.schritte) / `+nDays+`) as "Schritte",
  20.         round(sum(rows.G.schlafdauer) / `+nDays+`) as "SchlafDauer",
  21.         round(sum(rows.G.schlafquali) / `+nDays+`) as "SchlafIndex"
  22.     FROM "10 - Daily Notes"
  23.     WHERE gesundheit.wasser
  24.     AND gesundheit.wasser
  25.     AND gesundheit.schritte
  26.     AND gesundheit.schlafdauer
  27.     AND gesundheit.schlafquali
  28.     AND date(today) - file.day <= dur(`+nDays+` days)
  29.     FLATTEN gesundheit AS G
  30.     GROUP BY true
  31.     `)
  32.    
  33.     let wAvg = qryWeekAverage.value.values;
  34.     let wAvgTable = [];
  35.     let tmpTable = [];
  36.    
  37.     for (let r of wAvg) {
  38.         for (var i = 0; i < goals.length; i++) {
  39.             const stat = r[i];
  40.             const goal = goals[i];
  41.             const kpiName = kpis[i];
  42.            
  43.             const percentOfGoal = Math.round(stat * 100 / goal);
  44.             let progressBar = '<progress value="' + percentOfGoal + '" max="100"></progress>'
  45.  
  46.             tmpTable.push(kpiName);
  47.             tmpTable.push(stat);
  48.             tmpTable.push(goal);
  49.             tmpTable.push(percentOfGoal);
  50.             tmpTable.push(progressBar);
  51.    
  52.             wAvgTable.push(tmpTable);
  53.             tmpTable = [];
  54.         }
  55.     }
  56.    
  57.     // Output
  58.     var out = ''
  59.  
  60.     dv.header(6, nDays + ' Tage');
  61.  
  62.     out = '|*Kategorie*|*Ziel*|\n';
  63.     out = out + '|---|---|\n';
  64.     for (let r of wAvgTable) {
  65.             let kpi = r[0];
  66.             let actual = r[1];
  67.             let target = r[2];
  68.             let percent = r[3];
  69.             let bar = r[4];
  70.  
  71.             out = out + '|' + kpi + '|' + bar + '|\n';
  72.     }
  73.     dv.span(out);
  74. } // end: function
  75.  
  76. // Execute main function
  77. getGoalAverage(7)
  78. ```
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement