added ranges editor
test-org/trust-module-backend/pipeline/pr-rc This commit looks good Details

pull/28/head
DmitriyA 2025-06-11 07:31:58 -04:00
parent dadb6f3bcb
commit 4074d45384
5 changed files with 62 additions and 4 deletions

View File

@ -1,7 +1,6 @@
import { Controller, Get, Post, Put, Body, Param, Headers, HttpException, HttpStatus } from '@nestjs/common';
import { MenuService } from './menu.service';
import { MenuItem } from './menu.interface';
import { Response } from 'express';
@Controller('menu')
export class MenuController {

View File

@ -4,10 +4,11 @@ import { HttpModule } from '@nestjs/axios';
import { MenuService } from './menu.service';
import { PrometheusModule } from '../prometheus.module';
import { RangeService } from './range.service';
import { RangeController } from './range.controller';
@Module({
imports: [PrometheusModule, HttpModule],
controllers: [MenuController],
controllers: [MenuController, RangeController],
providers: [MenuService, RangeService]
})
export class MenuModule { }

View File

@ -289,17 +289,22 @@ export class MenuService {
if (!item) throw new Error('Menu item not found');
Object.assign(item, update);
// Инвалидируем кэш после изменения
this.menuCache = null;
return item;
}
async saveOverrides(overrides: Partial<MenuItem>[]): Promise<void> {
await fs.writeFile(this.menuOverridesPath, JSON.stringify({ overrides }, null, 2), 'utf-8');
// Инвалидируем кэш после изменения оверрайдов
this.menuCache = null;
}
invalidateCache(): void {
this.menuCache = null;
this.lastModified = new Date();
}
private findMenuItem(menu: MenuItem, id: string): MenuItem | null {
if (menu.id === id) return menu;

View File

@ -0,0 +1,37 @@
import { Controller, Post, Get, Body, HttpException, HttpStatus } from '@nestjs/common';
import { RangeService } from './range.service';
import { MenuService } from './menu.service';
@Controller('ranges')
export class RangeController {
constructor(
private readonly rangeService: RangeService,
private readonly menuService: MenuService
) { }
@Get('list')
async getRanges() {
try {
return await this.rangeService.getRanges();
} catch (error) {
throw new HttpException('Failed to fetch ranges', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Post('update')
async updateRanges(
@Body() data: Array<{ name: string; ranges: { min: number; max: number; status: number }[] }>
) {
if (!Array.isArray(data)) {
throw new HttpException('Invalid data format', HttpStatus.BAD_REQUEST);
}
try {
const result = await this.rangeService.updateRanges(data);
this.menuService.invalidateCache();
return result;
} catch (error) {
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

View File

@ -51,4 +51,20 @@ export class RangeService {
return {};
}
}
async updateRanges(data: Array<{ name: string; ranges: { min: number; max: number; status: number }[] }>) {
try {
const response = await firstValueFrom(
this.httpService.post('http://192.168.2.39:9999/api/ranges/9999', data, {
headers: { 'Content-Type': 'application/json' },
})
);
return response.data;
} catch (error) {
console.error('Failed to update ranges:', error);
throw new Error('Failed to update ranges');
}
}
}