check_docs.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // @ts-check
  2. import minimist from 'minimist'
  3. import PQueue from 'p-queue'
  4. import {
  5. db,
  6. ObjectId,
  7. READ_PREFERENCE_SECONDARY,
  8. } from '../app/src/infrastructure/mongodb.js'
  9. import DocstoreManager from '../app/src/Features/Docstore/DocstoreManager.js'
  10. import { NotFoundError } from '../app/src/Features/Errors/Errors.js'
  11. const OPTS = parseArgs()
  12. function parseArgs() {
  13. const args = minimist(process.argv.slice(2), {
  14. string: ['min-project-id', 'max-project-id', 'project-modified-since'],
  15. boolean: ['help', 'dangling-comments', 'tracked-changes'],
  16. })
  17. if (args.help) {
  18. usage()
  19. process.exit(0)
  20. }
  21. const danglingComments = Boolean(args['dangling-comments'])
  22. const trackedChanges = Boolean(args['tracked-changes'])
  23. if (!danglingComments && !trackedChanges) {
  24. console.log(
  25. 'At least one of --dangling-comments or --tracked-changes must be enabled'
  26. )
  27. process.exit(1)
  28. }
  29. return {
  30. minProjectId: args['min-project-id'] ?? null,
  31. maxProjectId: args['max-project-id'] ?? null,
  32. projectModifiedSince: args['project-modified-since']
  33. ? new Date(args['project-modified-since'])
  34. : null,
  35. danglingComments,
  36. trackedChanges,
  37. concurrency: parseInt(args.concurrency ?? '1', 10),
  38. }
  39. }
  40. function usage() {
  41. console.log(`Usage: find_dangling_comments.mjs [OPTS]
  42. Options:
  43. --min-project-id Start scanning at this project id
  44. --max-project-id Stop scanning at this project id
  45. --project-modified-since Only consider projects that were modified after the given date
  46. Example: 2020-01-01
  47. --dangling-comments Report projects with dangling comments
  48. --tracked-changes Report projects with tracked changes
  49. --concurrency How many projects can be processed in parallel
  50. `)
  51. }
  52. async function main() {
  53. const queue = new PQueue({ concurrency: OPTS.concurrency })
  54. let projectsProcessed = 0
  55. let danglingCommentsFound = 0
  56. let trackedChangesFound = 0
  57. for await (const projectId of getProjectIds()) {
  58. await queue.onEmpty()
  59. queue.add(async () => {
  60. const docs = await getDocs(projectId)
  61. if (OPTS.danglingComments) {
  62. const danglingThreadIds = await findDanglingThreadIds(projectId, docs)
  63. if (danglingThreadIds.length > 0) {
  64. console.log(
  65. `Project ${projectId} has dangling threads: ${danglingThreadIds.join(', ')}`
  66. )
  67. danglingCommentsFound += 1
  68. }
  69. }
  70. if (OPTS.trackedChanges) {
  71. if (docsHaveTrackedChanges(docs)) {
  72. console.log(`Project ${projectId} has tracked changes`)
  73. trackedChangesFound += 1
  74. }
  75. }
  76. projectsProcessed += 1
  77. if (projectsProcessed % 100000 === 0) {
  78. console.log(
  79. `${projectsProcessed} projects processed - Last project: ${projectId}`
  80. )
  81. }
  82. })
  83. }
  84. await queue.onIdle()
  85. if (OPTS.danglingComments) {
  86. console.log(
  87. `${danglingCommentsFound} projects with dangling comments found`
  88. )
  89. }
  90. if (OPTS.trackedChanges) {
  91. console.log(`${trackedChangesFound} projects with tracked changes found`)
  92. }
  93. }
  94. function getProjectIds() {
  95. const clauses = []
  96. if (OPTS.minProjectId != null) {
  97. clauses.push({ _id: { $gte: new ObjectId(OPTS.minProjectId) } })
  98. }
  99. if (OPTS.maxProjectId != null) {
  100. clauses.push({ _id: { $lte: new ObjectId(OPTS.maxProjectId) } })
  101. }
  102. if (OPTS.projectModifiedSince) {
  103. clauses.push({ lastUpdated: { $gte: OPTS.projectModifiedSince } })
  104. }
  105. const query = clauses.length > 0 ? { $and: clauses } : {}
  106. return db.projects
  107. .find(query, {
  108. projection: { _id: 1 },
  109. readPreference: READ_PREFERENCE_SECONDARY,
  110. sort: { _id: 1 },
  111. })
  112. .map(x => x._id.toString())
  113. }
  114. async function getDocs(projectId) {
  115. const mongoDocs = db.docs.find(
  116. {
  117. project_id: new ObjectId(projectId),
  118. deleted: { $ne: true },
  119. },
  120. {
  121. projection: { ranges: 1, inS3: 1 },
  122. readPreference: READ_PREFERENCE_SECONDARY,
  123. }
  124. )
  125. const docs = []
  126. for await (const mongoDoc of mongoDocs) {
  127. if (mongoDoc.inS3) {
  128. try {
  129. const archivedDoc = await DocstoreManager.promises.getDoc(
  130. projectId,
  131. mongoDoc._id,
  132. { peek: true }
  133. )
  134. docs.push({
  135. id: mongoDoc._id.toString(),
  136. ranges: archivedDoc.ranges,
  137. })
  138. } catch (err) {
  139. if (err instanceof NotFoundError) {
  140. console.warn(`Doc ${mongoDoc._id} in project ${projectId} not found`)
  141. } else {
  142. throw err
  143. }
  144. }
  145. } else {
  146. docs.push({
  147. id: mongoDoc._id.toString(),
  148. ranges: mongoDoc.ranges,
  149. })
  150. }
  151. }
  152. return docs
  153. }
  154. async function findDanglingThreadIds(projectId, docs) {
  155. const threadIds = new Set()
  156. for (const doc of docs) {
  157. const comments = doc.ranges?.comments ?? []
  158. for (const comment of comments) {
  159. threadIds.add(comment.op.t.toString())
  160. }
  161. }
  162. if (threadIds.size === 0) {
  163. return []
  164. }
  165. const rooms = await db.rooms.find(
  166. { project_id: new ObjectId(projectId), thread_id: { $exists: true } },
  167. { readPreference: READ_PREFERENCE_SECONDARY }
  168. )
  169. for await (const room of rooms) {
  170. threadIds.delete(room.thread_id.toString())
  171. if (threadIds.size === 0) {
  172. break
  173. }
  174. }
  175. return Array.from(threadIds)
  176. }
  177. function docsHaveTrackedChanges(docs) {
  178. for (const doc of docs) {
  179. const changes = doc.ranges?.changes ?? []
  180. if (changes.length > 0) {
  181. return true
  182. }
  183. }
  184. return false
  185. }
  186. try {
  187. await main()
  188. process.exit(0)
  189. } catch (err) {
  190. console.error(err)
  191. process.exit(1)
  192. }