Skip to content

Commit

Permalink
fix: add console log for staging test
Browse files Browse the repository at this point in the history
  • Loading branch information
g-tejas committed Aug 7, 2024
1 parent 7de13de commit 4237790
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ const useDecryptionWorkers = ({

const fasterDownloadsFeature = useFeature('faster-downloads')
const fasterDownloads = fasterDownloadsFeature.on || isDev
if (fasterDownloads) {
console.log('Faster downloads is enabled ⚡')
}

useEffect(() => {
return () => killWorkers(workers)
Expand Down Expand Up @@ -176,6 +173,8 @@ const useDecryptionWorkers = ({
let progress = 0
let timeSinceLastXAttachmentDownload = 0

let totalBlobDownloadTime = 0

return new Promise<DownloadResult>((resolve, reject) => {
reader
.read()
Expand All @@ -186,13 +185,16 @@ const useDecryptionWorkers = ({
// round-robin scheduling
const { workerApi } =
workerPool[receivedRecordCount % numWorkers]
const decryptResult = await workerApi.decryptIntoCsv({
line: result.value,
secretKey,
downloadAttachments,
formId: adminForm._id,
hostOrigin: window.location.origin,
})
const decryptResult = await workerApi.decryptIntoCsv(
{
line: result.value,
secretKey,
downloadAttachments,
formId: adminForm._id,
hostOrigin: window.location.origin,
},
fasterDownloads,
)
progress += 1
onProgress(progress)

Expand Down Expand Up @@ -233,10 +235,13 @@ const useDecryptionWorkers = ({
}
timeSinceLastXAttachmentDownload = now
}
const startTime = performance.now()
await downloadResponseAttachment(
decryptResult.downloadBlob,
decryptResult.id,
)
const delta = performance.now() - startTime
totalBlobDownloadTime += delta
}
}
}
Expand Down Expand Up @@ -358,6 +363,11 @@ const useDecryptionWorkers = ({
timeDifference,
)

console.log({
'Time it took to download blob url':
totalBlobDownloadTime / csvGenerator.length(),
})

resolve({
expectedCount: responsesCount,
successCount: csvGenerator.length(),
Expand All @@ -373,7 +383,7 @@ const useDecryptionWorkers = ({
})
})
},
[adminForm, onProgress, user?._id, workers],
[adminForm, onProgress, user?._id, workers, fasterDownloads],
)

const downloadEncryptedResponsesFaster = useCallback(
Expand All @@ -392,6 +402,8 @@ const useDecryptionWorkers = ({
})
}

console.log('Faster downloads is enabled ⚡')

abortControllerRef.current.abort()
const freshAbortController = new AbortController()
abortControllerRef.current = freshAbortController
Expand Down Expand Up @@ -442,16 +454,21 @@ const useDecryptionWorkers = ({
freshAbortController,
)

let totalBlobDownloadTime = 0

const processTask = async (value: string, workerIdx: number) => {
const { workerApi } = workerPool[workerIdx]

const decryptResult = await workerApi.decryptIntoCsv({
line: value,
secretKey,
downloadAttachments,
formId: adminForm._id,
hostOrigin: window.location.origin,
})
const decryptResult = await workerApi.decryptIntoCsv(
{
line: value,
secretKey,
downloadAttachments,
formId: adminForm._id,
hostOrigin: window.location.origin,
},
fasterDownloads,
)

switch (decryptResult.status) {
case CsvRecordStatus.Ok:
Expand All @@ -466,12 +483,14 @@ const useDecryptionWorkers = ({
// rate limit to pass. If decryption is fast, we would wait regardless.
// If decryption is slow, we won't hit rate limits.
if (downloadAttachments && decryptResult.downloadBlobURL) {
const startTime = performance.now()
await downloadResponseAttachmentURL(
decryptResult.downloadBlobURL,
decryptResult.id,
).then(() => {
URL.revokeObjectURL(decryptResult.downloadBlobURL!)
})
)
URL.revokeObjectURL(decryptResult.downloadBlobURL!)
const delta = performance.now() - startTime
totalBlobDownloadTime += delta
}
break
case CsvRecordStatus.Unknown:
Expand Down Expand Up @@ -660,6 +679,11 @@ const useDecryptionWorkers = ({
timeDifference,
)

console.log({
'Time it took to download blob url':
totalBlobDownloadTime / csvGenerator.length(),
})

resolve({
expectedCount: responsesCount,
successCount: csvGenerator.length(),
Expand All @@ -675,7 +699,7 @@ const useDecryptionWorkers = ({
})
})
},
[adminForm, onProgress, user?._id, workers],
[adminForm, onProgress, user?._id, workers, fasterDownloads],
)

const handleExportCsvMutation = useMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ function verifySignature(
* main thread.
* @param data The data to decrypt into a csvRecord.
*/
async function decryptIntoCsv(data: LineData): Promise<MaterializedCsvRecord> {
async function decryptIntoCsv(
data: LineData,
fasterDownloads: boolean,
): Promise<MaterializedCsvRecord> {
// This needs to be dynamically imported due to sharing code between main app and worker code.
// Fixes issue raised at https://stackoverflow.com/questions/66472945/referenceerror-refreshreg-is-not-defined
// Something to do with babel-loader.
Expand Down Expand Up @@ -185,12 +188,15 @@ async function decryptIntoCsv(data: LineData): Promise<MaterializedCsvRecord> {
attachmentDecryptionKey,
),
)
csvRecord.downloadBlobURL = URL.createObjectURL(downloadBlob) // remember to revokeObjectURL on the link in the main thread
csvRecord.setStatus(
CsvRecordStatus.Ok,
'Success (with Downloaded Attachment)',
)
// csvRecord.setDownloadBlob(downloadBlob)
if (fasterDownloads) {
csvRecord.downloadBlobURL = URL.createObjectURL(downloadBlob)
} else {
csvRecord.setDownloadBlob(downloadBlob)
}
} catch (error) {
csvRecord.setStatus(
CsvRecordStatus.AttachmentError,
Expand Down

0 comments on commit 4237790

Please sign in to comment.