2021-05-21 07:32:07 -04:00
|
|
|
import { v4 as uuid } from 'uuid'
|
2021-06-01 09:53:27 -04:00
|
|
|
import { sendMB } from '../../../infrastructure/event-tracking'
|
2022-06-21 08:15:26 -04:00
|
|
|
import getMeta from '../../../utils/meta'
|
2021-05-21 07:32:07 -04:00
|
|
|
|
2021-06-14 04:44:25 -04:00
|
|
|
// VERSION should get incremented when making changes to caching behavior or
|
|
|
|
// adjusting metrics collection.
|
|
|
|
// Keep in sync with the service worker.
|
|
|
|
const VERSION = 2
|
|
|
|
|
2021-05-21 07:32:07 -04:00
|
|
|
const pdfJsMetrics = {
|
2021-06-14 04:44:25 -04:00
|
|
|
version: VERSION,
|
2021-05-21 07:32:07 -04:00
|
|
|
id: uuid(),
|
|
|
|
epoch: Date.now(),
|
|
|
|
totalBandwidth: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
const SAMPLING_RATE = 0.01
|
|
|
|
|
2021-06-01 09:52:38 -04:00
|
|
|
export function trackPdfDownload(response, compileTimeClientE2E) {
|
2021-05-21 07:32:07 -04:00
|
|
|
const { serviceWorkerMetrics, stats, timings } = response
|
|
|
|
|
|
|
|
const t0 = performance.now()
|
|
|
|
let bandwidth = 0
|
2022-06-21 08:15:26 -04:00
|
|
|
const deliveryLatencies = {
|
|
|
|
compileTimeClientE2E,
|
|
|
|
compileTimeServerE2E: timings?.compileE2E,
|
|
|
|
}
|
|
|
|
|
2021-05-21 07:32:07 -04:00
|
|
|
function firstRenderDone({ timePDFFetched, timePDFRendered }) {
|
2022-06-21 08:15:26 -04:00
|
|
|
const latencyFetch = Math.ceil(timePDFFetched - t0)
|
|
|
|
deliveryLatencies.latencyFetch = latencyFetch
|
2021-05-21 07:32:07 -04:00
|
|
|
// The renderer does not yield in case the browser tab is hidden.
|
|
|
|
// It will yield when the browser tab is visible again.
|
|
|
|
// This will skew our performance metrics for rendering!
|
2021-06-01 09:53:14 -04:00
|
|
|
// We are omitting the render time in case we detect this state.
|
|
|
|
let latencyRender
|
|
|
|
if (timePDFRendered) {
|
2022-06-21 08:15:26 -04:00
|
|
|
latencyRender = Math.ceil(timePDFRendered - timePDFFetched)
|
|
|
|
deliveryLatencies.latencyRender = latencyRender
|
2021-06-01 09:53:14 -04:00
|
|
|
}
|
2021-05-21 07:32:07 -04:00
|
|
|
done({ latencyFetch, latencyRender })
|
|
|
|
}
|
|
|
|
function updateConsumedBandwidth(bytes) {
|
|
|
|
pdfJsMetrics.totalBandwidth += bytes - bandwidth
|
|
|
|
bandwidth = bytes
|
|
|
|
}
|
|
|
|
let done
|
|
|
|
const onFirstRenderDone = new Promise(resolve => {
|
|
|
|
done = resolve
|
|
|
|
})
|
|
|
|
|
2022-06-21 08:15:26 -04:00
|
|
|
if (getMeta('ol-trackPdfDownload')) {
|
|
|
|
// Submit latency along with compile context.
|
|
|
|
onFirstRenderDone.then(({ latencyFetch, latencyRender }) => {
|
|
|
|
submitCompileMetrics({
|
|
|
|
latencyFetch,
|
|
|
|
latencyRender,
|
|
|
|
compileTimeClientE2E,
|
|
|
|
stats,
|
|
|
|
timings,
|
|
|
|
})
|
2021-05-21 07:32:07 -04:00
|
|
|
})
|
2022-06-21 08:15:26 -04:00
|
|
|
// Submit bandwidth counter separate from compile context.
|
|
|
|
submitPDFBandwidth({ pdfJsMetrics, serviceWorkerMetrics })
|
|
|
|
}
|
2021-05-21 07:32:07 -04:00
|
|
|
|
|
|
|
return {
|
2022-06-21 08:15:26 -04:00
|
|
|
deliveryLatencies,
|
2021-05-21 07:32:07 -04:00
|
|
|
firstRenderDone,
|
|
|
|
updateConsumedBandwidth,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function submitCompileMetrics(metrics) {
|
2021-06-22 04:12:55 -04:00
|
|
|
const { latencyFetch, latencyRender, compileTimeClientE2E } = metrics
|
2021-06-01 09:52:55 -04:00
|
|
|
const leanMetrics = {
|
2021-06-14 05:05:03 -04:00
|
|
|
version: VERSION,
|
2021-06-01 09:52:55 -04:00
|
|
|
latencyFetch,
|
|
|
|
latencyRender,
|
|
|
|
compileTimeClientE2E,
|
|
|
|
}
|
2021-05-21 07:32:07 -04:00
|
|
|
sl_console.log('/event/compile-metrics', JSON.stringify(metrics))
|
2021-06-22 04:12:55 -04:00
|
|
|
sendMB('compile-metrics-v5', leanMetrics, SAMPLING_RATE)
|
2021-05-21 07:32:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function submitPDFBandwidth(metrics) {
|
2021-06-01 09:52:55 -04:00
|
|
|
const metricsFlat = {}
|
|
|
|
Object.entries(metrics).forEach(([section, items]) => {
|
2021-06-02 07:45:48 -04:00
|
|
|
if (!items) return
|
2021-06-01 09:52:55 -04:00
|
|
|
Object.entries(items).forEach(([key, value]) => {
|
|
|
|
metricsFlat[section + '_' + key] = value
|
|
|
|
})
|
|
|
|
})
|
2021-06-22 04:12:55 -04:00
|
|
|
const leanMetrics = {}
|
|
|
|
Object.entries(metricsFlat).forEach(([metric, value]) => {
|
|
|
|
if (
|
|
|
|
[
|
|
|
|
'serviceWorkerMetrics_id',
|
|
|
|
'serviceWorkerMetrics_cachedBytes',
|
|
|
|
'serviceWorkerMetrics_fetchedBytes',
|
|
|
|
'serviceWorkerMetrics_requestedBytes',
|
|
|
|
'serviceWorkerMetrics_version',
|
|
|
|
'serviceWorkerMetrics_epoch',
|
|
|
|
].includes(metric)
|
|
|
|
) {
|
|
|
|
leanMetrics[metric] = value
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (Object.entries(leanMetrics).length === 0) {
|
|
|
|
return
|
|
|
|
}
|
2021-05-21 07:32:07 -04:00
|
|
|
sl_console.log('/event/pdf-bandwidth', JSON.stringify(metrics))
|
2021-06-22 04:12:55 -04:00
|
|
|
sendMB('pdf-bandwidth-v5', leanMetrics, SAMPLING_RATE)
|
2021-05-21 07:32:07 -04:00
|
|
|
}
|