From 6b827c0bf77e5e14c615f4a5a5c89ce445880f99 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Fri, 17 Feb 2023 17:00:30 +0530 Subject: [PATCH] chore: add response time header Signed-off-by: Pranav C --- .../src/lib/meta/api/dataApis/dataAliasApis.ts | 13 +++++++++++-- .../nocodb/src/lib/meta/api/helpers/apiHelpers.ts | 4 ++++ packages/nocodb/src/lib/meta/api/helpers/index.ts | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 packages/nocodb/src/lib/meta/api/helpers/apiHelpers.ts diff --git a/packages/nocodb/src/lib/meta/api/dataApis/dataAliasApis.ts b/packages/nocodb/src/lib/meta/api/dataApis/dataAliasApis.ts index e3a340268b..4ef4b7ce5e 100644 --- a/packages/nocodb/src/lib/meta/api/dataApis/dataAliasApis.ts +++ b/packages/nocodb/src/lib/meta/api/dataApis/dataAliasApis.ts @@ -10,11 +10,16 @@ import ncMetaAclMw from '../../helpers/ncMetaAclMw'; import { getViewAndModelFromRequestByAliasOrId } from './helpers'; import apiMetrics from '../../helpers/apiMetrics'; import getAst from '../../../db/sql-data-mapper/lib/sql/helpers/getAst'; +import { parseHrtimeToSeconds } from '../helpers'; // todo: Handle the error case where view doesnt belong to model async function dataList(req: Request, res: Response) { + const startTime = process.hrtime(); const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - res.json(await getDataList(model, view, req)); + const responseData = await getDataList(model, view, req); + const elapsedSeconds = parseHrtimeToSeconds(process.hrtime(startTime)); + res.setHeader('xc-db-response', elapsedSeconds); + res.json(responseData); } async function dataFindOne(req: Request, res: Response) { @@ -226,8 +231,12 @@ async function dataExist(req: Request, res: Response) { // todo: Handle the error case where view doesnt belong to model async function groupedDataList(req: Request, res: Response) { + const startTime = process.hrtime(); const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - res.json(await getGroupedDataList(model, view, req)); + const groupedData = await getGroupedDataList(model, view, req); + const elapsedSeconds = parseHrtimeToSeconds(process.hrtime(startTime)); + res.setHeader('xc-db-response', elapsedSeconds); + res.json(groupedData); } async function getGroupedDataList(model, view: View, req) { diff --git a/packages/nocodb/src/lib/meta/api/helpers/apiHelpers.ts b/packages/nocodb/src/lib/meta/api/helpers/apiHelpers.ts new file mode 100644 index 0000000000..4ffeeb82b6 --- /dev/null +++ b/packages/nocodb/src/lib/meta/api/helpers/apiHelpers.ts @@ -0,0 +1,4 @@ +export function parseHrtimeToSeconds(hrtime) { + const seconds = (hrtime[0] + hrtime[1] / 1e6).toFixed(3); + return seconds; +} diff --git a/packages/nocodb/src/lib/meta/api/helpers/index.ts b/packages/nocodb/src/lib/meta/api/helpers/index.ts index 1b1e81ecfa..1fd8d4367b 100644 --- a/packages/nocodb/src/lib/meta/api/helpers/index.ts +++ b/packages/nocodb/src/lib/meta/api/helpers/index.ts @@ -1,4 +1,5 @@ import { populateMeta } from './populateMeta'; export * from './columnHelpers'; +export * from './apiHelpers'; export { populateMeta };