From 3b93efdf5c48c861f239838c07479ec96ab3e0c9 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 12 Mar 2025 14:06:43 +0000 Subject: [PATCH] Merge pull request #24256 from overleaf/bg-backup-fix-sample fix backup sample script GitOrigin-RevId: 4bf4c15324d9b3b7ebd809f7b212e52d75ab216d --- .../storage/scripts/backup_sample.mjs | 26 ++++--------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/services/history-v1/storage/scripts/backup_sample.mjs b/services/history-v1/storage/scripts/backup_sample.mjs index 06a2e4a375..35ee1e93f8 100644 --- a/services/history-v1/storage/scripts/backup_sample.mjs +++ b/services/history-v1/storage/scripts/backup_sample.mjs @@ -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(