Browse Source

refactor(api): handle all error cases and on error log and return as null

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

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

@ -184,33 +184,38 @@ export async function urlToDbConfig(req: Request, res: Response) {
} }
} }
interface ViewCount {
formCount: number | null;
gridCount: number | null;
galleryCount: number | null;
kanbanCount: number | null;
total: number | null;
sharedFormCount: number | null;
sharedGridCount: number | null;
sharedGalleryCount: number | null;
sharedKanbanCount: number | null;
sharedTotal: number | null;
sharedLockedCount: number | null;
}
interface AllMeta { interface AllMeta {
projectCount: number; projectCount: number;
projects: { projects: (
external?: boolean; | {
tableCount: { external?: boolean | null;
table: number; tableCount: {
view: number; table: number;
}; view: number;
viewCount: { } | null;
formCount: number; viewCount: ViewCount;
gridCount: number; webhookCount: number | null;
galleryCount: number; filterCount: number | null;
kanbanCount: number; sortCount: number | null;
total: number; rowCount: ({ totalRecords: number } | null)[] | null;
sharedFormCount: number; userCount: number | null;
sharedGridCount: number; }
sharedGalleryCount: number; | { error: string }
sharedKanbanCount: number; )[];
sharedTotal: number;
sharedLockedCount: number;
};
webhookCount: number;
filterCount: number;
sortCount: number;
rowCount: { totalRecords: number }[];
userCount: number;
}[];
userCount: number; userCount: number;
sharedBaseCount: number; sharedBaseCount: number;
} }
@ -229,136 +234,138 @@ export async function getAggregatedMetaInfo(_req: Request, res: Response) {
}; };
result.projects.push( result.projects.push(
...(await Promise.all( ...extractResultOrNull(
projects.map(async (project) => { await Promise.allSettled(
if (project.uuid) result.sharedBaseCount++; projects.map(async (project) => {
const [ if (project.uuid) result.sharedBaseCount++;
tableCount, const [
dbViewCount, tableCount,
viewCount, dbViewCount,
webhookCount, viewCount,
filterCount, webhookCount,
sortCount, filterCount,
rowCount, sortCount,
userCount, rowCount,
] = await Promise.all([ userCount,
// db tables count ] = extractResultOrNull(
Noco.ncMeta.metaCount(null, null, MetaTable.MODELS, { await Promise.allSettled([
condition: { // db tables count
project_id: project.id, Noco.ncMeta.metaCount(project.id, null, MetaTable.MODELS, {
type: 'table', condition: {
}, type: 'table',
}), },
// db views count }),
Noco.ncMeta.metaCount(null, null, MetaTable.MODELS, { // db views count
condition: { Noco.ncMeta.metaCount(project.id, null, MetaTable.MODELS, {
project_id: project.id, condition: {
type: 'view', type: 'view',
}, },
}), }),
// views count // views count
(async () => { (async () => {
const views = await Noco.ncMeta.metaList2( const views = await Noco.ncMeta.metaList2(
null, project.id,
null, null,
MetaTable.VIEWS MetaTable.VIEWS
); );
// grid, form, gallery, kanban and shared count // grid, form, gallery, kanban and shared count
return views.reduce<AllMeta['projects'][number]['viewCount']>( return views.reduce<ViewCount>(
(out, view) => { (out, view) => {
out.total++; out.total++;
switch (view.type) { switch (view.type) {
case ViewTypes.GRID: case ViewTypes.GRID:
out.gridCount++; out.gridCount++;
if (view.uuid) out.sharedGridCount++; if (view.uuid) out.sharedGridCount++;
break; break;
case ViewTypes.FORM: case ViewTypes.FORM:
out.formCount++; out.formCount++;
if (view.uuid) out.sharedFormCount++; if (view.uuid) out.sharedFormCount++;
break; break;
case ViewTypes.GALLERY: case ViewTypes.GALLERY:
out.galleryCount++; out.galleryCount++;
if (view.uuid) out.sharedGalleryCount++; if (view.uuid) out.sharedGalleryCount++;
break; break;
case ViewTypes.KANBAN: case ViewTypes.KANBAN:
out.kanbanCount++; out.kanbanCount++;
if (view.uuid) out.sharedKanbanCount++; if (view.uuid) out.sharedKanbanCount++;
} }
if (view.uuid && view.password) out.sharedLockedCount++; if (view.uuid && view.password) out.sharedLockedCount++;
return out; return out;
}, },
{ {
formCount: 0, formCount: 0,
gridCount: 0, gridCount: 0,
galleryCount: 0, galleryCount: 0,
kanbanCount: 0, kanbanCount: 0,
total: 0, total: 0,
sharedFormCount: 0, sharedFormCount: 0,
sharedGridCount: 0, sharedGridCount: 0,
sharedGalleryCount: 0, sharedGalleryCount: 0,
sharedKanbanCount: 0, sharedKanbanCount: 0,
sharedTotal: 0, sharedTotal: 0,
sharedLockedCount: 0, sharedLockedCount: 0,
} }
); );
})(), })(),
// webhooks count // webhooks count
Noco.ncMeta.metaCount(null, null, MetaTable.HOOKS, { Noco.ncMeta.metaCount(project.id, null, MetaTable.HOOKS),
condition: { // filters count
project_id: project.id, Noco.ncMeta.metaCount(project.id, null, MetaTable.FILTER_EXP),
}, // sorts count
}), Noco.ncMeta.metaCount(project.id, null, MetaTable.SORT),
// filters count // row count per base
Noco.ncMeta.metaCount(null, null, MetaTable.FILTER_EXP, { project.getBases().then(async (bases) => {
condition: { return extractResultOrNull(
project_id: project.id, await Promise.allSettled(
}, bases.map((base) =>
}), NcConnectionMgrv2.getSqlClient(base)
// sorts count .totalRecords?.()
Noco.ncMeta.metaCount(null, null, MetaTable.SORT, { ?.then((result) => result?.data)
condition: { )
project_id: project.id, )
}, );
}), }),
// row count per base // project users count
project.getBases().then((bases) => { Noco.ncMeta.metaCount(null, null, MetaTable.PROJECT_USERS, {
return Promise.all( condition: {
bases.map((base) => project_id: project.id,
NcConnectionMgrv2.getSqlClient(base) },
.totalRecords?.() aggField: '*',
?.then((result) => result?.data) }),
) ])
); );
}),
// project users count
Noco.ncMeta.metaCount(null, null, MetaTable.PROJECT_USERS, {
condition: {
project_id: project.id,
},
aggField: '*',
}),
]);
return { return {
tableCount: { table: tableCount, view: dbViewCount }, tableCount: { table: tableCount.status, view: dbViewCount },
external: !project.is_meta, external: !project.is_meta,
viewCount, viewCount,
webhookCount, webhookCount,
filterCount, filterCount,
sortCount, sortCount,
rowCount, rowCount,
userCount, userCount,
}; };
}) })
)) )
)
); );
res.json(result); res.json(result);
} }
const extractResultOrNull = (results: PromiseSettledResult<any>[]) => {
return results.map((result) => {
if (result.status === 'fulfilled') {
return result.value;
}
console.log(result.reason);
return null;
});
};
export default (router) => { export default (router) => {
router.post( router.post(
'/api/v1/db/meta/connection/test', '/api/v1/db/meta/connection/test',

Loading…
Cancel
Save