refactor parallel?

This commit is contained in:
Gaspard Jankowiak 2026-06-05 08:53:20 +02:00
commit ace00a0f14

View file

@ -19,11 +19,7 @@ async function getXSRFToken() {
return decodeURIComponent(tokenCookie.split("=").slice(1).join("=")) return decodeURIComponent(tokenCookie.split("=").slice(1).join("="))
} }
async function getApplicant(applicant, token) { async function fetchApplicantDetails(applicant, token) {
return getApplicantDetailsWithProgress(applicant, token)
}
async function getApplicantDetailsWithProgress(applicant, token, onProgress = () => { }) {
return fetch(`https://personal.uni-graz.at/api/erec/job-applications/${applicant.id}`, return fetch(`https://personal.uni-graz.at/api/erec/job-applications/${applicant.id}`,
{ {
credentials: "same-origin", credentials: "same-origin",
@ -35,34 +31,40 @@ async function getApplicantDetailsWithProgress(applicant, token, onProgress = ()
} }
throw `Failed to get applicant ${applicant.id}` throw `Failed to get applicant ${applicant.id}`
}) })
.then(async (jsonData) => { }
const files = jsonData.application_files ?? []
let completedDownloads = 0
let downloadedBytes = 0
onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes }) async function downloadApplicantFilesWithProgress(applicantDetails, token, onProgress = () => { }) {
const files = applicantDetails.application_files ?? []
let completedDownloads = 0
let downloadedBytes = 0
await Promise.all(files.map(async (afile) => { onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes })
const response = await fetch(
`https://personal.uni-graz.at/api/erec/download-file/${afile.file_id}`,
{
credentials: "same-origin",
headers: { "X-XSRF-TOKEN": token }
}
)
if (!response.ok) { await Promise.all(files.map(async (afile) => {
throw `Failed to download file ${afile.file_id} for applicant ${applicant.id}` const response = await fetch(
} `https://personal.uni-graz.at/api/erec/download-file/${afile.file_id}`,
{
credentials: "same-origin",
headers: { "X-XSRF-TOKEN": token }
}
)
afile.blob = await response.blob() if (!response.ok) {
completedDownloads += 1 throw `Failed to download file ${afile.file_id} for applicant ${applicantDetails.id}`
downloadedBytes += afile.blob.size }
onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes })
}))
return jsonData afile.blob = await response.blob()
}) completedDownloads += 1
downloadedBytes += afile.blob.size
onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes })
}))
return applicantDetails
}
async function getApplicant(applicant, token, onProgress = () => { }) {
const applicantDetails = await fetchApplicantDetails(applicant, token)
return downloadApplicantFilesWithProgress(applicantDetails, token, onProgress)
} }
async function getApplicants() { async function getApplicants() {
@ -137,8 +139,8 @@ function formatMegabytes(totalBytes) {
function createProgressDialog() { function createProgressDialog() {
const dialog = document.createElement("dialog") const dialog = document.createElement("dialog")
const title = document.createElement("h2") const title = document.createElement("h4")
const status = document.createElement("p") const status = document.createElement("h2")
const list = document.createElement("ul") const list = document.createElement("ul")
const summary = document.createElement("div") const summary = document.createElement("div")
const elapsed = document.createElement("p") const elapsed = document.createElement("p")
@ -153,9 +155,9 @@ function createProgressDialog() {
let timerId = null let timerId = null
dialog.classList.add("ripper-progress-dialog") dialog.classList.add("ripper-progress-dialog")
title.textContent = "Download progress" title.textContent = "EPAS Ripper"
status.classList.add("ripper-progress-status") status.classList.add("ripper-progress-status")
status.textContent = "Retrieving applicants list..." status.textContent = "Retrieving applicants list (will take some time)..."
list.classList.add("ripper-progress-list") list.classList.add("ripper-progress-list")
summary.classList.add("ripper-progress-summary") summary.classList.add("ripper-progress-summary")
elapsed.classList.add("ripper-progress-elapsed") elapsed.classList.add("ripper-progress-elapsed")
@ -281,7 +283,7 @@ function rip(event) {
const applicantDetailsResults = await Promise.allSettled(applicants.map(async (applicant) => { const applicantDetailsResults = await Promise.allSettled(applicants.map(async (applicant) => {
try { try {
const applicantDetails = await getApplicantDetailsWithProgress(applicant, token, ({ downloaded, total, bytes }) => { const applicantDetails = await getApplicant(applicant, token, ({ downloaded, total, bytes }) => {
progressDialog.updateApplicant(applicant, downloaded, total, bytes) progressDialog.updateApplicant(applicant, downloaded, total, bytes)
}) })
return applicantDetails return applicantDetails
@ -300,7 +302,6 @@ function rip(event) {
} }
const applicantDetails = result.value const applicantDetails = result.value
// const dirName = `${applicantDetails.last_name}_${applicantDetails.first_name}`
const dirName = `${sanitizeZipPathSegment(applicantDetails.last_name)}_${sanitizeZipPathSegment(applicantDetails.first_name)}` const dirName = `${sanitizeZipPathSegment(applicantDetails.last_name)}_${sanitizeZipPathSegment(applicantDetails.first_name)}`
const applicantDir = zip.folder(dirName) const applicantDir = zip.folder(dirName)
const files = applicantDetails.application_files ?? [] const files = applicantDetails.application_files ?? []