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.
|
2022-07-06 07:06:53 -04:00
|
|
|
const VERSION = 3
|
2021-06-14 04:44:25 -04:00
|
|
|
|
2022-07-08 04:15:13 -04:00
|
|
|
// editing session id
|
|
|
|
const EDITOR_SESSION_ID = uuid()
|
2021-05-21 07:32:07 -04:00
|
|
|
|
2022-07-06 07:06:53 -04:00
|
|
|
let pdfCachingMetrics
|
|
|
|
|
|
|
|
export function setCachingMetrics(metrics) {
|
|
|
|
pdfCachingMetrics = metrics
|
|
|
|
}
|
|
|
|
|
2021-06-01 09:52:38 -04:00
|
|
|
export function trackPdfDownload(response, compileTimeClientE2E) {
|
2022-07-08 04:15:13 -04:00
|
|
|
const { stats, timings } = response
|
2021-05-21 07:32:07 -04:00
|
|
|
|
|
|
|
const t0 = performance.now()
|
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 })
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-07-08 04:15:13 -04:00
|
|
|
id: EDITOR_SESSION_ID,
|
2022-07-06 07:06:53 -04:00
|
|
|
...(pdfCachingMetrics || {}),
|
2021-06-01 09:52:55 -04:00
|
|
|
}
|
2021-05-21 07:32:07 -04:00
|
|
|
sl_console.log('/event/compile-metrics', JSON.stringify(metrics))
|
2022-07-08 04:15:13 -04:00
|
|
|
sendMB('compile-metrics-v6', leanMetrics)
|
2021-05-21 07:32:07 -04:00
|
|
|
}
|