Files
overleaf-cep/services/clsi/app/js/StatsManager.js
T
Brian Gough f67d5fe006 Merge pull request #28880 from overleaf/bg-add-time-option-to-clsi
add latexmk `-time` option to clsi and record performance logs

GitOrigin-RevId: 467473859359913da73f83e10b63b45603ea175c
2025-10-09 08:06:12 +00:00

25 lines
960 B
JavaScript

const crypto = require('node:crypto')
/**
* Consistently sample a keyspace with a given sample percentage.
* The same key will always produce a consistent percentile value that
* can be compared against the sample percentage.
* Example: if key is the userId and the samplePercentage is 10, then
* we see all the activity for the 10% of users who are selected.
*
* @param {string} key - The unique identifier to be hashed and checked.
* @param {number} samplePercentage - The percentage (0-100) of keys that should return true.
* @returns {boolean} - True if the key is within the sample, false otherwise.
*/
function sampleByHash(key, samplePercentage) {
if (samplePercentage <= 0) {
return false
}
const hash = crypto.createHash('md5').update(key).digest()
const hashValue = hash.readUInt32BE(0)
const percentile = Math.floor((hashValue / 0xffffffff) * 100)
return percentile < samplePercentage
}
module.exports = { sampleByHash }