import { Controller, Get, Post } from '@nestjs/common'; import { ClickHouseService } from './clickhouse.service'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { HttpService } from '@nestjs/axios'; import { firstValueFrom } from 'rxjs'; import { ConfigService } from '@nestjs/config'; @ApiTags('Clickhouse') @Controller('clickhouse') export class ClickHouseController { constructor( private readonly clickhouseService: ClickHouseService, private readonly httpService: HttpService, private readonly configService: ConfigService, ) { } @Get() @ApiOperation({ summary: 'Get metrics from ClickHouse' }) @ApiResponse({ status: 200, description: 'Metrics data', schema: { type: 'array', items: { type: 'object', properties: { description: { type: 'string' }, device: { type: 'number' }, id: { type: 'string' }, name: { type: 'string' }, source: { type: 'string' }, status: { type: 'number' }, timestamp: { type: 'number' }, value: { type: 'string' }, }, }, }, }) async getClckhouse() { return this.clickhouseService.getClckhouse(); } @Post('send-to-ai') @ApiOperation({ summary: 'Send metrics to AI service' }) @ApiResponse({ status: 200, description: 'AI service response', }) async sendToAI() { const metrics = await this.clickhouseService.getClckhouse(); const aiServiceUrl = this.configService.get('AI_SERVICE_URL/api/metrics/rest'); try { const response = await firstValueFrom( this.httpService.post(aiServiceUrl, metrics) ); return response.data; } catch (error) { throw new Error(`Failed to send data to AI: ${error.message}`); } } }