shellphone.app/app/public-area/queries/get-metrics.ts

30 lines
720 B
TypeScript
Raw Normal View History

2021-08-28 20:17:01 +00:00
import { resolver } from "blitz";
2021-08-29 13:10:25 +00:00
2021-08-28 20:17:01 +00:00
import db from "../../../db";
export default resolver.pipe(async () => {
2021-10-20 22:24:29 +00:00
const [users, phoneNumbers, smsExchanged, allPhoneCalls] = await Promise.all([
db.user.count(),
2021-08-29 13:10:25 +00:00
db.phoneNumber.count(),
db.message.count(),
db.phoneCall.findMany(),
]);
const secondsCalled = allPhoneCalls.reduce<number>((seconds, phoneCall) => {
2021-08-28 20:17:01 +00:00
if (!phoneCall.duration) {
2021-08-29 13:10:25 +00:00
return seconds;
2021-08-28 20:17:01 +00:00
}
2021-08-29 13:10:25 +00:00
return seconds + Number.parseInt(phoneCall.duration);
2021-08-28 20:17:01 +00:00
}, 0);
const minutesCalled = Math.round(secondsCalled / 60);
2021-10-20 22:24:29 +00:00
const averageMinutesCalled = (minutesCalled / allPhoneCalls.length).toFixed(2);
2021-08-28 20:17:01 +00:00
return {
2021-10-20 22:24:29 +00:00
users,
2021-08-28 20:17:01 +00:00
phoneNumbers,
smsExchanged,
minutesCalled,
2021-10-20 22:24:29 +00:00
averageMinutesCalled,
2021-08-28 20:17:01 +00:00
};
});