@@ -7,6 +7,27 @@ import db from '../service/db';
77const sumStatus = ( status ) => ( { $sum : { $cond : [ { $eq : [ '$status' , status ] } , 1 , 0 ] } } ) ;
88
99export async function udoc ( report ) {
10+ const userStats = new Map < string , { nLiked ?: number , nAccept ?: number , nSubmit ?: number } > ( ) ;
11+
12+ report ( { message : 'Udoc nLiked' } ) ;
13+ const likedPipeline = [
14+ {
15+ $match : {
16+ docType : document . TYPE_PROBLEM_SOLUTION ,
17+ vote : { $gt : 0 } ,
18+ } ,
19+ } ,
20+ {
21+ $group : {
22+ _id : { domainId : '$domainId' , uid : '$owner' } ,
23+ nLiked : { $sum : '$vote' } ,
24+ } ,
25+ } ,
26+ ] ;
27+ for await ( const adoc of document . coll . aggregate < any > ( likedPipeline , { allowDiskUse : true } ) ) {
28+ userStats . set ( `${ adoc . _id . domainId } /${ adoc . _id . uid } ` , { nLiked : adoc . nLiked } ) ;
29+ }
30+
1031 report ( { message : 'Udoc' } ) ;
1132 const pipeline = [
1233 {
@@ -31,20 +52,26 @@ export async function udoc(report) {
3152 } ,
3253 } ,
3354 ] ;
55+ for await ( const adoc of db . collection ( 'record' ) . aggregate < any > ( pipeline , { allowDiskUse : true } ) ) {
56+ const key = `${ adoc . _id . domainId } /${ adoc . _id . uid } ` ;
57+ const stat = userStats . get ( key ) || { } ;
58+ stat . nSubmit = adoc . nSubmit ;
59+ stat . nAccept = adoc . nAccept ;
60+ userStats . set ( key , stat ) ;
61+ }
62+
3463 let bulk = db . collection ( 'domain.user' ) . initializeUnorderedBulkOp ( ) ;
35- const cursor = db . collection ( 'record' ) . aggregate < any > ( pipeline , { allowDiskUse : true } ) ;
36- for await ( const adoc of cursor ) {
37- bulk . find ( {
38- domainId : adoc . _id . domainId ,
39- uid : adoc . _id . uid ,
40- } ) . updateOne ( {
64+ for ( const [ key , stat ] of userStats ) {
65+ const [ domainId , uid ] = key . split ( '/' ) ;
66+ bulk . find ( { domainId, uid : + uid } ) . updateOne ( {
4167 $set : {
42- nSubmit : adoc . nSubmit ,
43- nAccept : adoc . nAccept ,
68+ nSubmit : stat . nSubmit || 0 ,
69+ nAccept : stat . nAccept || 0 ,
70+ nLiked : stat . nLiked || 0 ,
4471 } ,
4572 } ) ;
4673 if ( bulk . batches . length > 100 ) {
47- await bulk . execute ( ) ;
74+ await bulk . execute ( ) ; // eslint-disable-line no-await-in-loop
4875 bulk = db . collection ( 'domain.user' ) . initializeUnorderedBulkOp ( ) ;
4976 }
5077 }
0 commit comments