@@ -6,19 +6,19 @@ const logout = require('./logout');
66const CONSTANTS = require ( '../constants' ) ;
77const { randomDelay, waitForNavigation, isMaintenanceTime, isScreenshotExists } = require ( './utils' ) ;
88
9- async function processCompany ( webContents , id , company , context , sendLog , sendProgress , isStopRequested ) {
10- const { pendingCodes, outputDir, folderStructure, includeCompanyName, i, idsLength, totalVotes , totalShots } = context ;
9+ async function processCompany ( webContents , id , company , context , sendLog , emitProgress , isStopRequested ) {
10+ const { pendingCodes, outputDir, folderStructure, includeCompanyName, i, idsLength, sessionStats } = context ;
1111 const { code } = company ;
1212
1313 if ( isScreenshotExists ( id , code , outputDir , folderStructure ) ) {
1414 sendLog ( `[清單] ${ code } 已有截圖,跳過。` ) ;
15- if ( pendingCodes . includes ( code ) ) context . currentVote ++ ;
15+ if ( pendingCodes . includes ( code ) ) {
16+ context . currentVote ++ ;
17+ sessionStats . voted ++ ;
18+ }
1619 context . currentShot ++ ;
17- sendProgress ( {
18- id : { current : i + 1 , total : idsLength } ,
19- vote : { current : context . currentVote , total : totalVotes } ,
20- screenshot : { current : context . currentShot , total : totalShots } ,
21- } ) ;
20+ sessionStats . screenshoted ++ ;
21+ emitProgress ( ) ;
2222 return ;
2323 }
2424
@@ -35,12 +35,8 @@ async function processCompany(webContents, id, company, context, sendLog, sendPr
3535 sendLog ( `[投票] ${ code } 成功。` ) ;
3636
3737 context . currentVote ++ ;
38- if ( context . sessionStats ) context . sessionStats . voted ++ ;
39- sendProgress ( {
40- id : { current : i + 1 , total : idsLength } ,
41- vote : { current : context . currentVote , total : totalVotes } ,
42- screenshot : { current : context . currentShot , total : totalShots } ,
43- } ) ;
38+ sessionStats . voted ++ ;
39+ emitProgress ( ) ;
4440
4541 sendLog ( '[導航] 返回查詢...' ) ;
4642 const waitGo = waitForNavigation ( webContents ) ;
@@ -63,11 +59,8 @@ async function processCompany(webContents, id, company, context, sendLog, sendPr
6359 sendLog ( `[導航] 已投過,在查詢頁...` ) ;
6460 if ( pendingCodes . includes ( code ) ) {
6561 context . currentVote ++ ;
66- sendProgress ( {
67- id : { current : i + 1 , total : idsLength } ,
68- vote : { current : context . currentVote , total : totalVotes } ,
69- screenshot : { current : context . currentShot , total : totalShots } ,
70- } ) ;
62+ sessionStats . voted ++ ;
63+ emitProgress ( ) ;
7164 }
7265 }
7366
@@ -77,12 +70,8 @@ async function processCompany(webContents, id, company, context, sendLog, sendPr
7770 sendLog ( `[截圖] 已存: ${ path . basename ( screenshotPath ) } ` ) ;
7871
7972 context . currentShot ++ ;
80- if ( context . sessionStats ) context . sessionStats . screenshoted ++ ;
81- sendProgress ( {
82- id : { current : i + 1 , total : idsLength } ,
83- vote : { current : context . currentVote , total : totalVotes } ,
84- screenshot : { current : context . currentShot , total : totalShots } ,
85- } ) ;
73+ sessionStats . screenshoted ++ ;
74+ emitProgress ( ) ;
8675
8776 } catch ( procError ) {
8877 if ( isStopRequested ( ) ) return ;
@@ -94,8 +83,41 @@ async function processId(webContents, id, i, ids, sendLog, sendProgress, isStopR
9483 const { outputDir, folderStructure, includeCompanyName } = config ;
9584 const maskedId = `${ id . substring ( 0 , 4 ) } ****${ id . substring ( 8 ) } ` ;
9685
86+ const context = {
87+ pendingCodes : [ ] ,
88+ outputDir,
89+ folderStructure,
90+ includeCompanyName,
91+ i,
92+ idsLength : ids . length ,
93+ totalVotes : 0 ,
94+ totalShots : 0 ,
95+ currentVote : 0 ,
96+ currentShot : 0 ,
97+ sessionStats,
98+ } ;
99+
100+ const emitProgress = ( status = 'processing' ) => {
101+ sendProgress ( {
102+ id : { current : i + 1 , total : ids . length } ,
103+ vote : {
104+ current : context . currentVote ,
105+ total : context . totalVotes ,
106+ globalCurrent : sessionStats . voted ,
107+ globalTotal : sessionStats . totalVotes ,
108+ } ,
109+ screenshot : {
110+ current : context . currentShot ,
111+ total : context . totalShots ,
112+ globalCurrent : sessionStats . screenshoted ,
113+ globalTotal : sessionStats . totalShots ,
114+ } ,
115+ status,
116+ } ) ;
117+ } ;
118+
97119 sendLog ( `[系統] 處理: ${ maskedId } ` ) ;
98- sendProgress ( { id : { current : i , total : ids . length } , vote : { current : 0 , total : 0 } , screenshot : { current : 0 , total : 0 } } ) ;
120+ emitProgress ( 'initializing' ) ;
99121
100122 try {
101123 sendLog ( '[系統] 清空 Session...' ) ;
@@ -105,6 +127,7 @@ async function processId(webContents, id, i, ids, sendLog, sendProgress, isStopR
105127 const loggedIn = await login . execute ( webContents , id , sendLog ) ;
106128 if ( ! loggedIn ) {
107129 sendLog ( `[登入] ${ maskedId } 失敗,跳過。` , 'error' ) ;
130+ emitProgress ( 'finished' ) ;
108131 return ;
109132 }
110133
@@ -115,31 +138,20 @@ async function processId(webContents, id, i, ids, sendLog, sendProgress, isStopR
115138 const votedNeedScreenshot = companies . filter ( c => c . status === 'voted' && ! isScreenshotExists ( id , c . code , outputDir , folderStructure ) ) ;
116139 const targetCompanies = [ ...pendingCompanies , ...votedNeedScreenshot ] ;
117140
118- const context = {
119- pendingCodes : pendingCompanies . map ( c => c . code ) ,
120- outputDir,
121- folderStructure,
122- includeCompanyName,
123- i,
124- idsLength : ids . length ,
125- totalVotes : pendingCompanies . length ,
126- totalShots : targetCompanies . length ,
127- currentVote : 0 ,
128- currentShot : 0 ,
129- sessionStats,
130- } ;
141+ context . pendingCodes = pendingCompanies . map ( c => c . code ) ;
142+ context . totalVotes = pendingCompanies . length ;
143+ context . totalShots = targetCompanies . length ;
144+
145+ sessionStats . totalVotes += context . totalVotes ;
146+ sessionStats . totalShots += context . totalShots ;
131147
132148 sendLog ( `[清單] 需投 ${ context . totalVotes } ,需截 ${ votedNeedScreenshot . length } 。` ) ;
133- sendProgress ( {
134- id : { current : i + 1 , total : ids . length } ,
135- vote : { current : context . currentVote , total : context . totalVotes } ,
136- screenshot : { current : context . currentShot , total : context . totalShots } ,
137- } ) ;
149+ emitProgress ( ) ;
138150
139151 for ( const company of targetCompanies ) {
140152 if ( isStopRequested ( ) ) break ;
141153
142- await processCompany ( webContents , id , company , context , sendLog , sendProgress , isStopRequested ) ;
154+ await processCompany ( webContents , id , company , context , sendLog , emitProgress , isStopRequested ) ;
143155
144156 if ( ! isStopRequested ( ) ) await voting . navigateBackToList ( webContents , sendLog ) ;
145157 }
@@ -159,8 +171,10 @@ async function processId(webContents, id, i, ids, sendLog, sendProgress, isStopR
159171 await randomDelay ( 1000 , 2000 ) ;
160172
161173 sendLog ( `[系統] ${ maskedId } 結束。` , 'info' ) ;
174+ emitProgress ( 'finished' ) ;
162175 } catch ( error ) {
163176 sendLog ( `[系統] ${ maskedId } 錯誤: ${ error . message } ` , 'error' ) ;
177+ emitProgress ( 'finished' ) ;
164178 }
165179}
166180
@@ -170,7 +184,7 @@ async function run(webContents, ids, sendLog, sendProgress, isStopRequested, out
170184 return { voted : 0 , screenshoted : 0 } ;
171185 }
172186
173- const sessionStats = { voted : 0 , screenshoted : 0 } ;
187+ const sessionStats = { voted : 0 , screenshoted : 0 , totalVotes : 0 , totalShots : 0 } ;
174188 const config = { outputDir, folderStructure, includeCompanyName } ;
175189
176190 for ( let i = 0 ; i < ids . length ; i ++ ) {
0 commit comments