gg
This commit is contained in:
+29
-16
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user