feat(api): explain dashboard chargeability by chapter

This commit is contained in:
2026-03-31 23:34:03 +02:00
parent a8fcc4dacb
commit 703406a76b
7 changed files with 403 additions and 31 deletions
@@ -74,11 +74,16 @@ type DashboardTopValueResourcesInput = z.infer<typeof dashboardTopValueResources
type DashboardDemandInput = z.infer<typeof dashboardDemandInputSchema>;
type DashboardDetailInput = z.infer<typeof dashboardDetailInputSchema>;
type DashboardChargeabilityOverviewInput = z.infer<typeof dashboardChargeabilityOverviewInputSchema>;
type DashboardChargeabilityOverviewRead = Awaited<ReturnType<typeof getDashboardChargeabilityOverview>>;
function round1(value: number): number {
return Math.round(value * 10) / 10;
}
function formatPct(value: number): string {
return `${Math.round(value)}%`;
}
function mapStatisticsDetail(overview: Awaited<ReturnType<typeof getDashboardOverview>>) {
return {
activeResources: overview.activeResources,
@@ -148,6 +153,25 @@ async function getDemandCached(
return result;
}
async function getChargeabilityOverviewCached(
db: Parameters<typeof getDashboardChargeabilityOverview>[0],
input: DashboardChargeabilityOverviewInput,
): Promise<DashboardChargeabilityOverviewRead> {
const cacheKey = `chargeability:${input.includeProposed}:${input.topN}:${input.watchlistThreshold}:${(input.countryIds ?? []).join(",")}:${input.departed ?? ""}`;
const cached = await cacheGet<DashboardChargeabilityOverviewRead>(cacheKey);
if (cached) return cached;
const result = await getDashboardChargeabilityOverview(db, {
includeProposed: input.includeProposed,
topN: input.topN,
watchlistThreshold: input.watchlistThreshold,
...(input.countryIds !== undefined ? { countryIds: input.countryIds } : {}),
...(input.departed !== undefined ? { departed: input.departed } : {}),
});
await cacheSet(cacheKey, result, DEFAULT_TTL);
return result;
}
async function getTopValueResourcesCached(
db: Parameters<typeof getDashboardTopValueResources>[0],
input: { limit: number; userRole: string },
@@ -174,6 +198,101 @@ function getUserRole(ctx: DashboardProcedureContext) {
?? "USER";
}
function mapChargeabilityByChapter(
rows: DashboardChargeabilityOverviewRead["rows"],
month: string,
) {
const chapterMap = new Map<string, {
headcount: number;
avgTargetSum: number;
avgActualSum: number;
avgExpectedSum: number;
derivedHeadcount: number;
baseAvailableHours: number;
effectiveAvailableHours: number;
actualBookedHours: number;
expectedBookedHours: number;
targetBookedHours: number;
publicHolidayHoursDeduction: number;
absenceDayEquivalent: number;
absenceHoursDeduction: number;
unassignedHours: number;
}>();
for (const row of rows) {
const chapter = row.chapter ?? "Unassigned";
const summary = chapterMap.get(chapter) ?? {
headcount: 0,
avgTargetSum: 0,
avgActualSum: 0,
avgExpectedSum: 0,
derivedHeadcount: 0,
baseAvailableHours: 0,
effectiveAvailableHours: 0,
actualBookedHours: 0,
expectedBookedHours: 0,
targetBookedHours: 0,
publicHolidayHoursDeduction: 0,
absenceDayEquivalent: 0,
absenceHoursDeduction: 0,
unassignedHours: 0,
};
summary.headcount += 1;
summary.avgTargetSum += row.chargeabilityTarget;
summary.avgActualSum += row.actualChargeability;
summary.avgExpectedSum += row.expectedChargeability;
if (row.derivation) {
summary.derivedHeadcount += 1;
summary.baseAvailableHours += row.derivation.baseAvailableHours;
summary.effectiveAvailableHours += row.derivation.effectiveAvailableHours;
summary.actualBookedHours += row.derivation.actualBookedHours;
summary.expectedBookedHours += row.derivation.expectedBookedHours;
summary.targetBookedHours += row.derivation.targetBookedHours;
summary.publicHolidayHoursDeduction += row.derivation.publicHolidayHoursDeduction;
summary.absenceDayEquivalent += row.derivation.absenceDayEquivalent;
summary.absenceHoursDeduction += row.derivation.absenceHoursDeduction;
summary.unassignedHours += row.derivation.unassignedHours;
}
chapterMap.set(chapter, summary);
}
return [...chapterMap.entries()]
.map(([chapter, summary]) => ({
chapter,
headcount: summary.headcount,
avgTargetPct: Math.round(summary.avgTargetSum / summary.headcount),
avgActualPct: Math.round(summary.avgActualSum / summary.headcount),
avgExpectedPct: Math.round(summary.avgExpectedSum / summary.headcount),
gapToTargetPct: Math.round((summary.avgTargetSum - summary.avgActualSum) / summary.headcount),
avgTarget: formatPct(summary.avgTargetSum / summary.headcount),
avgActual: formatPct(summary.avgActualSum / summary.headcount),
avgExpected: formatPct(summary.avgExpectedSum / summary.headcount),
explainability: summary.derivedHeadcount > 0
? {
month,
resourceCount: summary.headcount,
derivedHeadcount: summary.derivedHeadcount,
baseAvailableHours: round1(summary.baseAvailableHours),
effectiveAvailableHours: round1(summary.effectiveAvailableHours),
actualBookedHours: round1(summary.actualBookedHours),
expectedBookedHours: round1(summary.expectedBookedHours),
targetBookedHours: round1(summary.targetBookedHours),
publicHolidayHoursDeduction: round1(summary.publicHolidayHoursDeduction),
absenceDayEquivalent: round1(summary.absenceDayEquivalent),
absenceHoursDeduction: round1(summary.absenceHoursDeduction),
unassignedHours: round1(summary.unassignedHours),
}
: null,
}))
.sort((left, right) => (
right.headcount - left.headcount
|| left.chapter.localeCompare(right.chapter)
));
}
export async function getDashboardOverviewRead(ctx: DashboardProcedureContext) {
return getOverviewCached(ctx.db);
}
@@ -211,7 +330,6 @@ export async function getDashboardDetail(ctx: DashboardProcedureContext, input:
section === "all"
|| section === "peak_times"
|| section === "demand_pipeline"
|| section === "chargeability_overview"
);
const overview = needsOverview ? await getOverviewCached(ctx.db) : null;
const now = new Date();
@@ -309,11 +427,15 @@ export async function getDashboardDetail(ctx: DashboardProcedureContext, input:
}
if (section === "all" || section === "chargeability_overview") {
result.chargeabilityByChapter = (overview?.chapterUtilization ?? []).map((chapter) => ({
chapter: chapter.chapter ?? "Unassigned",
headcount: chapter.resourceCount,
avgTarget: `${Math.round(chapter.avgChargeabilityTarget)}%`,
}));
const chargeabilityOverview = await getChargeabilityOverviewCached(ctx.db, {
includeProposed: false,
topN: 10,
watchlistThreshold: 15,
});
result.chargeabilityByChapter = mapChargeabilityByChapter(
chargeabilityOverview.rows,
chargeabilityOverview.month,
);
}
return result;
@@ -323,32 +445,17 @@ export async function getDashboardChargeabilityOverviewRead(
ctx: DashboardProcedureContext,
input: DashboardChargeabilityOverviewInput,
) {
const cacheKey = `chargeability:${input.includeProposed}:${input.topN}:${input.watchlistThreshold}:${(input.countryIds ?? []).join(",")}:${input.departed ?? ""}`;
const cached = await cacheGet<{
top: unknown[];
watchlist: unknown[];
[key: string]: unknown;
}>(cacheKey);
if (cached) return cached;
const [overview, directory] = await Promise.all([
getDashboardChargeabilityOverview(ctx.db, {
includeProposed: input.includeProposed,
topN: input.topN,
watchlistThreshold: input.watchlistThreshold,
...(input.countryIds !== undefined ? { countryIds: input.countryIds } : {}),
...(input.departed !== undefined ? { departed: input.departed } : {}),
}),
getChargeabilityOverviewCached(ctx.db, input),
getAnonymizationDirectory(ctx.db),
]);
const { rows: _rows, top, watchlist, ...rest } = overview;
const result = {
...overview,
top: anonymizeResources(overview.top, directory),
watchlist: anonymizeResources(overview.watchlist, directory),
return {
...rest,
top: anonymizeResources(top, directory),
watchlist: anonymizeResources(watchlist, directory),
};
await cacheSet(cacheKey, result, DEFAULT_TTL);
return result;
}
export async function getDashboardBudgetForecastRead(