Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { Entity } from "@toolctrl/db/prisma-browser";
|
||||
import { Stat } from "~/application/dtos/stats/Stat";
|
||||
import { getStatChangeType } from "../app/DashboardUtils";
|
||||
import { db } from "../db.server";
|
||||
import { getAllEntities } from "../db/entities/entities.db.server";
|
||||
import DateUtils from "../shared/DateUtils";
|
||||
|
||||
export async function getCustomEntitiesDashboardStats(lastDays: number): Promise<Stat[]> {
|
||||
const entities = await getAllEntities({ tenantId: null });
|
||||
const stats = entities.map(async (entity) => {
|
||||
return await getEntityStat(entity, lastDays);
|
||||
});
|
||||
return await Promise.all(stats);
|
||||
}
|
||||
|
||||
async function getEntityStat(entity: Entity, lastDays: number) {
|
||||
const { added, total } = await getEntityCreatedSince(entity.id, lastDays);
|
||||
const tenantStat: Stat = {
|
||||
name: entity.titlePlural,
|
||||
hint: "",
|
||||
stat: added.toString(),
|
||||
previousStat: (total - added).toString(),
|
||||
change: "+" + added.toString(),
|
||||
changeType: getStatChangeType(added, total),
|
||||
};
|
||||
return tenantStat;
|
||||
}
|
||||
|
||||
async function getEntityCreatedSince(entityId: string, lastDays: number) {
|
||||
const from = DateUtils.daysFromDate(new Date(), lastDays * -1);
|
||||
const [added, total] = await Promise.all([
|
||||
db.row.count({
|
||||
where: {
|
||||
entityId,
|
||||
createdAt: {
|
||||
gte: from,
|
||||
},
|
||||
},
|
||||
}),
|
||||
db.row.count({
|
||||
where: {
|
||||
entityId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
added,
|
||||
total,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user