@@ -247,9 +247,9 @@ search.filterMessagingSearchMessages = async function (data) {
247247
248248search . reindex = async function ( ) {
249249 await db . setObject ( 'nodebb-plugin-dbsearch' , {
250- topicsIndexed : 0 ,
251- postsIndexed : 0 ,
252- messagesIndexed : 0 ,
250+ topicsProgress : 0 ,
251+ postsProgress : 0 ,
252+ messagesProgress : 0 ,
253253 working : 1 ,
254254 } ) ;
255255 await Promise . all ( [
@@ -264,8 +264,11 @@ search.reindex = async function () {
264264
265265async function reIndexTopics ( ) {
266266 await batch . processSortedSet ( 'topics:tid' , async ( tids ) => {
267- const topicData = await topics . getTopicsFields ( tids , [ 'tid' , 'title' , 'uid' , 'cid' , 'deleted' , 'timestamp' ] ) ;
267+ const topicData = await topics . getTopicsFields ( tids , [
268+ 'tid' , 'title' , 'uid' , 'cid' , 'deleted' , 'timestamp' ,
269+ ] ) ;
268270 await topicsSave ( topicData ) ;
271+ await db . incrObjectFieldBy ( 'nodebb-plugin-dbsearch' , 'topicsProgress' , tids . length ) ;
269272 } , {
270273 batch : batchSize ,
271274 } ) ;
@@ -306,7 +309,6 @@ async function topicsSave(topics) {
306309
307310 const result = await plugins . hooks . fire ( 'filter:search.indexTopics' , { data : data , tids : tids , topics : topics } ) ;
308311 await db . searchIndex ( 'topic' , result . data , result . tids ) ;
309- await db . incrObjectFieldBy ( 'nodebb-plugin-dbsearch' , 'topicsIndexed' , result . tids . length ) ;
310312}
311313
312314async function reIndexPosts ( ) {
@@ -323,6 +325,7 @@ async function reIndexPosts() {
323325 } ) ;
324326 postData = postData . filter ( post => tidToTopic [ post . tid ] . deleted !== 1 ) ;
325327 await postsSave ( postData ) ;
328+ await db . incrObjectFieldBy ( 'nodebb-plugin-dbsearch' , 'postsProgress' , pids . length ) ;
326329 } , {
327330 batch : batchSize ,
328331 } ) ;
@@ -363,21 +366,23 @@ async function postsSave(posts) {
363366
364367 const result = await plugins . hooks . fire ( 'filter:search.indexPosts' , { data : data , pids : pids , posts : posts } ) ;
365368 await db . searchIndex ( 'post' , result . data , result . pids ) ;
366- await db . incrObjectFieldBy ( 'nodebb-plugin-dbsearch' , 'postsIndexed' , result . pids . length ) ;
367369}
368370
369371async function reIndexMessages ( ) {
370372 await batch . processSortedSet ( `messages:mid` , async ( mids ) => {
371373 let messageData = await messaging . getMessagesFields ( mids , [ 'mid' , 'content' , 'roomId' , 'fromuid' , 'deleted' , 'system' ] ) ;
372374 messageData = messageData . filter ( p => p && p . deleted !== 1 && p . system !== 1 ) ;
373375 await messagesSave ( messageData ) ;
376+ await db . incrObjectFieldBy ( 'nodebb-plugin-dbsearch' , 'messagesProgress' , mids . length ) ;
374377 } , {
375378 batch : batchSize ,
376379 } ) ;
377380}
378381
379382async function messagesSave ( msgs ) {
380- msgs = msgs . filter ( m => m && utils . isNumber ( m . mid ) && parseInt ( m . deleted , 10 ) !== 1 && parseInt ( m . system , 10 ) !== 1 ) ;
383+ msgs = msgs . filter (
384+ m => m && utils . isNumber ( m . mid ) && parseInt ( m . deleted , 10 ) !== 1 && parseInt ( m . system , 10 ) !== 1
385+ ) ;
381386
382387 let data = msgs . map ( ( msgData ) => {
383388 const indexData = { } ;
@@ -404,19 +409,11 @@ async function messagesSave(msgs) {
404409
405410 const result = await plugins . hooks . fire ( 'filter:search.indexMessages' , { data : data , mids : mids , messages : msgs } ) ;
406411 await searchModule . chat . index ( result . data , result . mids ) ;
407- await db . incrObjectFieldBy ( 'nodebb-plugin-dbsearch' , 'messagesIndexed' , result . mids . length ) ;
408412}
409413
410414async function searchRemove ( key , ids ) {
411415 ids = ids . filter ( id => id && utils . isNumber ( id ) ) ;
412416 await db . searchRemove ( key , ids ) ;
413- if ( key === 'topic' ) {
414- await db . incrObjectFieldBy ( 'nodebb-plugin-dbsearch' , 'topicsIndexed' , - ids . length ) ;
415- } else if ( key === 'post' ) {
416- await db . incrObjectFieldBy ( 'nodebb-plugin-dbsearch' , 'postsIndexed' , - ids . length ) ;
417- } else if ( key === 'chat' ) {
418- await db . incrObjectFieldBy ( 'nodebb-plugin-dbsearch' , 'messagesIndexed' , - ids . length ) ;
419- }
420417}
421418
422419async function reIndexTids ( tids ) {
@@ -468,6 +465,14 @@ async function reIndexPids(pids, topic) {
468465
469466async function renderAdmin ( req , res ) {
470467 const results = await getGlobalAndPluginData ( ) ;
468+ const [ topicsIndexed , postsIndexed , messagesIndexed ] = await Promise . all ( [
469+ searchModule . getIndexedTopicCount ( ) ,
470+ searchModule . getIndexedPostCount ( ) ,
471+ searchModule . getIndexedChatMessageCount ( ) ,
472+ ] ) ;
473+ results . plugin . topicsIndexed = parseInt ( topicsIndexed , 10 ) || 0 ;
474+ results . plugin . postsIndexed = parseInt ( postsIndexed , 10 ) || 0 ;
475+ results . plugin . messagesIndexed = parseInt ( messagesIndexed , 10 ) || 0 ;
471476 results . plugin . progressData = await getProgress ( ) ;
472477 results . plugin . title = 'DB Search' ;
473478 res . render ( 'admin/plugins/dbsearch' , results . plugin ) ;
@@ -498,14 +503,12 @@ socketAdmin.plugins.dbsearch.checkProgress = async function () {
498503
499504async function getPluginData ( ) {
500505 const data = await db . getObject ( 'nodebb-plugin-dbsearch' ) || { } ;
501- data . topicsIndexed = parseInt ( data . topicsIndexed , 10 ) || 0 ;
502- data . postsIndexed = parseInt ( data . postsIndexed , 10 ) || 0 ;
503- data . messagesIndexed = parseInt ( data . messagesIndexed , 10 ) || 0 ;
506+
504507 data . excludeCategories = data . excludeCategories || '[]' ;
505508 data . postLimit = data . postLimit || defaultPostLimit ;
506509 data . topicLimit = data . topicLimit || defaultTopicLimit ;
507510 data . indexLanguage = data . indexLanguage || 'en' ;
508- data . working = data . working || 0 ;
511+ data . working = parseInt ( data . working , 10 ) || 0 ;
509512
510513 try {
511514 data . excludeCategories = JSON . parse ( data . excludeCategories ) ;
@@ -537,9 +540,6 @@ async function getGlobalAndPluginData() {
537540 plugin . messageCount = parseInt ( global . messageCount , 10 ) ;
538541 plugin . topicLimit = plugin . topicLimit || defaultTopicLimit ;
539542 plugin . postLimit = plugin . postLimit || defaultPostLimit ;
540- plugin . topicsIndexed = plugin . topicsIndexed > plugin . topicCount ? plugin . topicCount : plugin . topicsIndexed ;
541- plugin . postsIndexed = plugin . postsIndexed > plugin . postCount ? plugin . postCount : plugin . postsIndexed ;
542- plugin . messagesIndexed = plugin . messagesIndexed > plugin . messageCount ? plugin . messageCount : plugin . messagesIndexed ;
543543 plugin . languageSupported = languageSupported ;
544544 plugin . languages = languages ;
545545 plugin . indexLanguage = plugin . indexLanguage || 'en' ;
@@ -557,21 +557,18 @@ async function getGlobalAndPluginData() {
557557async function getProgress ( ) {
558558 const [ global , pluginData ] = await Promise . all ( [
559559 db . getObjectFields ( 'global' , [ 'topicCount' , 'postCount' , 'messageCount' ] ) ,
560- getPluginData ( ) ,
560+ db . getObjectFields ( 'nodebb-plugin-dbsearch' , [ 'topicsProgress' , 'postsProgress' , 'messagesProgress' , 'working' ] ) ,
561561 ] ) ;
562562 const topicCount = parseInt ( global . topicCount , 10 ) ;
563563 const postCount = parseInt ( global . postCount , 10 ) ;
564564 const messageCount = parseInt ( global . messageCount , 10 ) ;
565- const topicsPercent = topicCount ? ( pluginData . topicsIndexed / topicCount ) * 100 : 0 ;
566- const postsPercent = postCount ? ( pluginData . postsIndexed / postCount ) * 100 : 0 ;
567- const messagesPercent = messageCount ? ( pluginData . messagesIndexed / messageCount ) * 100 : 0 ;
565+ const topicsPercent = topicCount ? ( pluginData . topicsProgress / topicCount ) * 100 : 0 ;
566+ const postsPercent = postCount ? ( pluginData . postsProgress / postCount ) * 100 : 0 ;
567+ const messagesPercent = messageCount ? ( pluginData . messagesProgress / messageCount ) * 100 : 0 ;
568568 return {
569569 topicsPercent : Math . max ( 0 , Math . min ( 100 , topicsPercent . toFixed ( 2 ) ) ) ,
570570 postsPercent : Math . max ( 0 , Math . min ( 100 , postsPercent . toFixed ( 2 ) ) ) ,
571571 messagesPercent : Math . max ( 0 , Math . min ( 100 , messagesPercent . toFixed ( 2 ) ) ) ,
572- topicsIndexed : topicsPercent >= 100 ? topicCount : Math . max ( 0 , pluginData . topicsIndexed ) ,
573- postsIndexed : postsPercent >= 100 ? postCount : Math . max ( 0 , pluginData . postsIndexed ) ,
574- messagesIndexed : messagesPercent >= 100 ? messageCount : Math . max ( 0 , pluginData . messagesIndexed ) ,
575572 working : pluginData . working ,
576573 } ;
577574}
@@ -599,26 +596,27 @@ socketAdmin.plugins.dbsearch.clearIndex = async function () {
599596
600597async function clearIndex ( ) {
601598 await db . setObject ( 'nodebb-plugin-dbsearch' , {
599+ postsProgress : 0 ,
600+ topicsProgress : 0 ,
601+ messagesProgress : 0 ,
602602 working : 1 ,
603603 } ) ;
604604
605605 await Promise . all ( [
606- clearSet ( 'topics:tid' , 'topic' ) ,
607- clearSet ( 'posts:pid' , 'post' ) ,
608- clearSet ( 'messages:mid' , 'chat' ) ,
606+ clearSet ( 'topics:tid' , 'topic' , 'topicsProgress' ) ,
607+ clearSet ( 'posts:pid' , 'post' , 'postsProgress' ) ,
608+ clearSet ( 'messages:mid' , 'chat' , 'messagesProgress' ) ,
609609 ] ) ;
610610
611611 await db . setObject ( 'nodebb-plugin-dbsearch' , {
612- postsIndexed : 0 ,
613- topicsIndexed : 0 ,
614- messagesIndexed : 0 ,
615612 working : 0 ,
616613 } ) ;
617614}
618615
619- async function clearSet ( set , key ) {
616+ async function clearSet ( set , key , progressKey ) {
620617 await batch . processSortedSet ( set , async ( ids ) => {
621618 await searchRemove ( key , ids ) ;
619+ await db . incrObjectFieldBy ( 'nodebb-plugin-dbsearch' , progressKey , ids . length ) ;
622620 } , {
623621 batch : batchSize ,
624622 } ) ;
0 commit comments