Status Model + Border Value (Test)

main
DenisN 2025-04-15 01:45:05 -04:00
parent b2a3c07c59
commit b50beb3fa8
3 changed files with 129 additions and 38 deletions

View File

@ -0,0 +1,18 @@
[
{
"name": "measure_190",
"ranges": [
{"min": 0, "max": 0.1, "status": 0},
{"min": 0.11, "max": 0.12, "status": 1},
{"min": 0.13, "max": 1, "status": 2}
]
},
{
"name": "measure_191",
"ranges": [
{"min": 0, "max": 0.2, "status": 0},
{"min": 0.21, "max": 0.32, "status": 1},
{"min": 0.33, "max": 1, "status": 2}
]
}
]

View File

@ -1,23 +1,33 @@
import { Body, ConfigurableModuleBuilder, Controller, Get, Post} from '@nestjs/common'; import { Body, ConfigurableModuleBuilder, Controller, Get, Options, Param, ParseIntPipe, Post} from '@nestjs/common';
import { ZvksmetricsService } from './zvksmetrics.service'; import { ZvksmetricsService } from './zvksmetrics.service';
@Controller('api') @Controller('api')
export class ZvksmetricsController { export class ZvksmetricsController {
constructor(private readonly metricsService: ZvksmetricsService) {} constructor(private readonly metricsService: ZvksmetricsService) {}
@Get() @Get('ranges')
getHello(): string { async getHello(): Promise<any> {
return 'Hello'; //this.appService.getHello(); // let a = await this.metricsService.showStatus();
// console.log(a);
return null;
} }
//Заглушка для будующей API
@Options('config/:code')
async showConfig (@Param('code', ParseIntPipe) code: number): Promise<JSON>{
let ret : JSON = JSON.parse('{"result":"null"}');
if(code === 9999){
return await this.metricsService.showRangesConfig('File Name');
}
return ret;
}
@Post('input') @Post('input')
async getPostMessage (@Body() inputMetrics : any) : Promise<string> { async getPostMessage (@Body() inputMetrics : any) : Promise<string> {
//console.log(inputMetrics); //console.log(inputMetrics);
//this.metricsService.getMetrics(inputMetrics); //this.metricsService.getMetrics(inputMetrics);
let out: any = await this.metricsService.useStatusModel(inputMetrics); let out: any = await this.metricsService.useStatusModel(inputMetrics);
console.log(out); // console.log(out);
return out; return out;
} }

View File

@ -1,15 +1,17 @@
import { Injectable } from '@nestjs/common'; import { Body, Injectable } from '@nestjs/common';
import axios from 'axios'; import axios, { Axios, AxiosResponse } from 'axios';
import { readFile } from 'node:fs/promises';
import { response } from 'express'; import { response } from 'express';
@Injectable() @Injectable()
export class MetricK2 { export class MetricK2 {
id : string; id : string;
type : string; type : string;
addr : string; addr : string;
value : any; value : number;
description: string; description: string;
status: any; status: number;
}; };
@Injectable() @Injectable()
@ -19,61 +21,122 @@ export class K2Metrics {
metrics: MetricK2 []; metrics: MetricK2 [];
}; };
@Injectable()
export class RangeValues {
min: number;
max: number;
status: number;
};
@Injectable()
export class MetricsRanges {
name: string;
ranges: RangeValues [];
};
@Injectable() @Injectable()
export class ZvksmetricsService { export class ZvksmetricsService {
async useStatusModel(inputDate : any) : Promise<any> { async useStatusModel(inputData : any) : Promise<any> {
let inp : K2Metrics = new K2Metrics(); let inp : K2Metrics = new K2Metrics();
inp = inputDate; inp = inputData;
let editedMetrics : K2Metrics = await this.getMetrics (inp);
let scope : MetricsRanges [] = [];
scope = await this.setMetricsRanges ();
let editedMetrics : K2Metrics = await this.getMetrics (inp, scope);
// console.log(editedMetrics.metrics.find(el=>el.id='measure_191')?.status);
let responseFromExporter : any = await this.sendMetrics (editedMetrics); let responseFromExporter : any = await this.sendMetrics (editedMetrics);
return responseFromExporter; return responseFromExporter;
} }
async getMetrics(inputDate : K2Metrics): Promise<K2Metrics> { async getMetrics(inputData : K2Metrics, scope: MetricsRanges []): Promise<K2Metrics> {
let arr : MetricK2 [] = inputData.metrics;
let length : number = arr.length;
inputDate.metrics.forEach(async element => { for(let i=0; i<length; i++){
element.status = await this.setMetricsStatus(element); arr[i].status = await this.setMetricsStatus(arr[i], scope);
}); // console.log(arr[i].status);
// console.log(arr[i].id);
return inputDate;
} }
async setMetricsStatus( metric : MetricK2) : Promise<number> { inputData.metrics = arr;
let stat = Math.floor(Math.random()*4);
// if (stat < 1) { // console.log(arr.findIndex(el=>el.id='measure_190'));
// stat = 99; return inputData;
// }
return stat;
} }
async sendMetrics(inputDate : K2Metrics) : Promise<string> { async setMetricsStatus( metric : MetricK2, scope: MetricsRanges []) : Promise<number> {
if (metric && scope){
let a = await this.showStatus(scope, metric.id, metric.value);
// console.log(a);
return await this.showStatus(scope, metric.id, metric.value);
}
else {
return 0;
}
}
async sendMetrics(inputData : K2Metrics) : Promise<string> {
let resp : any; let resp : any;
let path : string = 'http://192.168.2.34:9049/update'; let path : string = 'http://192.168.2.34:9049/update';
let body : any = inputDate; let body : any = inputData;
let options : any = { let options : any = {
headers: {'Content-Type' : 'application/json'} headers: {'Content-Type' : 'application/json'}
}; };
await axios.post(path, body, options).then((response)=>{ await axios.post(path, body, options).then((response)=>{
//resp = JSON.parse(response.data);
resp = response.status + ' ' + response.data; resp = response.status + ' ' + response.data;
//console.log(resp);
}).catch((error)=>{ }).catch((error)=>{
resp = error; resp = error;
}); });
return resp; return resp;
} }
/*async showMetrics(inputDate : any){ async showStatus(scope: MetricsRanges [], metricName: string, metricValue: number) : Promise<number> {
let metric : K2Metrics = new K2Metrics(); let status : number = 0;
metric = inputDate; let test : any = scope.find(element => element.name == metricName);
await this.getMetrics(metric); if (test){
console.log(metric); // let range : MetricsRanges = new MetricsRanges;
let range : MetricsRanges = test;
let transit : any = range.ranges.find(element => element.min <= metricValue && metricValue <= element.max)?.status;
status = transit;
// console.log(status);
}
// console.log(scope);
// console.log(range);
// console.log(status);
return status;
}
}*/ async setMetricsRanges (@Body() rangesData : any = null) : Promise<MetricsRanges []> {
let url : string = 'http://192.168.2.39:9999/api/config/9999';
let options : any = {
headers: {'Content-Type' : 'application/json'}
};
await axios.options(url, options).then((response) => {
rangesData = response.data;
}).catch((error)=>{
rangesData = error;
});
return rangesData;
}
async showRangesConfig(filePath: string) : Promise<JSON> {
let content : JSON = JSON.parse('{"name":"name"}');
filePath = './src/zvksmetrics/conf/ranges.json';
try {
content = JSON.parse(await readFile(filePath, { encoding: 'utf8' }));
}
catch {
console.log('Error read file');
}
return content;
}
}; };