const startDate = new Date(`${dv.current()["Start Date"]}`);
const endDate = new Date(`${dv.current()["End Date"]}`);
const MIN_SESSION_HOURS = 0.5; // 30 minutes
// Fetch pages with the 'clienthours' tag and #supervision
let pages = dv.pages('"100 CPD Record" and #supervision')
.where(p => {
const fileDate = new Date(p.date);
const hrs = Number(p.hours) || 0;
return fileDate >= startDate && fileDate <= endDate && hrs >= MIN_SESSION_HOURS;
})
.sort(p => p.date, 'asc');
dv.paragraph(`Total records fetched: ${pages.length}`);
if (pages.length === 0) {
dv.paragraph("No pages found for the specified query.");
} else {
// Group by medium
const mediumGroups = {};
let grandTotalHours = 0;
pages.forEach(p => {
const medium = p.medium || "No medium";
const hours = p.hours || 0;
if (!mediumGroups[medium]) {
mediumGroups[medium] = 0;
}
mediumGroups[medium] += hours;
grandTotalHours += hours;
});
// Prepare the table
const tableData = [];
for (const [medium, hours] of Object.entries(mediumGroups).sort()) {
const percentOfTotal = ((hours / grandTotalHours) * 100).toFixed(2);
tableData.push([
`**${medium}**`,
`**${hours}**`,
`**${percentOfTotal}%**`,
]);
}
// Grand total row
tableData.push([
"**Grand Total**",
`**${grandTotalHours}**`,
"**100%**",
]);
dv.table(["Medium", "Total Hours", "% of Total"], tableData);
}