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';
@Controller('api')
export class ZvksmetricsController {
constructor(private readonly metricsService: ZvksmetricsService) {}
@Get()
getHello(): string {
return 'Hello'; //this.appService.getHello();
@Get('ranges')
async getHello(): Promise<any> {
// 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')
async getPostMessage (@Body() inputMetrics : any) : Promise<string> {
//console.log(inputMetrics);
//this.metricsService.getMetrics(inputMetrics);
let out: any = await this.metricsService.useStatusModel(inputMetrics);
console.log(out);
// console.log(out);
return out;
}

View File

@ -1,15 +1,17 @@
import { Injectable } from '@nestjs/common';
import axios from 'axios';
import { Body, Injectable } from '@nestjs/common';
import axios, { Axios, AxiosResponse } from 'axios';
import { readFile } from 'node:fs/promises';
import { response } from 'express';
@Injectable()
export class MetricK2 {
id : string;
type : string;
addr : string;
value : any;
value : number;
description: string;
status: any;
status: number;
};
@Injectable()
@ -19,61 +21,122 @@ export class K2Metrics {
metrics: MetricK2 [];
};
@Injectable()
export class RangeValues {
min: number;
max: number;
status: number;
};
@Injectable()
export class MetricsRanges {
name: string;
ranges: RangeValues [];
};
@Injectable()
export class ZvksmetricsService {
async useStatusModel(inputDate : any) : Promise<any> {
async useStatusModel(inputData : any) : Promise<any> {
let inp : K2Metrics = new K2Metrics();
inp = inputDate;
let editedMetrics : K2Metrics = await this.getMetrics (inp);
inp = inputData;
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);
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 => {
element.status = await this.setMetricsStatus(element);
});
for(let i=0; i<length; i++){
arr[i].status = await this.setMetricsStatus(arr[i], scope);
// console.log(arr[i].status);
// console.log(arr[i].id);
}
return inputDate;
inputData.metrics = arr;
// console.log(arr.findIndex(el=>el.id='measure_190'));
return inputData;
}
async setMetricsStatus( metric : MetricK2) : Promise<number> {
let stat = Math.floor(Math.random()*4);
// if (stat < 1) {
// stat = 99;
// }
return stat;
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(inputDate : K2Metrics) : Promise<string> {
async sendMetrics(inputData : K2Metrics) : Promise<string> {
let resp : any;
let path : string = 'http://192.168.2.34:9049/update';
let body : any = inputDate;
let body : any = inputData;
let options : any = {
headers: {'Content-Type' : 'application/json'}
};
await axios.post(path, body, options).then((response)=>{
//resp = JSON.parse(response.data);
resp = response.status + ' ' + response.data;
//console.log(resp);
}).catch((error)=>{
resp = error;
});
return resp;
}
/*async showMetrics(inputDate : any){
let metric : K2Metrics = new K2Metrics();
metric = inputDate;
await this.getMetrics(metric);
console.log(metric);
async showStatus(scope: MetricsRanges [], metricName: string, metricValue: number) : Promise<number> {
let status : number = 0;
let test : any = scope.find(element => element.name == metricName);
if (test){
// 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;
}
};