Skip to content

Commit 657f954

Browse files
committed
fix: improve progressbar appearence - make progressbar stats shared between all runs of plugin instance, not only local ones
1 parent 6a541c1 commit 657f954

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

custom/imageGenerator.vue

+22-13
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
:minValue="0"
5151
:maxValue="historicalAverage"
5252
:showValues="false"
53-
:progressFormatter="(value: number, percentage: number) => `${ formatTime(loadingTimer) } ( ${ Math.floor( (
53+
:progressFormatter="(value: number, percentage: number) => `${ formatTime(loadingTimer) } ( ~ ${ Math.floor( (
5454
loadingTimer < historicalAverage ? loadingTimer : historicalAverage
5555
) / historicalAverage * 100) }% )`"
5656
/>
@@ -252,15 +252,10 @@ async function confirmImage() {
252252
253253
const loadingTimer: Ref<number | null> = ref(null);
254254
255-
const historicalRuns: Ref<number[]> = ref([]);
256255
257256
const errorMessage: Ref<string | null> = ref(null);
258257
259-
const historicalAverage: Ref<number | null> = computed(() => {
260-
if (historicalRuns.value.length === 0) return null;
261-
const sum = historicalRuns.value.reduce((a, b) => a + b, 0);
262-
return Math.floor(sum / historicalRuns.value.length);
263-
});
258+
const historicalAverage: Ref<number | null> = ref(null);
264259
265260
266261
function formatTime(seconds: number): string {
@@ -269,6 +264,14 @@ function formatTime(seconds: number): string {
269264
}
270265
271266
267+
async function getHistoricalAverage() {
268+
const resp = await callAdminForthApi({
269+
path: `/plugin/${props.meta.pluginInstanceId}/averageDuration`,
270+
method: 'GET',
271+
});
272+
historicalAverage.value = resp?.averageDuration || null;
273+
}
274+
272275
async function generateImages() {
273276
errorMessage.value = null;
274277
loading.value = true;
@@ -279,7 +282,8 @@ async function generateImages() {
279282
loadingTimer.value = elapsed;
280283
}, 100);
281284
const currentIndex = caurosel.value?.getActiveItem()?.position || 0;
282-
285+
286+
await getHistoricalAverage();
283287
let resp = null;
284288
let error = null;
285289
try {
@@ -294,7 +298,6 @@ async function generateImages() {
294298
} catch (e) {
295299
console.error(e);
296300
} finally {
297-
historicalRuns.value.push(loadingTimer.value);
298301
clearInterval(ticker);
299302
loadingTimer.value = null;
300303
loading.value = false;
@@ -330,12 +333,17 @@ async function generateImages() {
330333
// ];
331334
await nextTick();
332335
336+
333337
caurosel.value = new Carousel(
334338
document.getElementById('gallery'),
335-
images.value.map((img, index) => ({
336-
el: document.getElementById('gallery').querySelector(`[data-carousel-item]:nth-child(${index + 1})`),
337-
position: index,
338-
})),
339+
images.value.map((img, index) => {
340+
console.log('mapping image', img, index);
341+
return {
342+
image: img,
343+
el: document.getElementById('gallery').querySelector(`[data-carousel-item]:nth-child(${index + 1})`),
344+
position: index,
345+
};
346+
}),
339347
{
340348
internal: 0,
341349
defaultPosition: currentIndex,
@@ -345,6 +353,7 @@ async function generateImages() {
345353
}
346354
);
347355
await nextTick();
356+
348357
loading.value = false;
349358
}
350359

index.ts

+24
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ export default class UploadPlugin extends AdminForthPlugin {
1313

1414
adminforth!: IAdminForth;
1515

16+
totalCalls: number;
17+
totalDuration: number;
18+
1619
constructor(options: PluginOptions) {
1720
super(options, import.meta.url);
1821
this.options = options;
22+
23+
// for calcualting average time
24+
this.totalCalls = 0;
25+
this.totalDuration = 0;
1926
}
2027

2128
instanceUniqueRepresentation(pluginOptions: any) : string {
@@ -390,8 +397,21 @@ getBucketLifecycleConfiguration on bucket ${this.options.s3Bucket} in region ${t
390397
// called here because modifyResourceConfig can be called in build time where there is no environment and AWS secrets
391398
this.setupLifecycleRule();
392399
}
400+
393401

394402
setupEndpoints(server: IHttpServer) {
403+
server.endpoint({
404+
method: 'GET',
405+
path: `/plugin/${this.pluginInstanceId}/averageDuration`,
406+
handler: async () => {
407+
return {
408+
totalCalls: this.totalCalls,
409+
totalDuration: this.totalDuration,
410+
averageDuration: this.totalCalls ? this.totalDuration / this.totalCalls : null,
411+
};
412+
}
413+
});
414+
395415
server.endpoint({
396416
method: 'POST',
397417
path: `/plugin/${this.pluginInstanceId}/get_s3_upload_url`,
@@ -527,6 +547,7 @@ getBucketLifecycleConfiguration on bucket ${this.options.s3Bucket} in region ${t
527547
await new Promise((resolve) => setTimeout(resolve, 2000));
528548
return `https://picsum.photos/200/300?random=${Math.floor(Math.random() * 1000)}`;
529549
}
550+
const start = +new Date();
530551
const resp = await this.options.generation.adapter.generate(
531552
{
532553
prompt,
@@ -541,6 +562,9 @@ getBucketLifecycleConfiguration on bucket ${this.options.s3Bucket} in region ${t
541562
error = resp.error;
542563
return;
543564
}
565+
566+
this.totalCalls++;
567+
this.totalDuration += (+new Date() - start) / 1000;
544568

545569
return resp.imageURLs[0]
546570

0 commit comments

Comments
 (0)