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) => {