fix: download response (#1890)

This commit is contained in:
Dhruwang Jariwala
2024-01-12 01:11:22 +05:30
committed by GitHub
parent f3666b8745
commit 0ff7bb56ec
3 changed files with 34 additions and 9 deletions

View File

@@ -13,12 +13,18 @@ export default async function revalidateSurveyIdPath(environmentId: string, surv
revalidatePath(`/environments/${environmentId}/surveys/${surveyId}`);
}
export async function getMoreResponses(surveyId: string, page: number): Promise<TResponse[]> {
export async function getMoreResponses(
surveyId: string,
page: number,
batchSize?: number
): Promise<TResponse[]> {
const session = await getServerSession(authOptions);
if (!session) throw new AuthorizationError("Not authorized");
const isAuthorized = await canUserAccessSurvey(session.user.id, surveyId);
if (!isAuthorized) throw new AuthorizationError("Not authorized");
const responses = await getResponses(surveyId, page);
batchSize = batchSize ?? 10;
const responses = await getResponses(surveyId, page, batchSize);
return responses;
}

View File

@@ -4,6 +4,7 @@ import {
DateRange,
useResponseFilter,
} from "@/app/(app)/environments/[environmentId]/components/ResponseFilterContext";
import { getMoreResponses } from "@/app/(app)/environments/[environmentId]/surveys/[surveyId]/(analysis)/actions";
import { fetchFile } from "@/app/lib/fetchFile";
import { generateQuestionAndFilterOptions, getTodayDate } from "@/app/lib/surveys/surveys";
import { createId } from "@paralleldrive/cuid2";
@@ -155,9 +156,23 @@ const CustomFilter = ({ environmentTags, responses, survey, totalResponses }: Cu
return keys;
}, []);
const getAllResponsesInBatches = useCallback(async () => {
const BATCH_SIZE = 3000;
const responses: TResponse[] = [];
for (let page = 1; ; page++) {
const batchResponses = await getMoreResponses(survey.id, page, BATCH_SIZE);
responses.push(...batchResponses);
if (batchResponses.length < BATCH_SIZE) {
break;
}
}
return responses;
}, [survey.id]);
const downloadResponses = useCallback(
async (filter: FilterDownload, filetype: "csv" | "xlsx") => {
const downloadResponse = filter === FilterDownload.ALL ? totalResponses : responses;
const downloadResponse = filter === FilterDownload.ALL ? await getAllResponsesInBatches() : responses;
const questionNames = survey.questions?.map((question) => question.headline);
const hiddenFieldIds = survey.hiddenFields.fieldIds;
const hiddenFieldResponse = {};
@@ -269,7 +284,7 @@ const CustomFilter = ({ environmentTags, responses, survey, totalResponses }: Cu
URL.revokeObjectURL(downloadUrl);
},
[downloadFileName, responses, totalResponses, survey, extracMetadataKeys]
[downloadFileName, responses, survey, extracMetadataKeys, getAllResponsesInBatches]
);
const handleDateHoveredChange = (date: Date) => {

View File

@@ -384,11 +384,15 @@ export const getResponse = async (responseId: string): Promise<TResponse | null>
} as TResponse;
};
export const getResponses = async (surveyId: string, page?: number): Promise<TResponse[]> => {
export const getResponses = async (
surveyId: string,
page?: number,
batchSize?: number
): Promise<TResponse[]> => {
const responses = await unstable_cache(
async () => {
validateInputs([surveyId, ZId], [page, ZOptionalNumber]);
batchSize = batchSize ?? RESPONSES_PER_PAGE;
try {
const responses = await prisma.response.findMany({
where: {
@@ -400,8 +404,8 @@ export const getResponses = async (surveyId: string, page?: number): Promise<TRe
createdAt: "desc",
},
],
take: page ? RESPONSES_PER_PAGE : undefined,
skip: page ? RESPONSES_PER_PAGE * (page - 1) : undefined,
take: page ? batchSize : undefined,
skip: page ? batchSize * (page - 1) : undefined,
});
const transformedResponses: TResponse[] = await Promise.all(
@@ -423,7 +427,7 @@ export const getResponses = async (surveyId: string, page?: number): Promise<TRe
throw error;
}
},
[`getResponses-${surveyId}-${page}`],
[`getResponses-${surveyId}-${page}-${batchSize}`],
{
tags: [responseCache.tag.bySurveyId(surveyId)],
revalidate: SERVICES_REVALIDATION_INTERVAL,