@@ -104,49 +104,45 @@ class SummaryGenerator {
104104
105105 const successfulWrites : string [ ] = [ ] ;
106106 const failedWrites : string [ ] = [ ] ;
107+ const tasksDir = path . join ( this . metricsDir , 'tasks' ) ;
108+ try {
109+ await fs . mkdir ( tasksDir , { recursive : true } ) ;
110+ } catch ( mkdirError ) {
111+ logger . warn (
112+ `Could not create tasks directory: ${ ( mkdirError as Error ) . message } ` ,
113+ ) ;
114+ }
107115
108- for ( const metric of taskMetrics ) {
109- try {
110- // Include directoryId in the filename for better identification
111- // If directoryId is empty, use the startTime as a fallback
112- const directoryId =
113- metric . directoryId ??
114- metric . startTime ?. toString ( ) ??
115- Date . now ( ) . toString ( ) ;
116- const filename = `${ metric . mode ?? 'unknown' } _task${ metric . taskId ?? 0 } _${ directoryId } .json` ;
117- const tasksDir = path . join ( this . metricsDir , 'tasks' ) ;
118-
119- // Ensure tasks directory exists
116+ await Promise . all (
117+ taskMetrics . map ( async ( metric ) => {
120118 try {
121- await fs . mkdir ( tasksDir , { recursive : true } ) ;
122- } catch ( mkdirError ) {
123- logger . warn (
124- `Could not create tasks directory: ${ ( mkdirError as Error ) . message } ` ,
119+ const directoryId =
120+ metric . directoryId ??
121+ metric . startTime ?. toString ( ) ??
122+ Date . now ( ) . toString ( ) ;
123+ const filename = `${ metric . mode ?? 'unknown' } _task${ metric . taskId ?? 0 } _${ directoryId } .json` ;
124+ const filePath = path . join ( tasksDir , filename ) ;
125+
126+ logger . info (
127+ `Writing metric file for task ${ metric . taskId } with ${ metric . apiCalls ?? 0 } API calls` ,
125128 ) ;
126- }
127129
128- const filePath = path . join ( tasksDir , filename ) ;
129-
130- // Log the metric before writing to help debug
131- logger . info (
132- `Writing metric file for task ${ metric . taskId } with ${ metric . apiCalls ?? 0 } API calls` ,
133- ) ;
134-
135- const payload = JSON . stringify (
136- convertTaskMetricToFilePayload ( metric ) ,
137- null ,
138- 2 ,
139- ) ;
140- await fs . writeFile ( filePath , payload ) ;
130+ const payload = JSON . stringify (
131+ convertTaskMetricToFilePayload ( metric ) ,
132+ null ,
133+ 2 ,
134+ ) ;
135+ await fs . writeFile ( filePath , payload ) ;
141136
142- successfulWrites . push ( filename ) ;
143- } catch ( error ) {
144- logger . error (
145- `Error writing metric file for task ${ metric . taskId } : ${ ( error as Error ) . message } ` ,
146- ) ;
147- failedWrites . push ( `task${ metric . taskId ?? 0 } ` ) ;
148- }
149- }
137+ successfulWrites . push ( filename ) ;
138+ } catch ( error ) {
139+ logger . error (
140+ `Error writing metric file for task ${ metric . taskId } : ${ ( error as Error ) . message } ` ,
141+ ) ;
142+ failedWrites . push ( `task${ metric . taskId ?? 0 } ` ) ;
143+ }
144+ } ) ,
145+ ) ;
150146
151147 if ( failedWrites . length > 0 ) {
152148 return {
@@ -209,21 +205,33 @@ class SummaryGenerator {
209205 const allMetrics : TaskMetrics [ ] = [ ] ;
210206 const failedFiles : string [ ] = [ ] ;
211207
212- for ( const file of metricFiles ) {
208+ const readPromises = metricFiles . map ( async ( file ) => {
213209 const filePath = path . join ( this . metricsDir , 'tasks' , file ) ;
214210 try {
215211 const fileContent = await fs . readFile ( filePath , 'utf8' ) ;
216212 const metric = JSON . parse ( fileContent ) ;
217-
218- // Convert to the format expected by the summary
219- allMetrics . push ( convertFileMetricToTaskMetric ( metric ) ) ;
213+ return {
214+ success : true ,
215+ metric : convertFileMetricToTaskMetric ( metric ) ,
216+ file,
217+ } ;
220218 } catch ( error ) {
221219 logger . error (
222220 `Error parsing metric file ${ file } : ${ ( error as Error ) . message } ` ,
223221 ) ;
224- failedFiles . push ( file ) ;
222+ return { success : false , file } ;
225223 }
226- }
224+ } ) ;
225+
226+ const results = await Promise . all ( readPromises ) ;
227+
228+ results . forEach ( ( result ) => {
229+ if ( result . success && result . metric ) {
230+ allMetrics . push ( result . metric ) ;
231+ } else {
232+ failedFiles . push ( result . file ) ;
233+ }
234+ } ) ;
227235
228236 if ( allMetrics . length === 0 ) {
229237 return {
0 commit comments