690529:1116 ADR-030-230 context aware #04
CI / CD Pipeline / build (push) Successful in 4m39s
CI / CD Pipeline / deploy (push) Successful in 7m59s

This commit is contained in:
2026-05-29 11:16:03 +07:00
parent 5d46504c1d
commit b68a750e4f
5 changed files with 76 additions and 3 deletions
@@ -3,6 +3,7 @@
// - 2026-05-15: เพิ่ม OCR auto-detection service สำหรับ ADR-023A.
// - 2026-05-25: แก้ไข AggregateError (empty message) จาก axios โดย wrap เป็น Error พร้อม context ที่ชัดเจน.
// - 2026-05-25: เพิ่ม path remapping (OCR_UPLOAD_BASE_PATH) เพื่อแปลง local upload path เป็น path ที่ sidecar เห็นผ่าน CIFS.
// - 2026-05-29: เพิ่ม checkHealth() เพื่อตรวจสอบสุขภาพของ PaddleOCR sidecar สำหรับ getSystemHealth() (ADR-027)
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@@ -23,6 +24,13 @@ interface PaddleOcrResponse {
text?: string;
}
export interface OcrHealthResult {
status: 'HEALTHY' | 'DOWN';
latencyMs: number;
url: string;
error?: string;
}
/** บริการเลือก fast path หรือ PaddleOCR sidecar ตามจำนวนตัวอักษรที่ extract ได้ */
@Injectable()
export class OcrService {
@@ -56,6 +64,28 @@ export class OcrService {
return localPath;
}
/** ตรวจสอบสุขภาพและ latency ของ PaddleOCR sidecar ผ่าน GET /health */
async checkHealth(): Promise<OcrHealthResult> {
const startTime = Date.now();
try {
await axios.get(`${this.ocrApiUrl}/health`, { timeout: 5000 });
return {
status: 'HEALTHY',
latencyMs: Date.now() - startTime,
url: this.ocrApiUrl,
};
} catch (err: unknown) {
const cause = err instanceof Error ? err.message : String(err);
this.logger.warn(`OCR sidecar health check failed: ${cause}`);
return {
status: 'DOWN',
latencyMs: Date.now() - startTime,
url: this.ocrApiUrl,
error: cause,
};
}
}
/** ตรวจสอบ text layer ก่อนเลือก OCR slow path */
async detectAndExtract(
input: OcrDetectionInput