Merge pull request #4173 from overleaf/bg-add-service-worker-counter

[misc] serviceWorker: count all the things

GitOrigin-RevId: b9cef1ed96585e9b4c17b8eaf7a586770e06c984
This commit is contained in:
Jakob Ackermann 2021-06-10 12:02:32 +02:00 committed by Copybot
parent fb249cb24f
commit c471cecf79

View file

@ -28,9 +28,16 @@ function getClientContext(clientId) {
version: VERSION,
id: uuid(),
epoch: Date.now(),
failedCount: 0,
tooLargeOverheadCount: 0,
tooManyRequestsCount: 0,
cachedCount: 0,
cachedBytes: 0,
fetchedCount: 0,
fetchedBytes: 0,
requestedCount: 0,
requestedBytes: 0,
compileCount: 0,
}
clientContext = { pdfs, metrics }
CLIENT_CONTEXT.set(clientId, clientContext)
@ -87,12 +94,20 @@ function expirePdfContexts() {
*
* @param {Object} metrics
* @param {number} size
* @param {number} cachedCount
* @param {number} cachedBytes
* @param {number} fetchedCount
* @param {number} fetchedBytes
*/
function trackDownloadStats(metrics, { size, cachedBytes, fetchedBytes }) {
function trackDownloadStats(
metrics,
{ size, cachedCount, cachedBytes, fetchedCount, fetchedBytes }
) {
metrics.cachedCount += cachedCount
metrics.cachedBytes += cachedBytes
metrics.fetchedCount += fetchedCount
metrics.fetchedBytes += fetchedBytes
metrics.requestedCount++
metrics.requestedBytes += size
}
@ -160,6 +175,7 @@ function processCompileRequest(event) {
// Send the service workers metrics to the frontend.
const { metrics } = getClientContext(event.clientId)
metrics.compileCount++
body.serviceWorkerMetrics = metrics
return new Response(JSON.stringify(body), response)
@ -241,7 +257,14 @@ function processPdfRequest(
if (chunks.length + dynamicChunks.length > MAX_SUBREQUEST_COUNT) {
// fall back to the original range request when splitting the range creates
// too many subrequests.
trackDownloadStats(metrics, { size, cachedBytes: 0, fetchedBytes: size })
metrics.tooManyRequestsCount++
trackDownloadStats(metrics, {
size,
cachedCount: 0,
cachedBytes: 0,
fetchedCount: 1,
fetchedBytes: size,
})
return
}
if (
@ -251,7 +274,14 @@ function processPdfRequest(
// fall back to the original range request when a very large amount of
// object data would be requested, unless it is the only object in the
// request.
trackDownloadStats(metrics, { size, cachedBytes: 0, fetchedBytes: size })
metrics.tooLargeOverheadCount++
trackDownloadStats(metrics, {
size,
cachedCount: 0,
cachedBytes: 0,
fetchedCount: 1,
fetchedBytes: size,
})
return
}
@ -280,7 +310,9 @@ function processPdfRequest(
}
})
)
let cachedCount = 0
let cachedBytes = 0
let fetchedCount = 0
let fetchedBytes = 0
const reAssembledBlob = new Uint8Array(size)
event.respondWith(
@ -303,9 +335,11 @@ function processPdfRequest(
// | A BIG IMAGE BLOB |
// | THE FULL PDF |
if (blobFetchDate < pdfCreatedAt) {
cachedCount++
cachedBytes += chunkSize
} else {
// Blobs are fetched in bulk.
fetchedCount++
fetchedBytes += blobSize
}
}
@ -367,7 +401,13 @@ function processPdfRequest(
}
return verifyProcess.then(blob => {
trackDownloadStats(metrics, { size, cachedBytes, fetchedBytes })
trackDownloadStats(metrics, {
size,
cachedCount,
cachedBytes,
fetchedCount,
fetchedBytes,
})
return new Response(blob, {
status: 206,
headers: {
@ -381,7 +421,14 @@ function processPdfRequest(
})
.catch(error => {
fetchedBytes += size
trackDownloadStats(metrics, { size, cachedBytes: 0, fetchedBytes })
metrics.failedCount++
trackDownloadStats(metrics, {
size,
cachedCount: 0,
cachedBytes: 0,
fetchedCount,
fetchedBytes,
})
reportError(event, OError.tag(error, 'failed to compose pdf response'))
return fetch(event.request)
})