@@ -9,10 +9,16 @@ import { RateLimiter } from "adminforth";
99export default class BulkAiFlowPlugin extends AdminForthPlugin {
1010 options : PluginOptions ;
1111 uploadPlugin : AdminForthPlugin ;
12+ totalCalls : number ;
13+ totalDuration : number ;
1214
1315 constructor ( options : PluginOptions ) {
1416 super ( options , import . meta. url ) ;
1517 this . options = options ;
18+
19+ // for calculating average time
20+ this . totalCalls = 0 ;
21+ this . totalDuration = 0 ;
1622 }
1723
1824 // Compile Handlebars templates in outputFields using record fields as context
@@ -263,7 +269,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
263269 if ( this . checkRateLimit ( this . options . generateImages [ fieldName ] . rateLimit , headers ) ) {
264270 return { error : "Rate limit exceeded" } ;
265271 }
266-
272+ const start = + new Date ( ) ;
267273 const STUB_MODE = true ;
268274 const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( this . resourceConfig . columns . find ( c => c . primaryKey ) ?. name , Id ) ] ) ;
269275 const attachmentFiles = await this . options . attachFiles ( { record } ) ;
@@ -286,6 +292,8 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
286292 return resp . imageURLs [ 0 ]
287293 } )
288294 ) ;
295+ this . totalCalls ++ ;
296+ this . totalDuration += ( + new Date ( ) - start ) / 1000 ;
289297 return { images } ;
290298 }
291299 } ) ;
@@ -301,11 +309,10 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
301309 if ( this . checkRateLimit ( this . options . bulkGenerationRateLimit , headers ) ) {
302310 return { error : "Rate limit exceeded" } ;
303311 }
304-
312+ const start = + new Date ( ) ;
305313 const tasks = selectedIds . map ( async ( ID ) => {
306314 const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( this . resourceConfig . columns . find ( c => c . primaryKey ) ?. name , ID ) ] ) ;
307315 const attachmentFiles = await this . options . attachFiles ( { record } ) ;
308-
309316 const fieldTasks = Object . keys ( this . options ?. generateImages || { } ) . map ( async ( key ) => {
310317 const prompt = this . compileGenerationFieldTemplates ( record ) [ key ] ;
311318 let images ;
@@ -336,6 +343,10 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
336343 return recordResult ;
337344 } ) ;
338345 const result = await Promise . all ( tasks ) ;
346+
347+ this . totalCalls ++ ;
348+ this . totalDuration += ( + new Date ( ) - start ) / 1000 ;
349+
339350 return { result } ;
340351 }
341352 } ) ;
@@ -353,6 +364,16 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
353364 } ) ;
354365
355366
356-
367+ server . endpoint ( {
368+ method : 'GET' ,
369+ path : `/plugin/${ this . pluginInstanceId } /averageDuration` ,
370+ handler : async ( ) => {
371+ return {
372+ totalCalls : this . totalCalls ,
373+ totalDuration : this . totalDuration ,
374+ averageDuration : this . totalCalls ? this . totalDuration / this . totalCalls : null ,
375+ } ;
376+ }
377+ } ) ;
357378 }
358379}
0 commit comments