2021-04-15 05:04:30 -04:00
|
|
|
const { promisify } = require('util')
|
2024-08-02 12:27:31 -04:00
|
|
|
const { ObjectId } = require('mongodb-legacy')
|
2023-04-18 07:15:34 -04:00
|
|
|
const {
|
|
|
|
db,
|
|
|
|
waitForDb,
|
|
|
|
READ_PREFERENCE_SECONDARY,
|
|
|
|
} = require('../app/src/infrastructure/mongodb')
|
2021-04-15 05:04:30 -04:00
|
|
|
const sleep = promisify(setTimeout)
|
2021-08-04 05:34:41 -04:00
|
|
|
const _ = require('lodash')
|
2021-04-15 05:04:30 -04:00
|
|
|
|
|
|
|
const NOW_IN_S = Date.now() / 1000
|
2021-04-15 10:23:49 -04:00
|
|
|
const ONE_WEEK_IN_S = 60 * 60 * 24 * 7
|
2021-04-15 05:04:30 -04:00
|
|
|
const TEN_SECONDS = 10 * 1000
|
|
|
|
|
|
|
|
const DUMMY_NAME = 'unknown.tex'
|
|
|
|
const DUMMY_TIME = new Date('2021-04-12T00:00:00.000Z')
|
|
|
|
|
2021-04-20 08:21:32 -04:00
|
|
|
const LRUCache = require('lru-cache')
|
2021-08-04 05:34:41 -04:00
|
|
|
let deletedProjectsCache = null
|
2021-04-20 08:21:32 -04:00
|
|
|
|
2021-04-15 05:04:30 -04:00
|
|
|
function getSecondsFromObjectId(id) {
|
|
|
|
return id.getTimestamp().getTime() / 1000
|
|
|
|
}
|
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
async function main(options) {
|
|
|
|
if (!options) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
_.defaults(options, {
|
|
|
|
dryRun: process.env.DRY_RUN === 'true',
|
|
|
|
cacheSize: parseInt(process.env.CACHE_SIZE, 10) || 100,
|
2023-12-18 05:54:01 -05:00
|
|
|
firstProjectId: new ObjectId(process.env.FIRST_PROJECT_ID),
|
2021-08-04 05:34:41 -04:00
|
|
|
incrementByS: parseInt(process.env.INCREMENT_BY_S, 10) || ONE_WEEK_IN_S,
|
|
|
|
batchSize: parseInt(process.env.BATCH_SIZE, 10) || 1000,
|
|
|
|
stopAtS: parseInt(process.env.STOP_AT_S, 10) || NOW_IN_S,
|
|
|
|
letUserDoubleCheckInputsFor:
|
|
|
|
parseInt(process.env.LET_USER_DOUBLE_CHECK_INPUTS_FOR, 10) || TEN_SECONDS,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!options.firstProjectId) {
|
|
|
|
console.error('Set FIRST_PROJECT_ID and re-run.')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
deletedProjectsCache = new LRUCache({
|
|
|
|
max: options.cacheSize,
|
|
|
|
})
|
|
|
|
|
|
|
|
await letUserDoubleCheckInputs(options)
|
2021-04-15 05:04:30 -04:00
|
|
|
await waitForDb()
|
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
let startId = options.firstProjectId
|
2021-04-15 05:04:30 -04:00
|
|
|
|
|
|
|
let nProcessed = 0
|
2021-08-04 05:34:41 -04:00
|
|
|
while (getSecondsFromObjectId(startId) <= options.stopAtS) {
|
|
|
|
const end = getSecondsFromObjectId(startId) + options.incrementByS
|
2021-04-15 10:23:49 -04:00
|
|
|
let endId = ObjectId.createFromTime(end)
|
2021-04-15 05:04:30 -04:00
|
|
|
const query = {
|
|
|
|
project_id: {
|
|
|
|
// include edge
|
2021-04-20 08:21:32 -04:00
|
|
|
$gte: startId,
|
|
|
|
// exclude edge
|
2021-04-27 03:52:58 -04:00
|
|
|
$lt: endId,
|
2021-04-15 05:04:30 -04:00
|
|
|
},
|
|
|
|
deleted: true,
|
|
|
|
name: {
|
2021-04-27 03:52:58 -04:00
|
|
|
$exists: false,
|
|
|
|
},
|
2021-04-15 05:04:30 -04:00
|
|
|
}
|
|
|
|
const docs = await db.docs
|
2023-04-18 07:15:34 -04:00
|
|
|
.find(query, { readPreference: READ_PREFERENCE_SECONDARY })
|
2021-04-15 05:04:30 -04:00
|
|
|
.project({ _id: 1, project_id: 1 })
|
2021-08-04 05:34:41 -04:00
|
|
|
.limit(options.batchSize)
|
2021-04-15 05:04:30 -04:00
|
|
|
.toArray()
|
|
|
|
|
|
|
|
if (docs.length) {
|
|
|
|
const docIds = docs.map(doc => doc._id)
|
|
|
|
console.log('Back filling dummy meta data for', JSON.stringify(docIds))
|
2021-08-04 05:34:41 -04:00
|
|
|
await processBatch(docs, options)
|
2021-04-15 05:04:30 -04:00
|
|
|
nProcessed += docIds.length
|
2021-04-15 10:23:49 -04:00
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
if (docs.length === options.batchSize) {
|
2021-04-15 10:23:49 -04:00
|
|
|
endId = docs[docs.length - 1].project_id
|
|
|
|
}
|
2021-04-15 05:04:30 -04:00
|
|
|
}
|
|
|
|
console.error('Processed %d until %s', nProcessed, endId)
|
|
|
|
|
2021-04-20 08:21:32 -04:00
|
|
|
startId = endId
|
2021-04-15 05:04:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:21:32 -04:00
|
|
|
async function getDeletedProject(projectId) {
|
|
|
|
const cacheKey = projectId.toString()
|
|
|
|
if (deletedProjectsCache.has(cacheKey)) {
|
|
|
|
return deletedProjectsCache.get(cacheKey)
|
|
|
|
}
|
|
|
|
const deletedProject = await db.deletedProjects.findOne(
|
|
|
|
{ 'deleterData.deletedProjectId': projectId },
|
|
|
|
{
|
|
|
|
projection: {
|
|
|
|
_id: 1,
|
2021-04-27 03:52:58 -04:00
|
|
|
'project.deletedDocs': 1,
|
|
|
|
},
|
2021-04-20 08:21:32 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
deletedProjectsCache.set(cacheKey, deletedProject)
|
|
|
|
return deletedProject
|
|
|
|
}
|
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
async function processBatch(docs, options) {
|
2021-04-15 05:04:30 -04:00
|
|
|
for (const doc of docs) {
|
|
|
|
const { _id: docId, project_id: projectId } = doc
|
2021-04-20 08:21:32 -04:00
|
|
|
const deletedProject = await getDeletedProject(projectId)
|
2021-04-15 05:04:30 -04:00
|
|
|
let name = DUMMY_NAME
|
|
|
|
let deletedAt = DUMMY_TIME
|
|
|
|
if (deletedProject) {
|
|
|
|
const project = deletedProject.project
|
|
|
|
if (project) {
|
|
|
|
const deletedDoc =
|
|
|
|
project.deletedDocs &&
|
|
|
|
project.deletedDocs.find(deletedDoc => docId.equals(deletedDoc._id))
|
|
|
|
if (deletedDoc) {
|
|
|
|
console.log('Found deletedDoc for %s', docId)
|
|
|
|
name = deletedDoc.name
|
|
|
|
deletedAt = deletedDoc.deletedAt
|
|
|
|
} else {
|
|
|
|
console.log('Missing deletedDoc for %s', docId)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('Orphaned deleted doc %s (failed hard deletion)', docId)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('Orphaned deleted doc %s (no deletedProjects entry)', docId)
|
|
|
|
}
|
2021-08-04 05:34:41 -04:00
|
|
|
if (options.dryRun) continue
|
2021-04-15 05:04:30 -04:00
|
|
|
await db.docs.updateOne({ _id: docId }, { $set: { name, deletedAt } })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
async function letUserDoubleCheckInputs(options) {
|
|
|
|
console.error('Options:', JSON.stringify(options, null, 2))
|
2021-04-15 05:04:30 -04:00
|
|
|
console.error(
|
|
|
|
'Waiting for you to double check inputs for',
|
2021-08-04 05:34:41 -04:00
|
|
|
options.letUserDoubleCheckInputsFor,
|
2021-04-15 05:04:30 -04:00
|
|
|
'ms'
|
|
|
|
)
|
2021-08-04 05:34:41 -04:00
|
|
|
await sleep(options.letUserDoubleCheckInputsFor)
|
2021-04-15 05:04:30 -04:00
|
|
|
}
|
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
module.exports = main
|
|
|
|
|
|
|
|
if (require.main === module) {
|
|
|
|
main()
|
|
|
|
.then(() => {
|
|
|
|
console.error('Done.')
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
}
|