From b2a3c07c595a939fec2ae068e7401ef6ef9687f1 Mon Sep 17 00:00:00 2001 From: DenisN Date: Mon, 14 Apr 2025 08:14:47 -0400 Subject: [PATCH] Send Updated Status to Prometheus (Test) --- package.json | 5 +- pnpm-lock.yaml | 47 ++++++++++++++ src/app.service.ts | 35 +---------- src/zvksmetrics/zvksmetrics.controller.ts | 12 ++-- src/zvksmetrics/zvksmetrics.service.ts | 77 ++++++++++++++++++++++- 5 files changed, 136 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index f887406..04e3a4e 100644 --- a/package.json +++ b/package.json @@ -23,10 +23,13 @@ "@nestjs/common": "^11.0.1", "@nestjs/core": "^11.0.1", "@nestjs/platform-express": "^11.0.1", + "axios": "1.8.4", "module": "^1.2.5", "mqtt": "^5.10.4", "reflect-metadata": "^0.2.2", - "rxjs": "^7.8.1" + "rxjs": "^7.8.1", + "source-map-resolve": "0.6.0", + "source-map-url": "0.4.1" }, "devDependencies": { "@eslint/eslintrc": "^3.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d762d45..9c02c78 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: '@nestjs/platform-express': specifier: ^11.0.1 version: 11.0.12(@nestjs/common@11.0.12(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@11.0.12) + axios: + specifier: 1.8.4 + version: 1.8.4(debug@2.6.9) module: specifier: ^1.2.5 version: 1.2.5 @@ -29,6 +32,12 @@ importers: rxjs: specifier: ^7.8.1 version: 7.8.2 + source-map-resolve: + specifier: 0.6.0 + version: 0.6.0 + source-map-url: + specifier: 0.4.1 + version: 0.4.1 devDependencies: '@eslint/eslintrc': specifier: ^3.2.0 @@ -1308,6 +1317,9 @@ packages: engines: {node: '>= 4.5.0'} hasBin: true + axios@1.8.4: + resolution: {integrity: sha512-eBSYY4Y68NNlHbHBMdeDmKNtDgXWhQsJcGqzO3iLUM0GraQFSS9cVgPX5I9b3lbdFKyYoAEGAZF1DwhTaljNAw==} + b4a@1.6.7: resolution: {integrity: sha512-OnAYlL5b7LEkALw87fUVafQw5rVR9RjwGd4KUwNQ6DrrNmaVaUCgLipfVlzrPQ4tWOR9P0IXGNOx50jYCCdSJg==} @@ -2054,6 +2066,15 @@ packages: flatted@3.3.3: resolution: {integrity: sha512-GX+ysw4PBCz0PzosHDepZGANEuFCMLrnRTiEy9McGjmkCQYwRq4A/X786G/fjM/+OjsWSU1ZrY5qyARZmO/uwg==} + follow-redirects@1.15.9: + resolution: {integrity: sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==} + engines: {node: '>=4.0'} + peerDependencies: + debug: '*' + peerDependenciesMeta: + debug: + optional: true + for-in@1.0.2: resolution: {integrity: sha512-7EwmXrOjyL+ChxMhmG5lnW9MPt1aIeZEwKhQzoBUdTV0N3zuwWDZYVJatDvZ2OyzPUvdIAZDsCetk3coyMfcnQ==} engines: {node: '>=0.10.0'} @@ -3183,6 +3204,9 @@ packages: resolution: {integrity: sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==} engines: {node: '>= 0.10'} + proxy-from-env@1.1.0: + resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==} + punycode@2.3.1: resolution: {integrity: sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==} engines: {node: '>=6'} @@ -3457,6 +3481,10 @@ packages: resolution: {integrity: sha512-Htz+RnsXWk5+P2slx5Jh3Q66vhQj1Cllm0zvnaY98+NFx+Dv2CF/f5O/t8x+KaNdrdIAsruNzoh/KpialbqAnw==} deprecated: See https://github.com/lydell/source-map-resolve#deprecated + source-map-resolve@0.6.0: + resolution: {integrity: sha512-KXBr9d/fO/bWo97NXsPIAW1bFSBOuCnjbNTBMO7N59hsv5i9yzRDfcYwwt0l04+VqnKC+EwzvJZIP/qkuMgR/w==} + deprecated: See https://github.com/lydell/source-map-resolve#deprecated + source-map-support@0.5.13: resolution: {integrity: sha512-SHSKFHadjVA5oR4PPqhtAVdcBWwRYVd6g6cAXnIbRiIwc2EhPrTuKUBdSLvlEKyIP3GCf89fltvcZiP9MMFA1w==} @@ -5405,6 +5433,14 @@ snapshots: atob@2.1.2: {} + axios@1.8.4(debug@2.6.9): + dependencies: + follow-redirects: 1.15.9(debug@2.6.9) + form-data: 4.0.2 + proxy-from-env: 1.1.0 + transitivePeerDependencies: + - debug + b4a@1.6.7: {} babel-jest@29.7.0(@babel/core@7.26.10): @@ -6221,6 +6257,10 @@ snapshots: flatted@3.3.3: {} + follow-redirects@1.15.9(debug@2.6.9): + optionalDependencies: + debug: 2.6.9 + for-in@1.0.2: {} for-own@0.1.5: @@ -7503,6 +7543,8 @@ snapshots: forwarded: 0.2.0 ipaddr.js: 1.9.1 + proxy-from-env@1.1.0: {} + punycode@2.3.1: {} pure-rand@6.1.0: {} @@ -7797,6 +7839,11 @@ snapshots: source-map-url: 0.4.1 urix: 0.1.0 + source-map-resolve@0.6.0: + dependencies: + atob: 2.1.2 + decode-uri-component: 0.2.2 + source-map-support@0.5.13: dependencies: buffer-from: 1.1.2 diff --git a/src/app.service.ts b/src/app.service.ts index 69e53e6..23cc3cb 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -1,42 +1,9 @@ import { Injectable } from '@nestjs/common'; - -const mqtt = require('mqtt'); -let protocol: String = 'mqtt'; -let host: String = '192.168.2.33'; -let port: String = '1883' -let clientId: String = 'mqttx_20676f4c'; - -let connectUrl: String = protocol+'//'+host+':'+port; - -const client = mqtt.connect({ port: 1883, host: '192.168.2.33', keepalive: 10000}); -/*const client = mqtt.connect(({ port: 1883, host: '192.168.1.100', keepalive: 10000} , { - clientId, - clean: true, - connectTimeout: 4000, - username: "tester", - password: "GreatTester99", - reconnectPeriod: 1000, -});*/ - -let msg: String = 'Connected'; - -client.on('connect', () => { - console.log('Connected'); -}); - -/*client.subscribe('testtopic'); -client.on('message', function (topic, message) { - console.log(message) -})*/ - -client.publish('testtopic', 'hello!'); -client.end(); - @Injectable() export class AppService { getHello(): string { - return 'Hello worlda World!'+ msg; + return 'Hello worlda World!'; } } diff --git a/src/zvksmetrics/zvksmetrics.controller.ts b/src/zvksmetrics/zvksmetrics.controller.ts index 94a2436..5375456 100644 --- a/src/zvksmetrics/zvksmetrics.controller.ts +++ b/src/zvksmetrics/zvksmetrics.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, Post} from '@nestjs/common'; +import { Body, ConfigurableModuleBuilder, Controller, Get, Post} from '@nestjs/common'; import { ZvksmetricsService } from './zvksmetrics.service'; @Controller('api') @@ -12,9 +12,13 @@ export class ZvksmetricsController { @Post('input') - getMtetrics (@Body() dto : string) : string { - console.log(dto); - return dto; + + async getPostMessage (@Body() inputMetrics : any) : Promise { + //console.log(inputMetrics); + //this.metricsService.getMetrics(inputMetrics); + let out: any = await this.metricsService.useStatusModel(inputMetrics); + console.log(out); + return out; } } diff --git a/src/zvksmetrics/zvksmetrics.service.ts b/src/zvksmetrics/zvksmetrics.service.ts index a0f310a..64cd82e 100644 --- a/src/zvksmetrics/zvksmetrics.service.ts +++ b/src/zvksmetrics/zvksmetrics.service.ts @@ -1,4 +1,79 @@ import { Injectable } from '@nestjs/common'; +import axios from 'axios'; +import { response } from 'express'; @Injectable() -export class ZvksmetricsService {} +export class MetricK2 { + id : string; + type : string; + addr : string; + value : any; + description: string; + status: any; +}; + +@Injectable() +export class K2Metrics { + service_name: string; + endpoint_name: string; + metrics: MetricK2 []; +}; + +@Injectable() +export class ZvksmetricsService { + async useStatusModel(inputDate : any) : Promise { + let inp : K2Metrics = new K2Metrics(); + inp = inputDate; + let editedMetrics : K2Metrics = await this.getMetrics (inp); + let responseFromExporter : any = await this.sendMetrics (editedMetrics); + + return responseFromExporter; + } + + async getMetrics(inputDate : K2Metrics): Promise { + + inputDate.metrics.forEach(async element => { + element.status = await this.setMetricsStatus(element); + }); + + return inputDate; + } + + async setMetricsStatus( metric : MetricK2) : Promise { + let stat = Math.floor(Math.random()*4); + // if (stat < 1) { + // stat = 99; + // } + return stat; + } + + async sendMetrics(inputDate : K2Metrics) : Promise { + let resp : any; + let path : string = 'http://192.168.2.34:9049/update'; + let body : any = inputDate; + 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); + + }*/ + + +};