Skip to content

Commit

Permalink
ocr complete
Browse files Browse the repository at this point in the history
  • Loading branch information
TinyMurky committed Aug 13, 2024
1 parent 086f5ad commit d8b67cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iSunFA",
"version": "0.8.0+5",
"version": "0.8.0+6",
"private": false,
"scripts": {
"dev": "next dev",
Expand Down
18 changes: 0 additions & 18 deletions src/pages/api/v1/company/[companyId]/ocr/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,24 +445,6 @@ describe('GET OCR', () => {
});
});

describe('calculateProgress', () => {
beforeEach(() => {
jest.spyOn(common, 'timestampInMilliSeconds').mockReturnValue(0);
});
it('should return 100 if success', () => {
const mockImageUrl = 'testImageUrl';

const progress = module.calculateProgress(mockImageUrl);
expect(progress).toBe(100);
});

it('should return 0 if not success and not in progress', () => {
const mockImageUrl = '';
const progress = module.calculateProgress(mockImageUrl);
expect(progress).toBe(0);
});
});

describe('formatUnprocessedOCR', () => {
it('should return IOCR', async () => {
const mockAichId = 'testAichId';
Expand Down
42 changes: 21 additions & 21 deletions src/pages/api/v1/company/[companyId]/ocr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IResponseData } from '@/interfaces/response_data';
import {
formatApiResponse,
generateUUID,
timestampInMilliSeconds,
timestampInSeconds,
transformBytesToFileSizeString,
transformOCRImageIDToURL,
Expand All @@ -27,6 +28,7 @@ import { AuthFunctionsKeys } from '@/interfaces/auth';
import { FileFolder } from '@/constants/file';
import { getAichUrl } from '@/lib/utils/aich';
import { AICH_APIS_TYPES } from '@/constants/aich';
import { AVERAGE_OCR_PROCESSING_TIME } from '@/constants/ocr';

// Info Murky (20240424) 要使用formidable要先關掉bodyParser
export const config = {
Expand Down Expand Up @@ -221,33 +223,31 @@ export async function fetchStatus(aichResultId: string) {
}

// Deprecated (20240809 - Murky) This function is not used
// export function calculateProgress(createdAt: number, status: ProgressStatus) {
// const currentTime = new Date();
// const diffTime = currentTime.getTime() - timestampInMilliSeconds(createdAt);
// let process = Math.ceil((diffTime / AVERAGE_OCR_PROCESSING_TIME) * 100);

// if (process > 99) {
// process = 99;
// }

// if (status === ProgressStatus.SUCCESS) {
// process = 100;
// } else if (status !== ProgressStatus.IN_PROGRESS) {
// process = 0;
// }
// return process;
// }

export function calculateProgress(imageUrl?: string | null) {
return imageUrl && imageUrl.length > 0 ? 100 : 0;
export function calculateProgress(createdAt: number, status: ProgressStatus, ocrResultId: string) {
const currentTime = new Date();
const diffTime = currentTime.getTime() - timestampInMilliSeconds(createdAt);
let process = Math.ceil((diffTime / AVERAGE_OCR_PROCESSING_TIME) * 100);

if (process > 99) {
process = 99;
}

const errorRegexp = /error/i;

if (errorRegexp.test(ocrResultId)) {
process = 0;
} else if (status !== ProgressStatus.IN_PROGRESS) {
process = 100;
}
return process;
}

export async function formatUnprocessedOCR(ocrData: Ocr[]): Promise<IOCR[]> {
const unprocessedOCRs = await Promise.all(
ocrData.map(async (ocr) => {
const status = await fetchStatus(ocr.aichResultId);
// const progress = calculateProgress(ocr.createdAt, status);
const progress = calculateProgress(ocr.imageUrl);
const progress = calculateProgress(ocr.createdAt, status, ocr.aichResultId);
// const progress = calculateProgress(ocr.imageUrl);
const imageSize = transformBytesToFileSizeString(ocr.imageSize);
const createdAt = timestampInSeconds(ocr.createdAt);
const unprocessedOCR: IOCR = {
Expand Down

0 comments on commit d8b67cd

Please sign in to comment.