From 91abc364b73ddb210b599c452bf45acc8719a279 Mon Sep 17 00:00:00 2001 From: Badal Khatri Date: Mon, 4 Dec 2023 08:36:05 +0530 Subject: [PATCH 1/3] WIP: Statistics performance --- apps/web/app/services/client/api/tasks.ts | 46 +++++++++++++++++-- .../app/services/server/requests/timesheet.ts | 2 +- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/apps/web/app/services/client/api/tasks.ts b/apps/web/app/services/client/api/tasks.ts index 086f20eb6..377d8f146 100644 --- a/apps/web/app/services/client/api/tasks.ts +++ b/apps/web/app/services/client/api/tasks.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-mixed-spaces-and-tabs */ import { CreateResponse, DeleteResponse, PaginationResponse } from '@app/interfaces/IDataResponse'; import { ICreateTask, ITeamTask } from '@app/interfaces/ITask'; import { ITasksTimesheet } from '@app/interfaces/ITimer'; @@ -54,10 +55,47 @@ export function createTeamTaskAPI(body: Partial & { title: string } return api.post>('/tasks/team', body); } -export function tasksTimesheetStatisticsAPI(employeeId?: string) { - return api.get<{ global: ITasksTimesheet[]; today: ITasksTimesheet[] }>( - `/timer/timesheet/statistics-tasks${employeeId ? '?employeeId=' + employeeId : ''}` - ); +export async function tasksTimesheetStatisticsAPI( + tenantId: string, + activeTaskId: string, + organizationId: string, + employeeId?: string +) { + console.log('process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL', process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL); + if (process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL) { + const employeesParams = employeeId + ? [employeeId].reduce((acc: any, v, i) => { + acc[`employeeIds[${i}]`] = v; + return acc; + }) + : {}; + const commonParams = { + tenantId, + organizationId, + ...(activeTaskId ? { 'taskIds[0]': activeTaskId } : {}), + ...employeesParams + }; + const globalQueries = new URLSearchParams({ + ...commonParams, + defaultRange: 'false' + }); + const globalData = await get(`/timesheet/statistics/tasks?${globalQueries.toString()}`, true); + + const todayQueries = new URLSearchParams({ + defaultRange: 'true', + unitOfTime: 'day' + }); + const todayData = await get(`/timesheet/statistics/tasks?${todayQueries.toString()}`, true); + + return { + global: globalData.data, + today: todayData.data + }; + } else { + return api.get<{ global: ITasksTimesheet[]; today: ITasksTimesheet[] }>( + `/timer/timesheet/statistics-tasks${employeeId ? '?employeeId=' + employeeId : ''}` + ); + } } export function activeTaskTimesheetStatisticsAPI() { diff --git a/apps/web/app/services/server/requests/timesheet.ts b/apps/web/app/services/server/requests/timesheet.ts index 6b602c895..7b17116a2 100644 --- a/apps/web/app/services/server/requests/timesheet.ts +++ b/apps/web/app/services/server/requests/timesheet.ts @@ -1,7 +1,7 @@ import { ITasksTimesheet } from '@app/interfaces/ITimer'; import { serverFetch } from '../fetch'; -type TTasksTimesheetStatisticsParams = { +export type TTasksTimesheetStatisticsParams = { tenantId: string; organizationId: string; startDate?: string; From 2efdd8beade6975422e3b05084fb1fc601570619 Mon Sep 17 00:00:00 2001 From: Badal Khatri Date: Mon, 4 Dec 2023 13:04:16 +0530 Subject: [PATCH 2/3] Updated API --- .../app/hooks/features/useTaskStatistics.ts | 39 ++++++++--- apps/web/app/services/client/api/tasks.ts | 67 ++++++++++++++++--- 2 files changed, 86 insertions(+), 20 deletions(-) diff --git a/apps/web/app/hooks/features/useTaskStatistics.ts b/apps/web/app/hooks/features/useTaskStatistics.ts index b5c9fa256..04e717858 100644 --- a/apps/web/app/hooks/features/useTaskStatistics.ts +++ b/apps/web/app/hooks/features/useTaskStatistics.ts @@ -21,8 +21,10 @@ import { useSyncRef } from '../useSyncRef'; import { Nullable } from '@app/interfaces'; import { useRefreshInterval } from './useRefreshInterval'; import { useOrganizationTeams } from './useOrganizationTeams'; +import { useAuthenticateUser } from './useAuthenticateUser'; export function useTaskStatistics(addSeconds = 0) { + const { user } = useAuthenticateUser(); const [statActiveTask, setStatActiveTask] = useRecoilState(activeTaskStatisticsState); const [statTasks, setStatTasks] = useRecoilState(tasksStatisticsState); const setTasksFetching = useSetRecoilState(tasksFetchingState); @@ -45,14 +47,19 @@ export function useTaskStatistics(addSeconds = 0) { */ const getTasksStatsData = useCallback( (employeeId?: string) => { - tasksTimesheetStatisticsAPI(employeeId).then(({ data }) => { - setStatTasks({ - all: data.global || [], - today: data.today || [] - }); - }); + if (!user?.employee.tenantId) { + return; + } + tasksTimesheetStatisticsAPI(user?.employee.tenantId, '', user?.employee.organizationId, employeeId).then( + ({ data }) => { + setStatTasks({ + all: data.global || [], + today: data.today || [] + }); + } + ); }, - [setStatTasks] + [setStatTasks, user?.employee.organizationId, user?.employee.tenantId] ); const getAllTasksStatsData = useCallback(() => { allTaskTimesheetStatisticsAPI().then(({ data }) => { @@ -78,8 +85,20 @@ export function useTaskStatistics(addSeconds = 0) { * Get statistics of the active tasks fresh (API Call) */ const getActiveTaskStatData = useCallback(() => { + if (!user?.employee.tenantId) { + return new Promise((resolve) => { + resolve(true); + }); + } + setTasksFetching(true); - const promise = activeTaskTimesheetStatisticsAPI(); + + const promise = activeTaskTimesheetStatisticsAPI( + user?.employee.tenantId, + '', + user?.employee.organizationId, + '' + ); promise.then(({ data }) => { setStatActiveTask({ total: data.global ? data.global[0] || null : null, @@ -90,7 +109,7 @@ export function useTaskStatistics(addSeconds = 0) { setTasksFetching(false); }); return promise; - }, [setStatActiveTask, setTasksFetching]); + }, [setStatActiveTask, setTasksFetching, user?.employee.organizationId, user?.employee.tenantId]); // eslint-disable-next-line react-hooks/exhaustive-deps const debounceLoadActiveTaskStat = useCallback(debounce(getActiveTaskStatData, 100), []); @@ -104,7 +123,7 @@ export function useTaskStatistics(addSeconds = 0) { initialLoad.current = true; }); } - }, [firstLoad, getActiveTaskStatData]); + }, [firstLoad, getActiveTaskStatData, user?.employee.organizationId, user?.employee.tenantId]); /** * Get fresh statistic of the active task diff --git a/apps/web/app/services/client/api/tasks.ts b/apps/web/app/services/client/api/tasks.ts index 377d8f146..4fe919540 100644 --- a/apps/web/app/services/client/api/tasks.ts +++ b/apps/web/app/services/client/api/tasks.ts @@ -61,7 +61,6 @@ export async function tasksTimesheetStatisticsAPI( organizationId: string, employeeId?: string ) { - console.log('process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL', process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL); if (process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL) { const employeesParams = employeeId ? [employeeId].reduce((acc: any, v, i) => { @@ -72,24 +71,30 @@ export async function tasksTimesheetStatisticsAPI( const commonParams = { tenantId, organizationId, - ...(activeTaskId ? { 'taskIds[0]': activeTaskId } : {}), + // ...(activeTaskId ? { 'taskIds[0]': activeTaskId } : {}), ...employeesParams }; const globalQueries = new URLSearchParams({ ...commonParams, defaultRange: 'false' }); - const globalData = await get(`/timesheet/statistics/tasks?${globalQueries.toString()}`, true); + const globalData = await get(`/timesheet/statistics/tasks?${globalQueries.toString()}`, true, { + tenantId + }); const todayQueries = new URLSearchParams({ defaultRange: 'true', unitOfTime: 'day' }); - const todayData = await get(`/timesheet/statistics/tasks?${todayQueries.toString()}`, true); + const todayData = await get(`/timesheet/statistics/tasks?${todayQueries.toString()}`, true, { + tenantId + }); return { - global: globalData.data, - today: todayData.data + data: { + global: globalData.data, + today: todayData.data + } }; } else { return api.get<{ global: ITasksTimesheet[]; today: ITasksTimesheet[] }>( @@ -98,10 +103,52 @@ export async function tasksTimesheetStatisticsAPI( } } -export function activeTaskTimesheetStatisticsAPI() { - return api.get<{ global: ITasksTimesheet[]; today: ITasksTimesheet[] }>( - `/timer/timesheet/statistics-tasks?activeTask=true` - ); +export async function activeTaskTimesheetStatisticsAPI( + tenantId: string, + activeTaskId: string, + organizationId: string, + employeeId?: string +) { + if (process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL) { + const employeesParams = employeeId + ? [employeeId].reduce((acc: any, v, i) => { + acc[`employeeIds[${i}]`] = v; + return acc; + }) + : {}; + const commonParams = { + tenantId, + organizationId, + ...(activeTaskId ? { 'taskIds[0]': activeTaskId } : {}), + ...employeesParams + }; + const globalQueries = new URLSearchParams({ + ...commonParams, + defaultRange: 'false' + }); + const globalData = await get(`/timesheet/statistics/tasks?${globalQueries.toString()}`, true, { + tenantId + }); + + const todayQueries = new URLSearchParams({ + defaultRange: 'true', + unitOfTime: 'day' + }); + const todayData = await get(`/timesheet/statistics/tasks?${todayQueries.toString()}`, true, { + tenantId + }); + + return { + data: { + global: globalData.data, + today: todayData.data + } + }; + } else { + return api.get<{ global: ITasksTimesheet[]; today: ITasksTimesheet[] }>( + `/timer/timesheet/statistics-tasks?activeTask=true` + ); + } } export function allTaskTimesheetStatisticsAPI() { From 0a34e09e88f2f3b0c787887a05ed8539a190b95a Mon Sep 17 00:00:00 2001 From: Badal Khatri Date: Mon, 4 Dec 2023 16:09:52 +0530 Subject: [PATCH 3/3] fix: Query Params --- apps/web/app/hooks/features/useTaskStatistics.ts | 2 +- apps/web/app/services/client/api/tasks.ts | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/apps/web/app/hooks/features/useTaskStatistics.ts b/apps/web/app/hooks/features/useTaskStatistics.ts index 04e717858..fd44986a7 100644 --- a/apps/web/app/hooks/features/useTaskStatistics.ts +++ b/apps/web/app/hooks/features/useTaskStatistics.ts @@ -85,7 +85,7 @@ export function useTaskStatistics(addSeconds = 0) { * Get statistics of the active tasks fresh (API Call) */ const getActiveTaskStatData = useCallback(() => { - if (!user?.employee.tenantId) { + if (!user?.employee.tenantId || !user?.employee.organizationId) { return new Promise((resolve) => { resolve(true); }); diff --git a/apps/web/app/services/client/api/tasks.ts b/apps/web/app/services/client/api/tasks.ts index 4fe919540..62c81e640 100644 --- a/apps/web/app/services/client/api/tasks.ts +++ b/apps/web/app/services/client/api/tasks.ts @@ -130,10 +130,7 @@ export async function activeTaskTimesheetStatisticsAPI( tenantId }); - const todayQueries = new URLSearchParams({ - defaultRange: 'true', - unitOfTime: 'day' - }); + const todayQueries = new URLSearchParams({ ...commonParams, defaultRange: 'true', unitOfTime: 'day' }); const todayData = await get(`/timesheet/statistics/tasks?${todayQueries.toString()}`, true, { tenantId });