feat(analytics): AVM v2 batch valuation, comparison, history + frontend upgrade
Add batch valuation (POST /analytics/valuation/batch, max 50 properties), valuation comparison (POST /analytics/valuation/compare, 2-5 properties), and history endpoint (GET /analytics/valuation/history/:propertyId) with confidence explanation helper. Frontend: enhanced valuation form with project autocomplete and deep analysis toggle, results with confidence badges and price range visualization, comparables table, history chart, market context card, and PDF export. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -1,17 +1,22 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { PrismaService, LoggerService } from '@modules/shared';
|
||||
import { type PrismaService, type LoggerService } from '@modules/shared';
|
||||
import {
|
||||
IAVMService,
|
||||
type IAVMService,
|
||||
type AVMParams,
|
||||
type ValuationResult,
|
||||
type Comparable,
|
||||
type BatchValuationItem,
|
||||
type BatchValuationResult,
|
||||
} from '../../domain/services/avm-service';
|
||||
import {
|
||||
AI_SERVICE_CLIENT,
|
||||
IAiServiceClient,
|
||||
type IAiServiceClient,
|
||||
type AiPredictRequest,
|
||||
} from './ai-service.client';
|
||||
import { PrismaAVMService } from './prisma-avm.service';
|
||||
import { type PrismaAVMService } from './prisma-avm.service';
|
||||
|
||||
/** Max concurrency for batch AI calls to avoid overloading the Python service. */
|
||||
const BATCH_CONCURRENCY = 5;
|
||||
|
||||
@Injectable()
|
||||
export class HttpAVMService implements IAVMService {
|
||||
@@ -38,6 +43,41 @@ export class HttpAVMService implements IAVMService {
|
||||
return this.fallback.getComparables(propertyId, radiusMeters);
|
||||
}
|
||||
|
||||
async estimateBatch(items: BatchValuationItem[]): Promise<BatchValuationResult[]> {
|
||||
const results: BatchValuationResult[] = [];
|
||||
|
||||
// Process in batches with limited concurrency
|
||||
for (let i = 0; i < items.length; i += BATCH_CONCURRENCY) {
|
||||
const chunk = items.slice(i, i + BATCH_CONCURRENCY);
|
||||
const chunkResults = await Promise.allSettled(
|
||||
chunk.map(async (item) => {
|
||||
const valuation = await this.estimateValue({ propertyId: item.propertyId });
|
||||
return { propertyId: item.propertyId, valuation } as BatchValuationResult;
|
||||
}),
|
||||
);
|
||||
|
||||
for (let j = 0; j < chunkResults.length; j++) {
|
||||
const result = chunkResults[j]!;
|
||||
const item = chunk[j]!;
|
||||
if (result.status === 'fulfilled') {
|
||||
results.push(result.value);
|
||||
} else {
|
||||
this.logger.warn(
|
||||
`Batch valuation failed for property ${item.propertyId}: ${String(result.reason)}`,
|
||||
'HttpAVMService',
|
||||
);
|
||||
results.push({
|
||||
propertyId: item.propertyId,
|
||||
valuation: null,
|
||||
error: result.reason instanceof Error ? result.reason.message : 'Lỗi định giá',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private async estimateViaAi(params: AVMParams): Promise<ValuationResult> {
|
||||
const propertyData = params.propertyId
|
||||
? await this.getPropertyDetails(params.propertyId)
|
||||
|
||||
Reference in New Issue
Block a user