Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/db/src/kpi.server.ts
T

39 lines
1.2 KiB
TypeScript

import { getDbClient, type DbClient } from "./client.server.js";
export type AdminKpiCounts = {
added: number;
total: number;
};
export type AdminKpiPeriod = {
gte?: Date;
};
export type AdminKpiClient = Pick<DbClient, "tenant" | "log">;
function buildCreatedAtWhere({ gte }: AdminKpiPeriod) {
return gte ? { createdAt: { gte } } : undefined;
}
export async function getAdminTenantKpiCounts(period: AdminKpiPeriod, client: AdminKpiClient = getDbClient()): Promise<AdminKpiCounts> {
const addedWhere = buildCreatedAtWhere(period);
const [total, added] = await Promise.all([
client.tenant.count(),
client.tenant.count(addedWhere ? { where: addedWhere } : undefined),
]);
return { added, total };
}
export async function getAdminActiveUserKpiCounts(period: AdminKpiPeriod, client: AdminKpiClient = getDbClient()): Promise<AdminKpiCounts> {
const addedWhere = buildCreatedAtWhere(period);
const [totalGroups, addedGroups] = await Promise.all([
client.log.groupBy({ by: ["userId"] }),
addedWhere
? client.log.groupBy({ by: ["userId"], where: addedWhere })
: client.log.groupBy({ by: ["userId"] }),
]);
return { added: addedGroups.length, total: totalGroups.length };
}