Преглед изворни кода

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

[misc] serviceWorker: count all the things

GitOrigin-RevId: b9cef1ed96585e9b4c17b8eaf7a586770e06c984
Jakob Ackermann пре 5 година
родитељ
комит
c471cecf79
1 измењених фајлова са 52 додато и 5 уклоњено
  1. 52 5
      services/web/frontend/js/serviceWorker.js

+ 52 - 5
services/web/frontend/js/serviceWorker.js

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