From 7dfb9fa4a25f3f755e7861370d2462dd0d170975 Mon Sep 17 00:00:00 2001 From: DEV-Admin Date: Wed, 28 May 2025 10:03:01 -0400 Subject: [PATCH] Dockerfile added --- Dockerfile | 16 + src/zvksmetrics/conf/complex.json | 264 ++++++++++++++- src/zvksmetrics/conf/ranges.json | 391 +++++++++++++++++++++- src/zvksmetrics/zvksmetrics.controller.ts | 34 +- src/zvksmetrics/zvksmetrics.service.ts | 238 +++++++++---- 5 files changed, 838 insertions(+), 105 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b084731 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM node:20 + +WORKDIR /usr/src/app + +COPY package*.json ./ + +RUN npm install + +COPY . . + +RUN npm run build + +EXPOSE 9999 + +CMD ["npm", "run", "start:dev"] + diff --git a/src/zvksmetrics/conf/complex.json b/src/zvksmetrics/conf/complex.json index 3754ef7..52fb24a 100644 --- a/src/zvksmetrics/conf/complex.json +++ b/src/zvksmetrics/conf/complex.json @@ -1,20 +1,260 @@ [ { - "name": "CPU usage", - "values": "{\"arr\":[191, 192, 193]}", - "formula": "arr[1]+arr[2]+arr[3]" + "id": "10001", + "name": "cpu_utilization", + "values": {"arr":["cpurawidle", "cpurawuser", "cpurawnice", "cpurawsystem", "cpurawwait", "cpurawinterrupt"],"prevarr":["cpurawidle", "cpurawuser", "cpurawnice", "cpurawsystem", "cpurawwait", "cpurawinterrupt"]}, + "formula": "((arr[2]-prevarr[2])+(arr[3]-prevarr[3])+(arr[4]-prevarr[4])+(arr[6]-prevarr[6]))/((arr[1]-prevarr[1])+(arr[2]-prevarr[2])+(arr[3]-prevarr[3])+(arr[4]-prevarr[4])+(arr[5]-prevarr[5])+(arr[6]-prevarr[6]))*100" }, { - "name": "RAM usage", - "values": "{\"arr\":[191, 192, 193]}", - "formula": "arr[1]+arr[2]+arr[3]" + "id": "10002", + "name": "cpu_load_average_1", + "values": {"arr":["cpu1min"]}, + "formula": "arr[1]" }, { - "name": "Hard disks usage", - "values": "{\"arr\":[191, 192, 193]}", - "formula": "arr[1]+arr[2]+arr[3]" + "id": "10003", + "name": "cpu_load_average_5", + "values": {"arr":["cpu5min"]}, + "formula": "arr[1]" + }, + { + "id": "10004", + "name": "cpu_load_average_15", + "values": {"arr":["cpu15min"]}, + "formula": "arr[1]" + }, + { + "id": "10005", + "name": "ram_usage", + "values": {"arr":["totalMemory", "totalMemoryAvail"]}, + "formula": "(arr[1]-arr[2])/arr[1]*100" + }, + { + "id": "10006", + "name": "disk_usage", + "values": {"arr":["totalDiskSize", "useDiskSize"]}, + "formula": "(arr[1]-arr[2])/arr[1]*100" + }, + { + "id": "10007", + "name": "disk_read_rate", + "values": {"arr":["diskReadAccess"], "prevarr":["diskReadAccess"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10008", + "name": "disk_write_rate", + "values": {"arr":["diskWriteAccess"], "prevarr":["diskWriteAccess"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10009", + "name": "network_interface_rx_Eth_1", + "values": {"arr":["speed1", "ifInOctets1"], "prevarr":["speed1", "ifInOctets1"]}, + "formula": "(arr[2]-prevarr[2])/arr[1]*100" + }, + { + "id": "10010", + "name": "network_interface_tx_Eth_1", + "values": {"arr":["speed1", "ifOutOctets1"], "prevarr":["speed1", "ifOutOctets1"]}, + "formula": "(arr[2]-prevarr[2])/arr[1]*100" + }, + { + "id": "10011", + "name": "network_interface_rx_Eth_2", + "values": {"arr":["speed2", "ifInOctets2"], "prevarr":["speed2", "ifInOctets2"]}, + "formula": "(arr[2]-prevarr[2])/arr[1]*100" + }, + { + "id": "10012", + "name": "network_interface_tx_Eth_2", + "values": {"arr":["speed2", "ifOutOctets2"], "prevarr":["speed2", "ifOutOctets2"]}, + "formula": "(arr[2]-prevarr[2])/arr[1]*100" + }, + { + "id": "10013", + "name": "network_interface_rx_Eth_3", + "values": {"arr":["speed3", "ifInOctets3"], "prevarr":["speed3", "ifInOctets3"]}, + "formula": "(arr[2]-prevarr[2])/arr[1]*100" + }, + { + "id": "10014", + "name": "network_interface_tx_Eth_3", + "values": {"arr":["speed3", "ifOutOctets3"], "prevarr":["speed3", "ifOutOctets3"]}, + "formula": "(arr[2]-prevarr[2])/arr[1]*100" + }, + { + "id": "10015", + "name": "network_interface_rx_Eth_4", + "values": {"arr":["speed4", "ifInOctets4"], "prevarr":["speed4", "ifInOctets4"]}, + "formula": "(arr[2]-prevarr[2])/arr[1]*100" + }, + { + "id": "10016", + "name": "network_interface_tx_Eth_4", + "values": {"arr":["speed4", "ifOutOctets4"], "prevarr":["speed4", "ifOutOctets4"]}, + "formula": "(arr[2]-prevarr[2])/arr[1]*100" + }, + { + "id": "10017", + "name": "network_interfaces_read_packets_errors_Eth_1", + "values": {"arr":["ifInErrors1"], "prevarr":["ifInErrors1"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10018", + "name": "network_interfaces_write_packets_errors_Eth_1", + "values": {"arr":["ifOutErrors1"], "prevarr":["ifOutErrors1"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10019", + "name": "network_interfaces_read_packets_errors_Eth_2", + "values": {"arr":["ifInErrors2"], "prevarr":["ifInErrors2"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10020", + "name": "network_interfaces_write_packets_errors_Eth_2", + "values": {"arr":["ifOutErrors2"], "prevarr":["ifOutErrors2"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10021", + "name": "network_interfaces_read_packets_errors_Eth_3", + "values": {"arr":["ifInErrors3"], "prevarr":["ifInErrors3"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10022", + "name": "network_interfaces_write_packets_errors_Eth_3", + "values": {"arr":["ifOutErrors3"], "prevarr":["ifOutErrors3"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10023", + "name": "network_interfaces_read_packets_errors_Eth_4", + "values": {"arr":["ifInErrors4"], "prevarr":["ifInErrors4"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10024", + "name": "network_interfaces_write_packets_errors_Eth_4", + "values": {"arr":["ifOutErrors4"], "prevarr":["ifOutErrors4"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10025", + "name": "network_interfaces_read_packets_discarded_Eth_1", + "values": {"arr":["ifInDiscard1"], "prevarr":["ifInDiscard1"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10026", + "name": "network_interfaces_write_packets_discarded_Eth_1", + "values": {"arr":["ifOutDiscard1"], "prevarr":["ifOutDiscard1"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10027", + "name": "network_interfaces_read_packets_discarded_Eth_2", + "values": {"arr":["ifInDiscard2"], "prevarr":["ifInDiscard2"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10028", + "name": "network_interfaces_write_packets_discarded_Eth_2", + "values": {"arr":["ifOutDiscard2"], "prevarr":["ifOutDiscard2"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10029", + "name": "network_interfaces_read_packets_discarded_Eth_3", + "values": {"arr":["ifInDiscard3"], "prevarr":["ifInDiscard3"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10030", + "name": "network_interfaces_write_packets_discarded_Eth_3", + "values": {"arr":["ifOutDiscard3"], "prevarr":["ifOutDiscard3"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10031", + "name": "network_interfaces_read_packets_discarded_Eth_4", + "values": {"arr":["ifInDiscard4"], "prevarr":["ifInDiscard4"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10032", + "name": "network_interfaces_write_packets_discarded_Eth_4", + "values": {"arr":["ifOutDiscard4"], "prevarr":["ifOutDiscard4"]}, + "formula": "(arr[1]-prevarr[1])" + }, + { + "id": "10033", + "name": "response_icmp", + "values": {"arr":["ping_check"]}, + "formula": "arr[1]" + }, + { + "id": "10034", + "name": "server_uptime", + "values": {"arr":["upTime"]}, + "formula": "arr[1]" + }, + { + "id": "10035", + "name": "total_participants", + "values": {"arr":["TotalParticipants"]}, + "formula": "arr[1]" + }, + { + "id": "10036", + "name": "registered_abonents", + "values": {"arr":["RegisteredAbonents"]}, + "formula": "arr[1]" + }, + { + "id": "10037", + "name": "webcasts_HLS_clients", + "values": {"arr":["WebcastsHLSclients"]}, + "formula": "arr[1]" + }, + { + "id": "10038", + "name": "total_P2P_rooms", + "values": {"arr":["TotalP2Prooms"]}, + "formula": "arr[1]" + }, + { + "id": "10039", + "name": "conferences_total", + "values": {"arr":["Conferencestotal"]}, + "formula": "arr[1]" + }, + { + "id": "10040", + "name": "conferences_active", + "values": {"arr":["ConferencesActive"]}, + "formula": "arr[1]" + }, + { + "id": "10041", + "name": "records_active", + "values": {"arr":["RecordsActive"]}, + "formula": "arr[1]" + }, + { + "id": "10042", + "name": "number_of_records_saved", + "values": {"arr":["NumberRecordsSaved"]}, + "formula": "arr[1]" + }, + { + "id": "10043", + "name": "replication_status", + "values": {"arr":["replicationStatus"]}, + "formula": "arr[1]" } - - - ] \ No newline at end of file diff --git a/src/zvksmetrics/conf/ranges.json b/src/zvksmetrics/conf/ranges.json index 57542f8..680de19 100644 --- a/src/zvksmetrics/conf/ranges.json +++ b/src/zvksmetrics/conf/ranges.json @@ -1,20 +1,385 @@ [ - { - "name": "measure_190", "_comment":"Загрузка процессора за 1 минуту", + { + "name": "cpu_utilization", "ranges": [ - {"min": 0, "max": 0, "status": 3}, - {"min": 0, "max": 0.1, "status": 0}, - {"min": 0.11, "max": 0.12, "status": 1}, - {"min": 0.13, "max": 1, "status": 2}, - {"min": 0.5, "max": 0.8, "status": 1} - ] + {"min": 0, "max": 60, "status": 1}, + {"min": 61, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] }, { - "name": "measure_191", "_comment":"Загрузка процессора за 5 минут", + "name": "cpu_load_average_1", "ranges": [ - {"min": 0, "max": 0.2, "status": 0}, - {"min": 0.21, "max": 0.32, "status": 1}, - {"min": 0.33, "max": 1, "status": 2} - ] + {"min": 0, "max": 60, "status": 1}, + {"min": 61, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "cpu_load_average_5", + "ranges": [ + {"min": 0, "max": 60, "status": 1}, + {"min": 61, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "cpu_load_average_15", + "ranges": [ + {"min": 0, "max": 60, "status": 1}, + {"min": 61, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "ram_usage", + "ranges": [ + {"min": 0, "max": 60, "status": 1}, + {"min": 61, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "disk_usage", + "ranges": [ + {"min": 0, "max": 70, "status": 1}, + {"min": 71, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "disk_read_rate", + "ranges": [ + {"min": 0, "max": 100, "status": 1}, + {"min": 101, "max": 300, "status": 2}, + {"min": 301, "max": 500, "status": 3}, + {"min": 501, "max": 5000, "status": 4} + ] + }, + { + "name": "disk_write_rate", + "ranges": [ + {"min": 0, "max": 100, "status": 1}, + {"min": 101, "max": 300, "status": 2}, + {"min": 301, "max": 500, "status": 3}, + {"min": 501, "max": 5000, "status": 4} + ] + }, + { + "name": "network_interface_rx_Eth_1", + "ranges": [ + {"min": 0, "max": 70, "status": 1}, + {"min": 71, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "network_interface_tx_Eth_1", + "ranges": [ + {"min": 0, "max": 70, "status": 1}, + {"min": 71, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "network_interface_rx_Eth_2", + "ranges": [ + {"min": 0, "max": 70, "status": 1}, + {"min": 71, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "network_interface_tx_Eth_2", + "ranges": [ + {"min": 0, "max": 70, "status": 1}, + {"min": 71, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "network_interface_rx_Eth_3", + "ranges": [ + {"min": 0, "max": 70, "status": 1}, + {"min": 71, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "network_interface_tx_Eth_3", + "ranges": [ + {"min": 0, "max": 70, "status": 1}, + {"min": 71, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "network_interface_rx_Eth_4", + "ranges": [ + {"min": 0, "max": 70, "status": 1}, + {"min": 71, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "network_interface_tx_Eth_4", + "ranges": [ + {"min": 0, "max": 70, "status": 1}, + {"min": 71, "max": 80, "status": 2}, + {"min": 81, "max": 90, "status": 3}, + {"min": 91, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_read_packets_errors_Eth_1", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_write_packets_errors_Eth_1", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_read_packets_errors_Eth_2", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_write_packets_errors_Eth_2", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_read_packets_errors_Eth_3", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_write_packets_errors_Eth_3", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_read_packets_errors_Eth_4", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_write_packets_errors_Eth_4", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_read_packets_discarded_Eth_1", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_write_packets_discarded_Eth_1", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_read_packets_discarded_Eth_2", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_write_packets_discarded_Eth_2", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_read_packets_discarded_Eth_3", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_write_packets_discarded_Eth_3", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_read_packets_discarded_Eth_4", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "network_interfaces_write_packets_discarded_Eth_4", + "ranges": [ + {"min": 0, "max": 1, "status": 1}, + {"min": 2, "max": 10, "status": 2}, + {"min": 11, "max": 20, "status": 3}, + {"min": 21, "max": 100, "status": 4} + ] + }, + { + "name": "response_icmp", + "ranges": [ + {"min": 0, "max": 100, "status": 1}, + {"min": 101, "max": 200, "status": 2}, + {"min": 201, "max": 300, "status": 3}, + {"min": 301, "max": 1000, "status": 4} + ] + }, + { + "name": "server_uptime", + "ranges": [ + {"min": 0, "max": 0, "status": 4}, + {"min": 1, "max": 1000, "status": 1} + ] + }, + { + "name": "total_participants", + "ranges": [ + {"min": 0, "max": 10, "status": 1}, + {"min": 11, "max": 30, "status": 2}, + {"min": 31, "max": 50, "status": 3}, + {"min": 51, "max": 1000, "status": 4} + ] + }, + { + "name": "registered_abonents", + "ranges": [ + {"min": 0, "max": 10, "status": 1}, + {"min": 11, "max": 30, "status": 2}, + {"min": 31, "max": 50, "status": 3}, + {"min": 51, "max": 1000, "status": 4} + ] + }, + { + "name": "webcasts_HLS_clients", + "ranges": [ + {"min": 0, "max": 10, "status": 1}, + {"min": 11, "max": 30, "status": 2}, + {"min": 31, "max": 50, "status": 3}, + {"min": 51, "max": 1000, "status": 4} + ] + }, + { + "name": "total_P2P_rooms", + "ranges": [ + {"min": 0, "max": 10, "status": 1}, + {"min": 11, "max": 30, "status": 2}, + {"min": 31, "max": 50, "status": 3}, + {"min": 51, "max": 1000, "status": 4} + ] + }, + { + "name": "conferences_total", + "ranges": [ + {"min": 0, "max": 10, "status": 1}, + {"min": 11, "max": 30, "status": 2}, + {"min": 31, "max": 50, "status": 3}, + {"min": 51, "max": 1000, "status": 4} + ] + }, + { + "name": "conferences_active", + "ranges": [ + {"min": 0, "max": 10, "status": 1}, + {"min": 11, "max": 30, "status": 2}, + {"min": 31, "max": 50, "status": 3}, + {"min": 51, "max": 1000, "status": 4} + ] + }, + { + "name": "records_active", + "ranges": [ + {"min": 0, "max": 10, "status": 1}, + {"min": 11, "max": 30, "status": 2}, + {"min": 31, "max": 50, "status": 3}, + {"min": 51, "max": 1000, "status": 4} + ] + }, + { + "name": "number_of_records_saved", + "ranges": [ + {"min": 0, "max": 10, "status": 1}, + {"min": 11, "max": 30, "status": 2}, + {"min": 31, "max": 50, "status": 3}, + {"min": 51, "max": 1000, "status": 4} + ] + }, + { + "name": "replication_status", + "ranges": [ + {"min": 0, "max": 0, "status": 4}, + {"min": 1, "max": 1, "status": 1} + ] } ] \ No newline at end of file diff --git a/src/zvksmetrics/zvksmetrics.controller.ts b/src/zvksmetrics/zvksmetrics.controller.ts index 84d880b..1ed265d 100644 --- a/src/zvksmetrics/zvksmetrics.controller.ts +++ b/src/zvksmetrics/zvksmetrics.controller.ts @@ -1,26 +1,26 @@ -import { Body, ConfigurableModuleBuilder, Controller, Get, Options, Param, ParseIntPipe, 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) {} - + constructor(private readonly metricsService: ZvksmetricsService) { } + @Get('ranges') async getHello(): Promise { let a: number = 7; let b: number = 9; - let sum: any = {value: (a+b)/4*b+a}; - + let sum: any = { value: (a + b) / 4 * b + a }; + await this.metricsService.useStatusModel(1); // let a = await this.metricsService.showStatus(); - console.log(sum.value); + // console.log(sum.value); return sum.value; } //Заглушка для будующей API под граничные значения @Options('ranges/:code') - async showRanges (@Param('code', ParseIntPipe) code: number): Promise{ - let ret : JSON = JSON.parse('{"result":"null"}'); - if(code === 9999){ + async showRanges(@Param('code', ParseIntPipe) code: number): Promise { + let ret: JSON = JSON.parse('{"result":"null"}'); + if (code === 9999) { return await this.metricsService.showRangesOptions('File Name'); } return ret; @@ -28,19 +28,21 @@ export class ZvksmetricsController { //Заглушка для будующей API под комплексные значения метрик @Options('complex/:code') - async showComplexMetrics (@Param('code', ParseIntPipe) code: number): Promise{ - let ret : JSON = JSON.parse('{"result":"null"}'); - if(code === 5555){ + async showComplexMetrics(@Param('code', ParseIntPipe) code: number): Promise { + let ret: JSON = JSON.parse('{"result":"null"}'); + if (code === 5555) { return await this.metricsService.showComplexMetricsConfig('File Name'); } return ret; } @Post('input') - async getPostMessage (@Body() inputMetrics : any) : Promise { - //console.log(inputMetrics); + async getPostMessage(@Body() inputMetrics: any): Promise { + // console.log(inputMetrics); + // console.log(23+12); + console.log(inputMetrics); let out: any = await this.metricsService.useStatusModel(inputMetrics); return out; - } - + } + } diff --git a/src/zvksmetrics/zvksmetrics.service.ts b/src/zvksmetrics/zvksmetrics.service.ts index f9066a2..e41f593 100644 --- a/src/zvksmetrics/zvksmetrics.service.ts +++ b/src/zvksmetrics/zvksmetrics.service.ts @@ -3,24 +3,27 @@ import axios, { Axios, AxiosResponse } from 'axios'; import { readFile } from 'node:fs/promises'; import { response } from 'express'; import { json } from 'node:stream/consumers'; -import math, { create, all, evaluate } from 'mathjs' +import math, { create, all, evaluate, number, random, complex, string } from 'mathjs' @Injectable() export class MetricK2 { - id : string; - type : string; - addr : string; - value : number; + id: string; + name: string; + type: string; + addr: string; + value: number; description: string; status: number; + device: number; + source: string; }; @Injectable() export class K2Metrics { service_name: string; - endpoint_name: string; - metrics: MetricK2 []; + //endpoint_name: string; + metrics: MetricK2[]; }; @Injectable() @@ -28,26 +31,62 @@ export class RangeValues { min: number; max: number; status: number; -}; +}; @Injectable() export class MetricsRanges { name: string; - ranges: RangeValues []; + ranges: RangeValues[]; }; +@Injectable() +export class ComplexMetricsCalculation { + id: string; + name: string; + values: string; + formula: string; +}; + +var prev: K2Metrics; //Prevous K2 @Injectable() export class ZvksmetricsService { - async useStatusModel(inputData : any) : Promise { - let inp : K2Metrics = new K2Metrics(); + async useStatusModel(inputData: any): Promise { + let inp: K2Metrics = new K2Metrics(); inp = inputData; - let scope : MetricsRanges [] = []; - scope = await this.setMetricsRanges (); - + let complex: K2Metrics = new K2Metrics(); + complex.service_name = inputData.service_name; + complex.metrics = []; + let model: ComplexMetricsCalculation[] = []; + model = await this.setComplexMetrics(); + + let scope: MetricsRanges[] = []; + scope = await this.setMetricsRanges(); + + if (!prev) { + prev = inp; + } + // console.log(prev); + + let intermediate = await this.getComplexMetricsValues(inp, prev, complex, model, scope); + console.log(inp); + + // console.log(inp.metrics[20], complex.metrics[2]); + + const val = JSON.parse(JSON.stringify(model[0].values)); + // console.log(val.arr[1]); + // console.log(val); + + // const reif = JSON.parse(comlexCalc[1].values); + // console.log(reif); + + + + + // console.log(inp); // let rng: any = {val: [{x:3}, {x:4}, {x:5}, {x:9}]}; // console.log(rng.val[0].x); // let testFormula: string = (rng.val[0].x + rng.val[1].x - rng.val[2].x + rng.val[3].x).toString(); @@ -55,26 +94,33 @@ export class ZvksmetricsService { // let formula: string = scope[0].ranges[0].sum; // let value: any = {formula}; // console.log(evaluate(testFormula, rng)); - + // console.log(JSON.stringify("arr:[191, 192, 193]")); - // let x: any = {arr:[191, 192, 193], sum: 'arr[1]+arr[2]+arr[3]'}; - // let x: any = JSON.parse(scope[0].ranges[0].values); - // let y: string = scope[0].ranges[0].formula; + let x: any = { arr: ["191", "192", "193"], prev: ["1", "1"], abb: ["1", "34"] }; + //let k = JSON.parse(scope[0].ranges[0].values); // + //let y: string = scope[0].ranges[0].formula; + let y = "arr[1]/prev[1]*abb[1]+arr[2]*prev[2]+arr[3]+prev[1]/prev[2]"; // console.log(y, x); // console.log(evaluate(y, x)); - let editedMetrics : K2Metrics = await this.getMetrics (inp, scope); - let responseFromExporter : any = await this.sendMetrics (editedMetrics); + // let editedMetrics : K2Metrics = await this.getMetrics (inp, comlexCalc, scope); + let responseFromExporter: any = await this.sendMetrics(intermediate); + let responseFromExporter1: any = await this.sendMetrics(inp); + + // console.log(inp.metrics[200].device); + + prev = inp; return responseFromExporter; - } - - async getMetrics(inputData : K2Metrics, scope: MetricsRanges []): Promise { - let arr : MetricK2 [] = inputData.metrics; - let length : number = arr.length; - for(let i=0; i { + let arr: MetricK2[] = inputData.metrics; + let length: number = arr.length; + + for (let i = 0; i < length; i++) { arr[i].status = await this.setMetricsStatus(arr[i], scope); } inputData.metrics = arr; @@ -82,8 +128,57 @@ export class ZvksmetricsService { return inputData; } - async setMetricsStatus( metric : MetricK2, scope: MetricsRanges []) : Promise { - if (metric && scope){ + async getComplexMetricsValues(inputData: K2Metrics, prevousData: K2Metrics, complexData: K2Metrics, complexCalc: ComplexMetricsCalculation[], scope: MetricsRanges[]): Promise { + let length: number = complexCalc.length; + let metrics: MetricK2[] = complexData.metrics; + + // let val = await this.setComplexMetricValue(complexCalc[0].values, complexCalc[0].formula, inputData.metrics, prevousData.metrics); + // let status = 3; + + for (let i = 0; i < length; i++) { + let val = await this.setComplexMetricValue(complexCalc[i].values, complexCalc[i].formula, inputData.metrics, prevousData.metrics); + let status = 3; + let complexMetric = new MetricK2(); + complexMetric.id = complexCalc[i].id; + complexMetric.name = complexCalc[i].name; + complexMetric.type = inputData.metrics[0].type; + complexMetric.addr = inputData.metrics[0].addr; + complexMetric.value = val; + complexMetric.description = complexCalc[i].name; + complexMetric.status = status; + // complexMetric.device = number(inputData.metrics.find(el => el.name == complexMetric.name)?.device); + // complexMetric.device = inputData.metrics[0].device; + // complexMetric.source = string(inputData.metrics.find(elem => elem.name == complexMetric.name)?.source); + metrics.push(complexMetric); + } + + console.log(complexData); + + return complexData; + } + + async setComplexMetricValue(value: string, formula: string, currMetrics: MetricK2[], prevMetrics: MetricK2[]): Promise { + const val = JSON.parse(JSON.stringify(value)); + let arr = val.arr; + let arr_len: number = arr.length; + for (let i = 0; i < arr_len; i++) { + arr[i] = currMetrics.find(element => element.name == arr[i])?.value; + } + let prevarr = val.prevarr; + if (prevarr) { + let prevarr_len: number = prevarr.length; + for (let i = 0; i < prevarr_len; i++) { + prevarr[i] = prevMetrics.find(element => element.name == prevarr[i])?.value; + } + } + // console.log(val); + let result: number = evaluate(formula, val); + // console.log(result); + return result; + } + + async setMetricsStatus(metric: MetricK2, scope: MetricsRanges[]): Promise { + if (metric && scope) { return await this.showStatus(scope, metric.id, metric.value); } else { @@ -91,78 +186,93 @@ export class ZvksmetricsService { } } - async sendMetrics(inputData : K2Metrics) : Promise { - let resp : any; - let path : string = 'http://192.168.2.34:9049/update'; - let body : any = inputData; - let options : any = { - headers: {'Content-Type' : 'application/json'} - }; + async sendMetrics(inputData: K2Metrics): Promise { + let resp: any; + let path: string = 'http://192.168.2.34:9050/update'; + let body: any = inputData; + let options: any = { + headers: { 'Content-Type': 'application/json' } + }; - await axios.post(path, body, options).then((response)=>{ + await axios.post(path, body, options).then((response) => { resp = response.status + ' ' + response.data; // console.log(resp); - }).catch((error)=>{ + }).catch((error) => { resp = error; }); return resp; } - async showStatus(scope: MetricsRanges [], metricName: string, metricValue: number) : Promise { - let status : number = 0; - let test : any = scope.find(element => element.name == metricName); - if (test){ - let range : MetricsRanges = test; - let transit : any = range.ranges.find(element => element.min <= metricValue && metricValue <= element.max)?.status; - if(transit){ + async showStatus(scope: MetricsRanges[], metricName: string, metricValue: number): Promise { + let status: number = 0; + let test: any = scope.find(element => element.name == metricName); + if (test) { + let range: MetricsRanges = test; + let transit: any = range.ranges.find(element => element.min <= metricValue && metricValue <= element.max)?.status; + if (transit) { status = transit; return status; } - else{ - return 4; + else { + return 0; } } return status; } - - async setMetricsRanges (@Body() rangesData : any = null) : Promise { - let url : string = 'http://192.168.2.39:9999/api/ranges/9999'; - let options : any = { - headers: {'Content-Type' : 'application/json'} + + async setMetricsRanges(@Body() rangesData: any = null): Promise { + let url: string = 'http://192.168.2.39:9999/api/ranges/9999'; + let options: any = { + headers: { 'Content-Type': 'application/json' } }; await axios.options(url, options).then((response) => { rangesData = response.data; - }).catch((error)=>{ + }).catch((error) => { rangesData = error; }); - + // console.log(rangesData); return rangesData; } - - async showRangesOptions(filePath: string) : Promise { - let content : JSON = JSON.parse('{"name":"name"}'); + + async setComplexMetrics(@Body() complexData: any = null): Promise { + let url: string = 'http://192.168.2.39:9999/api/complex/5555'; + let options: any = { + headers: { 'Content-Type': 'application/json' } + }; + + await axios.options(url, options).then((response) => { + complexData = response.data; + }).catch((error) => { + complexData = error; + }); + // console.log(rangesData); + return complexData; + } + + async showRangesOptions(filePath: string): Promise { + let content: JSON = JSON.parse('{"name":"name"}'); filePath = './src/zvksmetrics/conf/ranges.json'; try { content = JSON.parse(await readFile(filePath, { encoding: 'utf8' })); - } - catch { + } + catch { console.log('Error read file'); - } + } return content; } - async showComplexMetricsConfig(filePath: string) : Promise { - let content : JSON = JSON.parse('{"name":"name"}'); + async showComplexMetricsConfig(filePath: string): Promise { + let content: JSON = JSON.parse('{"name":"name"}'); filePath = './src/zvksmetrics/conf/complex.json'; try { content = JSON.parse(await readFile(filePath, { encoding: 'utf8' })); - } - catch { + } + catch { console.log('Error read file'); - } + } return content; }