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. 116
      packages/nocodb/src/lib/meta/api/utilApis.ts

116
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,44 +216,55 @@ 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;
for (const project of projects) {
if (project.uuid) result.sharedBaseCount++; if (project.uuid) result.sharedBaseCount++;
const tableCount = await Noco.ncMeta.metaCount( const [
null, tableCount,
null, dbViewCount,
MetaTable.MODELS, viewCount,
{ webhookCount,
filterCount,
sortCount,
rowCount,
userCount,
] = await Promise.all([
// db tables count
Noco.ncMeta.metaCount(null, null, MetaTable.MODELS, {
condition: { condition: {
project_id: project.id, project_id: project.id,
type: 'table', type: 'table',
}, },
} }),
); // db views count
Noco.ncMeta.metaCount(null, null, MetaTable.MODELS, {
const dbViewCount = await Noco.ncMeta.metaCount(
null,
null,
MetaTable.MODELS,
{
condition: { condition: {
project_id: project.id, project_id: project.id,
type: 'view', type: 'view',
}, },
} }),
// views count
(async () => {
const views = await Noco.ncMeta.metaList2(
null,
null,
MetaTable.VIEWS
); );
const views = await Noco.ncMeta.metaList2(null, null, MetaTable.VIEWS); // grid, form, gallery, kanban and shared count
const viewCount = views.reduce<AllMeta['projects'][number]['viewCount']>( return views.reduce<AllMeta['projects'][number]['viewCount']>(
(out, view) => { (out, view) => {
out.total++; out.total++;
@ -274,7 +286,7 @@ export async function getAggregatedMetaInfo(_req: Request, res: Response) {
if (view.uuid) out.sharedKanbanCount++; if (view.uuid) out.sharedKanbanCount++;
} }
if (view.uuid && view.password) out.sharedPasswordProtected++; if (view.uuid && view.password) out.sharedLockedCount++;
return out; return out;
}, },
@ -289,35 +301,30 @@ export async function getAggregatedMetaInfo(_req: Request, res: Response) {
sharedGalleryCount: 0, sharedGalleryCount: 0,
sharedKanbanCount: 0, sharedKanbanCount: 0,
sharedTotal: 0, sharedTotal: 0,
sharedPasswordProtected: 0, sharedLockedCount: 0,
} }
); );
})(),
result.projects.push({ // webhooks count
tableCount: { table: tableCount, view: dbViewCount }, Noco.ncMeta.metaCount(null, null, MetaTable.HOOKS, {
viewCount,
webhookCount: await Noco.ncMeta.metaCount(null, null, MetaTable.HOOKS, {
condition: { condition: {
project_id: project.id, project_id: project.id,
}, },
}), }),
filterCount: await Noco.ncMeta.metaCount( // filters count
null, Noco.ncMeta.metaCount(null, null, MetaTable.FILTER_EXP, {
null,
MetaTable.FILTER_EXP,
{
condition: { condition: {
project_id: project.id, project_id: project.id,
}, },
} }),
), // sorts count
sortCount: await Noco.ncMeta.metaCount(null, null, MetaTable.SORT, { Noco.ncMeta.metaCount(null, null, MetaTable.SORT, {
condition: { condition: {
project_id: project.id, project_id: project.id,
}, },
}), }),
rowCount: await project.getBases().then((bases) => { // row count per base
project.getBases().then((bases) => {
return Promise.all( return Promise.all(
bases.map((base) => bases.map((base) =>
NcConnectionMgrv2.getSqlClient(base) NcConnectionMgrv2.getSqlClient(base)
@ -326,19 +333,28 @@ export async function getAggregatedMetaInfo(_req: Request, res: Response) {
) )
); );
}), }),
userCount: await Noco.ncMeta.metaCount( // project users count
null, Noco.ncMeta.metaCount(null, null, MetaTable.PROJECT_USERS, {
null,
MetaTable.PROJECT_USERS,
{
condition: { condition: {
project_id: project.id, project_id: project.id,
}, },
aggField: '*', aggField: '*',
} }),
), ]);
});
} return {
tableCount: { table: tableCount, view: dbViewCount },
external: !project.is_meta,
viewCount,
webhookCount,
filterCount,
sortCount,
rowCount,
userCount,
};
})
))
);
res.json(result); res.json(result);
} }

Loading…
Cancel
Save