Додан моніторінг застосунку
Додани веб компоненти карточок територій та повідомлень
This commit is contained in:
13
ws/middleware/pushToMetrics.js
Normal file
13
ws/middleware/pushToMetrics.js
Normal file
@@ -0,0 +1,13 @@
|
||||
async function pushToMetrics(metric) {
|
||||
if (!metric || !metric.type) return;
|
||||
|
||||
const payload = { ...metric, timestamp: Date.now() };
|
||||
|
||||
fetch("http://metrics:4005/push", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload)
|
||||
}).catch(err => console.error("Metrics push error:", err));
|
||||
}
|
||||
|
||||
module.exports = { pushToMetrics };
|
||||
@@ -2,9 +2,16 @@ const { updateApartment } = require("../services/apartments.service");
|
||||
const { updateBuilding } = require("../services/buildings.service");
|
||||
const { lockingStand, unlockingStand, updateStand } = require("../services/stand.service");
|
||||
const { broadcast } = require("../utils/broadcaster");
|
||||
const { pushToMetrics } = require("../middleware/pushToMetrics");
|
||||
|
||||
module.exports = async (wss, ws, message) => {
|
||||
try {
|
||||
pushToMetrics({
|
||||
type: "ws_out",
|
||||
length: message.length,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
switch (message.type) {
|
||||
case "apartment":
|
||||
await updateApartment(ws.user, message.data);
|
||||
|
||||
15
ws/ws.js
15
ws/ws.js
@@ -2,6 +2,7 @@ const WebSocket = require("ws");
|
||||
const { URL } = require('url');
|
||||
const { routeMessage } = require("./routes");
|
||||
const { auth } = require("./middleware/auth");
|
||||
const { pushToMetrics } = require("./middleware/pushToMetrics");
|
||||
const { setupPing } = require("./utils/ping");
|
||||
require("dotenv").config();
|
||||
|
||||
@@ -28,10 +29,19 @@ wss.on("connection", async (ws, request) => {
|
||||
ws.user = user;
|
||||
ws.send(JSON.stringify({ connection: "success", api_version, user: {name: ws.user.name, id: ws.user.id } }));
|
||||
|
||||
pushToMetrics({ type: "connection_status", status: "online", api_version, user: {name: ws.user.name, id: ws.user.id } });
|
||||
|
||||
// Periodic ping to maintain a connection
|
||||
setupPing(ws);
|
||||
|
||||
ws.on("message", (raw) => {
|
||||
|
||||
pushToMetrics({
|
||||
type: "ws_in",
|
||||
length: raw.length,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
try {
|
||||
const message = JSON.parse(raw);
|
||||
routeMessage(wss, ws, message);
|
||||
@@ -41,7 +51,10 @@ wss.on("connection", async (ws, request) => {
|
||||
}
|
||||
});
|
||||
|
||||
ws.on("close", () => console.log("🔌 Client disconnected"));
|
||||
ws.on("close", () => {
|
||||
console.log("🔌 Client disconnected");
|
||||
pushToMetrics({ type: "connection_status", status: "offline" });
|
||||
});
|
||||
ws.on("error", (err) => console.error("❌ WS error:", err));
|
||||
} catch (err) {
|
||||
console.error("❌ Auth error:", err);
|
||||
|
||||
Reference in New Issue
Block a user