(name: any, src: Db, report: Report)
| 395 | } |
| 396 | |
| 397 | async function task(name: any, src: Db, report: Report) { |
| 398 | await report({ progress: 1, message: `${name}` }); |
| 399 | let lastProgress = -1; |
| 400 | for (let i = 0; ; i++) { |
| 401 | const docs = await cursor[name](src).skip(i * 50).limit(50).toArray(); |
| 402 | if (!docs.length) break; |
| 403 | const res = []; |
| 404 | for (const doc of docs) { |
| 405 | let d: any = {}; |
| 406 | if (typeof tasks[name].call === 'function') { |
| 407 | d = await tasks[name](doc); |
| 408 | } else { |
| 409 | const mapper = tasks[name]; |
| 410 | for (const key in doc) { |
| 411 | if (typeof mapper[key] === 'string') { |
| 412 | d[mapper[key]] = doc[key]; |
| 413 | } else if (mapper[key] === null) { |
| 414 | // Ignore this key |
| 415 | } else if (typeof mapper[key] === 'object') { |
| 416 | d[mapper[key].field] = mapper[key].processer(doc[key], doc); |
| 417 | } else { |
| 418 | await report({ message: `Unknown key ${key} in collection ${name}` }); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | if (d) { |
| 423 | if (d.rule) d.rated = true; // Hack contest rated field |
| 424 | const docWithoutId = {}; |
| 425 | const docWithoutDid = {}; |
| 426 | for (const key in d) { |
| 427 | if (key !== '_id') { |
| 428 | if (key !== 'domainId' && key !== 'docId' && key !== 'docType' && key !== 'uid') { |
| 429 | docWithoutDid[key] = d[key]; |
| 430 | } |
| 431 | docWithoutId[key] = d[key]; |
| 432 | } |
| 433 | } |
| 434 | if (d.domainId && d.docId && d.docType) { |
| 435 | const query: any = { domainId: d.domainId, docId: d.docId, docType: d.docType }; |
| 436 | if (d.uid) query.uid = d.uid; |
| 437 | res.push((async () => { |
| 438 | const data = await dst.collection(name).findOne(query); |
| 439 | if (data) { |
| 440 | await dst.collection(name).updateOne(query, { $set: docWithoutDid }); |
| 441 | } else if (d._id) { |
| 442 | const dat = await dst.collection(name).findOne({ _id: d._id }); |
| 443 | if (dat) { |
| 444 | await dst.collection(name).updateOne({ |
| 445 | _id: d._id, |
| 446 | }, { $set: docWithoutId }); |
| 447 | } else { |
| 448 | await dst.collection(name).insertOne(d); |
| 449 | } |
| 450 | } else { |
| 451 | await dst.collection(name).insertOne(d); |
| 452 | } |
| 453 | })()); |
| 454 | } else if (d._id) { |
no test coverage detected