parallelize metric fetching requests

This commit is contained in:
m5r 2021-08-29 21:10:25 +08:00
parent e65922a1a0
commit 549558142d

View File

@ -1,16 +1,19 @@
import { resolver } from "blitz";
import db from "../../../db";
export default resolver.pipe(async () => {
const phoneNumbers = await db.phoneNumber.count();
const smsExchanged = await db.message.count();
const allPhoneCalls = await db.phoneCall.findMany();
const secondsCalled = allPhoneCalls.reduce<number>((minutes, phoneCall) => {
const [phoneNumbers, smsExchanged, allPhoneCalls] = await Promise.all([
db.phoneNumber.count(),
db.message.count(),
db.phoneCall.findMany(),
]);
const secondsCalled = allPhoneCalls.reduce<number>((seconds, phoneCall) => {
if (!phoneCall.duration) {
return minutes;
return seconds;
}
return minutes + Number.parseInt(phoneCall.duration);
return seconds + Number.parseInt(phoneCall.duration);
}, 0);
const minutesCalled = Math.round(secondsCalled / 60);