Spaces:
Sleeping
Sleeping
Download src/classes/core/AggregationCounterAPI.ts from lamsaoganh/teamta: direct link, hf CLI and curl.
- Browser
- Download file 8.54 kB
-
https://huggingface.co/spaces/lamsaoganh/teamta/resolve/main/src/classes/core/AggregationCounterAPI.ts
- Command line
-
hf download hf://spaces/lamsaoganh/teamta/src/classes/core/AggregationCounterAPI.ts
-
curl -L -o AggregationCounterAPI.ts https://huggingface.co/spaces/lamsaoganh/teamta/resolve/main/src/classes/core/AggregationCounterAPI.ts
8.54 kB
| import axios from 'axios'; | |
| import { IAggregationCounterAPI } from '../../interfaces/core/IAggregationCounterAPI'; | |
| import { IAggregationCounter } from '../../interfaces/core/IAggregationCounter'; | |
| import { IWorkerRegistry } from '../../interfaces/core/IWorkerRegistry'; | |
| export class AggregationCounterAPI implements IAggregationCounterAPI { | |
| private apiBaseUrl: string; | |
| private apiTimeout: number; | |
| private workerRegistry: IWorkerRegistry; | |
| constructor(apiBaseUrl: string, workerRegistry: IWorkerRegistry, apiTimeout: number = 30000) { | |
| this.apiBaseUrl = apiBaseUrl; | |
| this.workerRegistry = workerRegistry; | |
| this.apiTimeout = apiTimeout; | |
| } | |
| getApiBaseUrl(): string { | |
| return this.apiBaseUrl; | |
| } | |
| setApiBaseUrl(apiBaseUrl: string): void { | |
| this.apiBaseUrl = String(apiBaseUrl || '').trim().replace(/\/$/, ''); | |
| } | |
| // Tạo counter mới | |
| async createCounter(params: { | |
| requestId: string; | |
| stepId: string; | |
| targetMessageType: string; | |
| expectedCount: number; | |
| timeoutMs?: number; | |
| timeoutAt?: string|undefined; | |
| parentResults?: any; | |
| delayMs?: number; // Số milliseconds delay khi tạo target message | |
| targetMessageAggregationCounters?: any[]; // Aggregation counters cho target message | |
| }): Promise<IAggregationCounter> { | |
| console.log(`Creating aggregation counter: ${params.requestId}:${params.stepId}`); | |
| try { | |
| const response = await axios.post( | |
| `${this.apiBaseUrl}/aggregation-counters`, | |
| params, | |
| { | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| timeout: this.apiTimeout | |
| } | |
| ); | |
| const counter = this.mapFromDto(response.data.counter); | |
| console.log(`Created aggregation counter successfully: ${counter.aggregationKey}`); | |
| return counter; | |
| } catch (error: any) { | |
| if (error.response) { | |
| console.error(`Failed to create aggregation counter: ${error.response.status} - ${error.response.data?.error || 'Unknown error'}`); | |
| throw new Error(`API error: ${error.response.data?.error || 'Unknown error'}`); | |
| } else if (error.request) { | |
| console.error('Failed to create aggregation counter: No response from server'); | |
| throw new Error('No response from Noah API server'); | |
| } else { | |
| console.error('Failed to create aggregation counter:', error.message); | |
| throw error; | |
| } | |
| } | |
| } | |
| // Increment counter | |
| async incrementCounter(params: { | |
| aggregationKey: string; | |
| taskType: string; | |
| result: any; | |
| workerId?: string; | |
| messageId?: string; // ID của message đã hoàn thành | |
| }): Promise<{ | |
| counter: IAggregationCounter | null; | |
| isComplete: boolean; | |
| messageCreated?: boolean; | |
| }> { | |
| console.log(`Incrementing aggregation counter: ${params.aggregationKey} (${params.taskType})`); | |
| try { | |
| const response = await axios.post( | |
| `${this.apiBaseUrl}/aggregation-counters/increment`, | |
| { | |
| workerRegistry: this.workerRegistry, | |
| incrementData: params | |
| }, | |
| { | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| timeout: this.apiTimeout | |
| } | |
| ); | |
| const result = { | |
| counter: response.data.counter ? this.mapFromDto(response.data.counter) : null, | |
| isComplete: response.data.isComplete, | |
| messageCreated: response.data.messageCreated | |
| }; | |
| if (result.isComplete) { | |
| console.log(`Aggregation counter completed: ${params.aggregationKey}`); | |
| if (result.messageCreated) { | |
| console.log(`Target message created for: ${params.aggregationKey}`); | |
| } | |
| } else { | |
| console.log(`Aggregation counter updated: ${params.aggregationKey}`); | |
| } | |
| return result; | |
| } catch (error: any) { | |
| if (error.response?.status === 404) { | |
| console.log(`Counter not found or not in waiting state: ${params.aggregationKey}`); | |
| return { counter: null, isComplete: false }; | |
| } | |
| if (error.response) { | |
| console.error(`Failed to increment counter: ${error.response.status} - ${error.response.data?.error || 'Unknown error'}`); | |
| throw new Error(`API error: ${error.response.data?.error || 'Unknown error'}`); | |
| } else if (error.request) { | |
| console.error('Failed to increment counter: No response from server'); | |
| throw new Error('No response from Noah API server'); | |
| } else { | |
| console.error('Failed to increment counter:', error.message); | |
| throw error; | |
| } | |
| } | |
| } | |
| // Lấy counter theo key | |
| async getCounterByKey(aggregationKey: string): Promise<IAggregationCounter | null> { | |
| try { | |
| const response = await axios.get( | |
| `${this.apiBaseUrl}/aggregation-counters/${aggregationKey}`, | |
| { | |
| timeout: this.apiTimeout | |
| } | |
| ); | |
| return this.mapFromDto(response.data.counter); | |
| } catch (error: any) { | |
| if (error.response?.status === 404) { | |
| return null; | |
| } | |
| if (error.response) { | |
| console.error(`Failed to get counter: ${error.response.status} - ${error.response.data?.error || 'Unknown error'}`); | |
| throw new Error(`API error: ${error.response.data?.error || 'Unknown error'}`); | |
| } else if (error.request) { | |
| console.error('Failed to get counter: No response from server'); | |
| throw new Error('No response from Noah API server'); | |
| } else { | |
| console.error('Failed to get counter:', error.message); | |
| throw error; | |
| } | |
| } | |
| } | |
| // Lấy counters theo requestId | |
| async getCountersByRequestId(requestId: string): Promise<IAggregationCounter[]> { | |
| try { | |
| const response = await axios.get( | |
| `${this.apiBaseUrl}/aggregation-counters/request/${requestId}`, | |
| { | |
| timeout: this.apiTimeout | |
| } | |
| ); | |
| return response.data.counters.map((counter: any) => this.mapFromDto(counter)); | |
| } catch (error: any) { | |
| if (error.response) { | |
| console.error(`Failed to get counters by requestId: ${error.response.status} - ${error.response.data?.error || 'Unknown error'}`); | |
| throw new Error(`API error: ${error.response.data?.error || 'Unknown error'}`); | |
| } else if (error.request) { | |
| console.error('Failed to get counters by requestId: No response from server'); | |
| throw new Error('No response from Noah API server'); | |
| } else { | |
| console.error('Failed to get counters by requestId:', error.message); | |
| throw error; | |
| } | |
| } | |
| } | |
| // Helper method to map DTO to interface | |
| private mapFromDto(dto: any): IAggregationCounter { | |
| return { | |
| _id: dto._id || null, | |
| requestId: dto.requestId, | |
| stepId: dto.stepId, | |
| aggregationKey: dto.aggregationKey, | |
| targetMessageType: dto.targetMessageType, | |
| expectedCount: dto.expectedCount, | |
| currentCount: dto.currentCount, | |
| status: dto.status, | |
| createdAt: new Date(dto.createdAt), | |
| updatedAt: new Date(dto.updatedAt), | |
| timeoutAt: dto.timeoutAt ? new Date(dto.timeoutAt) : null, | |
| parentResults: dto.parentResults, | |
| completedTaskMessageIds: dto.completedTaskMessageIds || [], | |
| ...(dto.delayMs !== undefined && { delayMs: dto.delayMs }) | |
| }; | |
| } | |
| } |