Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/get api performance #1955

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions apps/web/app/hooks/features/useTaskStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 }) => {
Expand All @@ -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 || !user?.employee.organizationId) {
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,
Expand All @@ -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), []);
Expand All @@ -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
Expand Down
98 changes: 90 additions & 8 deletions apps/web/app/services/client/api/tasks.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -54,16 +55,97 @@ export function createTeamTaskAPI(body: Partial<ICreateTask> & { title: string }
return api.post<PaginationResponse<ITeamTask>>('/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
) {
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${employeeId ? '?employeeId=' + employeeId : ''}`
);
}
}

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({ ...commonParams, 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() {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/services/server/requests/timesheet.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading