Merge pull request #24256 from overleaf/bg-backup-fix-sample

fix backup sample script

GitOrigin-RevId: 4bf4c15324d9b3b7ebd809f7b212e52d75ab216d
This commit is contained in:
Brian Gough 2025-03-12 14:06:43 +00:00 committed by Copybot
parent 28ff69b51b
commit 3b93efdf5c

View file

@ -32,34 +32,18 @@ async function takeSample(sampleSize) {
[
{ $sample: { size: sampleSize } },
{
$project: {
_id: 0,
hasBackup: {
$ifNull: ['$overleaf.backup.lastBackedUpVersion', false],
},
},
$match: { 'overleaf.backup.lastBackedUpVersion': { $exists: true } },
},
{
$group: {
_id: null,
totalSampled: { $sum: 1 },
backedUp: {
$sum: {
$cond: ['$hasBackup', 1, 0],
},
},
},
$count: 'total',
},
],
{ readPreference: READ_PREFERENCE_SECONDARY }
)
.toArray()
if (results.length === 0) {
return { totalSampled: 0, backedUp: 0 }
}
return results[0]
const count = results[0]?.total || 0
return { totalSampled: sampleSize, backedUp: count }
}
function calculateStatistics(
@ -67,7 +51,7 @@ function calculateStatistics(
cumulativeBackedUp,
totalPopulation
) {
const proportion = cumulativeBackedUp / cumulativeSampled
const proportion = Math.max(1, cumulativeBackedUp) / cumulativeSampled
// Standard error with finite population correction
const fpc = Math.sqrt(