diff --git a/app/lib/service/index.ts b/app/lib/service/index.ts index 8a922a3..17cb1b2 100644 --- a/app/lib/service/index.ts +++ b/app/lib/service/index.ts @@ -24,6 +24,7 @@ import { defaultConfig } from '@/app/lib/config'; import { env } from '@/app/config/env'; import { type S3 } from '@/app/lib/storage/s3'; import { isValidPlaywrightVersion } from '@/app/lib/pw'; +import { getTimestamp } from '@/app/lib/time'; class Service { private static instance: Service; @@ -79,13 +80,6 @@ class Service { }); } - const getTimestamp = (date?: Date | string) => { - if (!date) return 0; - if (typeof date === 'string') return new Date(date).getTime(); - - return date.getTime(); - }; - reports.sort((a, b) => getTimestamp(b.createdAt) - getTimestamp(a.createdAt)); const currentReports = handlePagination(reports, input?.pagination); @@ -215,13 +209,6 @@ class Service { return await storage.readResults(input); } - const getTimestamp = (date?: Date | string) => { - if (!date) return 0; - if (typeof date === 'string') return new Date(date).getTime(); - - return date.getTime(); - }; - cached.sort((a, b) => getTimestamp(b.createdAt) - getTimestamp(a.createdAt)); let filtered = input?.project diff --git a/app/lib/storage/fs.ts b/app/lib/storage/fs.ts index 051f9c1..4ea1c10 100644 --- a/app/lib/storage/fs.ts +++ b/app/lib/storage/fs.ts @@ -77,8 +77,9 @@ export async function readFile(targetPath: string, contentType: string | null) { async function getResultsCount() { const files = await fs.readdir(RESULTS_FOLDER); + const zipFilesCount = files.filter((file) => file.endsWith('.zip')); - return Math.round(files.length / 2); + return zipFilesCount.length; } export async function readResults(input?: ReadResultsInput) { diff --git a/app/lib/storage/s3.ts b/app/lib/storage/s3.ts index e69c76f..858cf02 100644 --- a/app/lib/storage/s3.ts +++ b/app/lib/storage/s3.ts @@ -1,5 +1,5 @@ -import { randomUUID, type UUID } from 'crypto'; -import fs from 'fs/promises'; +import { randomUUID, type UUID } from 'node:crypto'; +import fs from 'node:fs/promises'; import path from 'node:path'; import { PassThrough, Readable } from 'node:stream'; @@ -42,6 +42,7 @@ import { withError } from '@/app/lib/withError'; import { env } from '@/app/config/env'; import { SiteWhiteLabelConfig } from '@/app/types'; import { defaultConfig, isConfigValid } from '@/app/lib/config'; +import { getTimestamp } from '@/app/lib/time'; const createClient = () => { const endPoint = env.S3_ENDPOINT; @@ -183,7 +184,7 @@ export class S3 implements Storage { let resultCount = 0; let indexCount = 0; let totalSize = 0; - const stream = this.client.listObjects(this.bucket, folderPath, true); + const stream = this.client.listObjectsV2(this.bucket, folderPath, true); return new Promise((resolve, reject) => { stream.on('data', (obj) => { @@ -287,14 +288,6 @@ export class S3 implements Storage { total: 0, }; } - - const getTimestamp = (date?: Date | string) => { - if (!date) return 0; - if (typeof date === 'string') return new Date(date).getTime(); - - return date.getTime(); - }; - jsonFiles.sort((a, b) => getTimestamp(b.lastModified) - getTimestamp(a.lastModified)); // check if we can apply pagination early @@ -423,13 +416,6 @@ export class S3 implements Storage { }); reportsStream.on('end', async () => { - const getTimestamp = (date?: Date | string) => { - if (!date) return 0; - if (typeof date === 'string') return new Date(date).getTime(); - - return date.getTime(); - }; - reports.sort((a, b) => getTimestamp(b.createdAt) - getTimestamp(a.createdAt)); const currentReports = handlePagination(reports, input?.pagination); @@ -691,7 +677,7 @@ export class S3 implements Storage { console.error(`[s3] failed to create temporary folder: ${mkdirTempError.message}`); } - const resultsStream = this.client.listObjects(this.bucket, RESULTS_BUCKET, true); + const resultsStream = this.client.listObjectsV2(this.bucket, RESULTS_BUCKET, true); console.log(`[s3] start processing...`); for await (const result of resultsStream) { diff --git a/app/lib/time.ts b/app/lib/time.ts index 175e9be..fb5c5c6 100644 --- a/app/lib/time.ts +++ b/app/lib/time.ts @@ -18,3 +18,10 @@ export const parseMilliseconds = (ms: number) => { return `${leftMs}ms`; }; + +export const getTimestamp = (date?: Date | string) => { + if (!date) return 0; + if (typeof date === 'string') return new Date(date).getTime(); + + return date.getTime(); +};