Browse Source

refactor(api): do all db operations parallel

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/3962/head
Pranav C 2 years ago
parent
commit
c356bb1ec3
  1. 248
      packages/nocodb/src/lib/meta/api/utilApis.ts

248
packages/nocodb/src/lib/meta/api/utilApis.ts

@ -187,6 +187,7 @@ export async function urlToDbConfig(req: Request, res: Response) {
interface AllMeta { interface AllMeta {
projectCount: number; projectCount: number;
projects: { projects: {
external?: boolean;
tableCount: { tableCount: {
table: number; table: number;
view: number; view: number;
@ -202,7 +203,7 @@ interface AllMeta {
sharedGalleryCount: number; sharedGalleryCount: number;
sharedKanbanCount: number; sharedKanbanCount: number;
sharedTotal: number; sharedTotal: number;
sharedPasswordProtected: number; sharedLockedCount: number;
}; };
webhookCount: number; webhookCount: number;
filterCount: number; filterCount: number;
@ -215,130 +216,145 @@ interface AllMeta {
} }
export async function getAggregatedMetaInfo(_req: Request, res: Response) { export async function getAggregatedMetaInfo(_req: Request, res: Response) {
const [projects, userCount] = await Promise.all([
Project.list({}),
Noco.ncMeta.metaCount(null, null, MetaTable.USERS),
]);
const result: AllMeta = { const result: AllMeta = {
projectCount: 0, projectCount: projects.length,
projects: [], projects: [],
userCount: await Noco.ncMeta.metaCount(null, null, MetaTable.USERS), userCount,
sharedBaseCount: 0, sharedBaseCount: 0,
}; };
const projects = await Project.list({}); result.projects.push(
const userCount = await User.count(); ...(await Promise.all(
result.projectCount = projects.length; projects.map(async (project) => {
result.userCount = userCount; if (project.uuid) result.sharedBaseCount++;
for (const project of projects) { const [
if (project.uuid) result.sharedBaseCount++; tableCount,
const tableCount = await Noco.ncMeta.metaCount( dbViewCount,
null, viewCount,
null, webhookCount,
MetaTable.MODELS, filterCount,
{ sortCount,
condition: { rowCount,
project_id: project.id, userCount,
type: 'table', ] = await Promise.all([
}, // db tables count
} Noco.ncMeta.metaCount(null, null, MetaTable.MODELS, {
); condition: {
project_id: project.id,
const dbViewCount = await Noco.ncMeta.metaCount( type: 'table',
null, },
null, }),
MetaTable.MODELS, // db views count
{ Noco.ncMeta.metaCount(null, null, MetaTable.MODELS, {
condition: { condition: {
project_id: project.id, project_id: project.id,
type: 'view', type: 'view',
}, },
} }),
); // views count
const views = await Noco.ncMeta.metaList2(null, null, MetaTable.VIEWS); (async () => {
const viewCount = views.reduce<AllMeta['projects'][number]['viewCount']>( const views = await Noco.ncMeta.metaList2(
(out, view) => { null,
out.total++; null,
MetaTable.VIEWS
switch (view.type) { );
case ViewTypes.GRID: // grid, form, gallery, kanban and shared count
out.gridCount++; return views.reduce<AllMeta['projects'][number]['viewCount']>(
if (view.uuid) out.sharedGridCount++; (out, view) => {
break; out.total++;
case ViewTypes.FORM:
out.formCount++;
if (view.uuid) out.sharedFormCount++;
break;
case ViewTypes.GALLERY:
out.galleryCount++;
if (view.uuid) out.sharedGalleryCount++;
break;
case ViewTypes.KANBAN:
out.kanbanCount++;
if (view.uuid) out.sharedKanbanCount++;
}
if (view.uuid && view.password) out.sharedPasswordProtected++; switch (view.type) {
case ViewTypes.GRID:
out.gridCount++;
if (view.uuid) out.sharedGridCount++;
break;
case ViewTypes.FORM:
out.formCount++;
if (view.uuid) out.sharedFormCount++;
break;
case ViewTypes.GALLERY:
out.galleryCount++;
if (view.uuid) out.sharedGalleryCount++;
break;
case ViewTypes.KANBAN:
out.kanbanCount++;
if (view.uuid) out.sharedKanbanCount++;
}
return out; if (view.uuid && view.password) out.sharedLockedCount++;
},
{
formCount: 0,
gridCount: 0,
galleryCount: 0,
kanbanCount: 0,
total: 0,
sharedFormCount: 0,
sharedGridCount: 0,
sharedGalleryCount: 0,
sharedKanbanCount: 0,
sharedTotal: 0,
sharedPasswordProtected: 0,
}
);
result.projects.push({ return out;
tableCount: { table: tableCount, view: dbViewCount }, },
{
formCount: 0,
gridCount: 0,
galleryCount: 0,
kanbanCount: 0,
total: 0,
sharedFormCount: 0,
sharedGridCount: 0,
sharedGalleryCount: 0,
sharedKanbanCount: 0,
sharedTotal: 0,
sharedLockedCount: 0,
}
);
})(),
// webhooks count
Noco.ncMeta.metaCount(null, null, MetaTable.HOOKS, {
condition: {
project_id: project.id,
},
}),
// filters count
Noco.ncMeta.metaCount(null, null, MetaTable.FILTER_EXP, {
condition: {
project_id: project.id,
},
}),
// sorts count
Noco.ncMeta.metaCount(null, null, MetaTable.SORT, {
condition: {
project_id: project.id,
},
}),
// row count per base
project.getBases().then((bases) => {
return Promise.all(
bases.map((base) =>
NcConnectionMgrv2.getSqlClient(base)
.totalRecords?.()
?.then((result) => result?.data)
)
);
}),
// project users count
Noco.ncMeta.metaCount(null, null, MetaTable.PROJECT_USERS, {
condition: {
project_id: project.id,
},
aggField: '*',
}),
]);
viewCount, return {
webhookCount: await Noco.ncMeta.metaCount(null, null, MetaTable.HOOKS, { tableCount: { table: tableCount, view: dbViewCount },
condition: { external: !project.is_meta,
project_id: project.id, viewCount,
}, webhookCount,
}), filterCount,
filterCount: await Noco.ncMeta.metaCount( sortCount,
null, rowCount,
null, userCount,
MetaTable.FILTER_EXP, };
{ })
condition: { ))
project_id: project.id, );
},
}
),
sortCount: await Noco.ncMeta.metaCount(null, null, MetaTable.SORT, {
condition: {
project_id: project.id,
},
}),
rowCount: await project.getBases().then((bases) => {
return Promise.all(
bases.map((base) =>
NcConnectionMgrv2.getSqlClient(base)
.totalRecords?.()
?.then((result) => result?.data)
)
);
}),
userCount: await Noco.ncMeta.metaCount(
null,
null,
MetaTable.PROJECT_USERS,
{
condition: {
project_id: project.id,
},
aggField: '*',
}
),
});
}
res.json(result); res.json(result);
} }

Loading…
Cancel
Save