This commit is contained in:
2026-04-19 13:23:00 +03:00
parent e4c74025e5
commit 1346924387
25 changed files with 1639 additions and 1076 deletions
+29 -16
View File
@@ -1,7 +1,9 @@
import { Injectable, Logger } from "@nestjs/common";
import { HttpService } from "@nestjs/axios";
import { ConfigService } from "@nestjs/config";
import { firstValueFrom } from "rxjs";
import {
AiEngineClient,
AiEngineRequestError,
} from "../common/utils/ai-engine-client";
export interface AIPredictionResult {
matchId: string;
@@ -40,13 +42,21 @@ export interface AIPredictionResult {
export class AiService {
private readonly logger = new Logger(AiService.name);
private readonly pythonEngineUrl: string;
private readonly aiEngineClient: AiEngineClient;
constructor(
private readonly httpService: HttpService,
private readonly configService: ConfigService,
) {
this.pythonEngineUrl =
this.configService.get("AI_ENGINE_URL") || "http://127.0.0.1:8000";
this.aiEngineClient = new AiEngineClient({
baseUrl: this.pythonEngineUrl,
logger: this.logger,
serviceName: AiService.name,
timeoutMs: 30000,
maxRetries: 2,
retryDelayMs: 500,
});
}
/**
@@ -71,14 +81,9 @@ export class AiService {
`Calling Python V25 Engine for ${matchDetails.homeTeam} vs ${matchDetails.awayTeam}`,
);
const response = await firstValueFrom(
this.httpService.post(
`${this.pythonEngineUrl}/v20plus/analyze/${matchId}`,
{},
{
timeout: 30000,
},
),
const response = await this.aiEngineClient.post(
`/v20plus/analyze/${matchId}`,
{},
);
if (response.data) {
@@ -86,8 +91,14 @@ export class AiService {
}
return null;
} catch (error: any) {
this.logger.warn(`Python Engine error: ${error.message}`);
} catch (error: unknown) {
const message =
error instanceof AiEngineRequestError
? error.message
: error instanceof Error
? error.message
: "Unknown AI engine error";
this.logger.warn(`Python Engine error: ${message}`);
return null;
}
}
@@ -286,10 +297,12 @@ export class AiService {
*/
async checkHealth(): Promise<boolean> {
try {
const response = await firstValueFrom(
this.httpService.get(`${this.pythonEngineUrl}/health`, {
const response = await this.aiEngineClient.get<{ status?: string }>(
"/health",
{
timeout: 5000,
}),
retryCount: 0,
},
);
return response.data?.status === "healthy";
} catch {