@@ -9,10 +9,16 @@ import { RateLimiter } from "adminforth";
9
9
export default class BulkAiFlowPlugin extends AdminForthPlugin {
10
10
options : PluginOptions ;
11
11
uploadPlugin : AdminForthPlugin ;
12
+ totalCalls : number ;
13
+ totalDuration : number ;
12
14
13
15
constructor ( options : PluginOptions ) {
14
16
super ( options , import . meta. url ) ;
15
17
this . options = options ;
18
+
19
+ // for calculating average time
20
+ this . totalCalls = 0 ;
21
+ this . totalDuration = 0 ;
16
22
}
17
23
18
24
// Compile Handlebars templates in outputFields using record fields as context
@@ -263,7 +269,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
263
269
if ( this . checkRateLimit ( this . options . generateImages [ fieldName ] . rateLimit , headers ) ) {
264
270
return { error : "Rate limit exceeded" } ;
265
271
}
266
-
272
+ const start = + new Date ( ) ;
267
273
const STUB_MODE = true ;
268
274
const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( this . resourceConfig . columns . find ( c => c . primaryKey ) ?. name , Id ) ] ) ;
269
275
const attachmentFiles = await this . options . attachFiles ( { record } ) ;
@@ -286,6 +292,8 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
286
292
return resp . imageURLs [ 0 ]
287
293
} )
288
294
) ;
295
+ this . totalCalls ++ ;
296
+ this . totalDuration += ( + new Date ( ) - start ) / 1000 ;
289
297
return { images } ;
290
298
}
291
299
} ) ;
@@ -301,11 +309,10 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
301
309
if ( this . checkRateLimit ( this . options . bulkGenerationRateLimit , headers ) ) {
302
310
return { error : "Rate limit exceeded" } ;
303
311
}
304
-
312
+ const start = + new Date ( ) ;
305
313
const tasks = selectedIds . map ( async ( ID ) => {
306
314
const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( this . resourceConfig . columns . find ( c => c . primaryKey ) ?. name , ID ) ] ) ;
307
315
const attachmentFiles = await this . options . attachFiles ( { record } ) ;
308
-
309
316
const fieldTasks = Object . keys ( this . options ?. generateImages || { } ) . map ( async ( key ) => {
310
317
const prompt = this . compileGenerationFieldTemplates ( record ) [ key ] ;
311
318
let images ;
@@ -336,6 +343,10 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
336
343
return recordResult ;
337
344
} ) ;
338
345
const result = await Promise . all ( tasks ) ;
346
+
347
+ this . totalCalls ++ ;
348
+ this . totalDuration += ( + new Date ( ) - start ) / 1000 ;
349
+
339
350
return { result } ;
340
351
}
341
352
} ) ;
@@ -353,6 +364,16 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
353
364
} ) ;
354
365
355
366
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
+ } ) ;
357
378
}
358
379
}
0 commit comments