|
@@ -13,19 +13,19 @@
|
|
|
*/
|
|
*/
|
|
|
import logger from '@overleaf/logger'
|
|
import logger from '@overleaf/logger'
|
|
|
import minimist from 'minimist'
|
|
import minimist from 'minimist'
|
|
|
|
|
+import { z } from 'zod'
|
|
|
import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
|
|
import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
|
|
|
import { db } from '../../app/src/infrastructure/mongodb.js'
|
|
import { db } from '../../app/src/infrastructure/mongodb.js'
|
|
|
import AccountMappingHelper from '../../app/src/Features/Analytics/AccountMappingHelper.js'
|
|
import AccountMappingHelper from '../../app/src/Features/Analytics/AccountMappingHelper.js'
|
|
|
import { registerAccountMapping } from '../../app/src/Features/Analytics/AnalyticsManager.js'
|
|
import { registerAccountMapping } from '../../app/src/Features/Analytics/AnalyticsManager.js'
|
|
|
import { triggerGracefulShutdown } from '../../app/src/infrastructure/GracefulShutdown.js'
|
|
import { triggerGracefulShutdown } from '../../app/src/infrastructure/GracefulShutdown.js'
|
|
|
-import Validation from '../../app/src/infrastructure/Validation.js'
|
|
|
|
|
import { scriptRunner } from '../lib/ScriptRunner.mjs'
|
|
import { scriptRunner } from '../lib/ScriptRunner.mjs'
|
|
|
|
|
|
|
|
-const paramsSchema = Validation.Joi.object({
|
|
|
|
|
- endDate: Validation.Joi.string().isoDate(),
|
|
|
|
|
- commit: Validation.Joi.boolean().default(false),
|
|
|
|
|
- verbose: Validation.Joi.boolean().default(false),
|
|
|
|
|
-}).unknown(true)
|
|
|
|
|
|
|
+const paramsSchema = z.object({
|
|
|
|
|
+ endDate: z.string().datetime(),
|
|
|
|
|
+ commit: z.boolean().default(false),
|
|
|
|
|
+ verbose: z.boolean().default(false),
|
|
|
|
|
+})
|
|
|
|
|
|
|
|
let mapped = 0
|
|
let mapped = 0
|
|
|
let subscriptionCount = 0
|
|
let subscriptionCount = 0
|
|
@@ -54,7 +54,7 @@ function registerMapping(subscription) {
|
|
|
},
|
|
},
|
|
|
`processing subscription ${subscription._id}`
|
|
`processing subscription ${subscription._id}`
|
|
|
)
|
|
)
|
|
|
- if (commit) {
|
|
|
|
|
|
|
+ if (opts.commit) {
|
|
|
registerAccountMapping(mapping)
|
|
registerAccountMapping(mapping)
|
|
|
mapped++
|
|
mapped++
|
|
|
}
|
|
}
|
|
@@ -63,8 +63,8 @@ function registerMapping(subscription) {
|
|
|
async function main(trackProgress) {
|
|
async function main(trackProgress) {
|
|
|
const additionalBatchedUpdateOptions = {}
|
|
const additionalBatchedUpdateOptions = {}
|
|
|
|
|
|
|
|
- if (endDate) {
|
|
|
|
|
- additionalBatchedUpdateOptions.BATCH_RANGE_END = endDate
|
|
|
|
|
|
|
+ if (opts.endDate) {
|
|
|
|
|
+ additionalBatchedUpdateOptions.BATCH_RANGE_END = opts.endDate
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
await batchedUpdate(
|
|
await batchedUpdate(
|
|
@@ -82,35 +82,32 @@ async function main(trackProgress) {
|
|
|
readPreference: 'secondaryPreferred',
|
|
readPreference: 'secondaryPreferred',
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
- verboseLogging: verbose,
|
|
|
|
|
|
|
+ verboseLogging: opts.verbose,
|
|
|
...additionalBatchedUpdateOptions,
|
|
...additionalBatchedUpdateOptions,
|
|
|
trackProgress,
|
|
trackProgress,
|
|
|
}
|
|
}
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
logger.debug({}, `${subscriptionCount} subscriptions processed`)
|
|
logger.debug({}, `${subscriptionCount} subscriptions processed`)
|
|
|
- if (commit) {
|
|
|
|
|
|
|
+ if (opts.commit) {
|
|
|
logger.debug({}, `${mapped} mappings registered`)
|
|
logger.debug({}, `${mapped} mappings registered`)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const {
|
|
|
|
|
- error,
|
|
|
|
|
- value: { commit, endDate, verbose },
|
|
|
|
|
-} = paramsSchema.validate(
|
|
|
|
|
|
|
+const { error, data: opts } = paramsSchema.safeParse(
|
|
|
minimist(process.argv.slice(2), {
|
|
minimist(process.argv.slice(2), {
|
|
|
boolean: ['commit', 'verbose'],
|
|
boolean: ['commit', 'verbose'],
|
|
|
string: ['endDate'],
|
|
string: ['endDate'],
|
|
|
})
|
|
})
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-logger.logger.level(verbose ? 'debug' : 'info')
|
|
|
|
|
|
|
+logger.logger.level(opts.verbose ? 'debug' : 'info')
|
|
|
|
|
|
|
|
if (error) {
|
|
if (error) {
|
|
|
logger.error({ error }, 'error with parameters')
|
|
logger.error({ error }, 'error with parameters')
|
|
|
triggerGracefulShutdown(done => done(1))
|
|
triggerGracefulShutdown(done => done(1))
|
|
|
} else {
|
|
} else {
|
|
|
- logger.info({ verbose, commit, endDate }, commit ? 'COMMITTING' : 'DRY RUN')
|
|
|
|
|
|
|
+ logger.info(opts, opts.commit ? 'COMMITTING' : 'DRY RUN')
|
|
|
await scriptRunner(main)
|
|
await scriptRunner(main)
|
|
|
|
|
|
|
|
triggerGracefulShutdown({
|
|
triggerGracefulShutdown({
|