Skip to content

Commit

Permalink
fix: deviceMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio1988 committed Nov 29, 2023
1 parent 412e5a3 commit f074ac2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
9 changes: 6 additions & 3 deletions packages/connections/src/lib/controllerConnection.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { EventEmitter } from 'events';
import { WebSocket } from 'ws';
import { Logger } from 'winston';
import { WebSocket } from 'ws';
import { DeviceWorkerConnection } from './deviceWorkerConnection';
import { DTO } from './utils/type';
import { RotomProtos } from './utils/mitmProto';
import { DTO } from './utils/type';
import MitmRequest = RotomProtos.MitmRequest;
//import MitmCommand = RotomProtos.MitmCommand;

let instanceNo = 0;

export type ControllerConnectionDTO = Omit<DTO<ControllerConnection>, 'ws' | 'log' | 'heartbeatHandle' | 'deviceWorkerConnection'>;
export type ControllerConnectionDTO = Omit<
DTO<ControllerConnection>,
'ws' | 'log' | 'heartbeatHandle' | 'deviceWorkerConnection'
>;

export class ControllerConnection extends EventEmitter {
_device_disconnect_handler: () => void;
Expand Down
4 changes: 2 additions & 2 deletions packages/connections/src/lib/deviceControlConnection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';
import { WebSocket } from 'ws';
import { Logger } from 'winston';
import { WebSocket } from 'ws';

import { DTO } from './utils/type';

Expand All @@ -23,7 +23,7 @@ export type DeviceControlDTO = Omit<DTO<DeviceControlConnection>, 'ws' | 'log' |

interface MemoryStatus {
memFree: number;
//DEPRECATED
/**@deprecated */
memMitm: number;
memDevice: number;
memStart: number;
Expand Down
39 changes: 24 additions & 15 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
process.title = 'Rotom';
import { config } from '@rotom/config';
import { ControllerConnection, DeviceControlConnection, DeviceWorkerConnection } from '@rotom/connections';
import { JobsDTO, JobsStatusDTO, StatusDTO, WorkerDTO } from '@rotom/types';
import { FastifyInstance } from 'fastify';
import { inspect } from 'util';
import { DeviceWorkerConnection, ControllerConnection, DeviceControlConnection } from '@rotom/connections';
import { WebSocketServer } from 'ws';
import { StatusDTO, WorkerDTO, JobsDTO, JobsStatusDTO } from '@rotom/types';
import {
promRegistry,
workersGauge,
devicesGauge,
deviceMemory,
deviceMemoryFree,
deviceMemoryMitm,
deviceMemoryStart,
devicesGauge,
promRegistry,
valueOrZero,
workersGauge,
} from './utils';

import { JobExecutor } from './jobExecutor';
import { jobs, JobLoader } from './jobLoader';
import { JobLoader, jobs } from './jobLoader';
import { log } from './logger';
import { startWebserver, fastify } from './webserver';
import { fastify, startWebserver } from './webserver';
//import fa from '@faker-js/faker/locales/fa';

/* Initialise websocket server from Mitm */
const wssMitm = new WebSocketServer({ port: config.deviceListener.port, perMessageDeflate: false });

const controlConnections: Record<string, DeviceControlConnection> = {};
const currentConnections: Record<string, { deviceWorker: DeviceWorkerConnection; controller: ControllerConnection | null }> = {};
const currentConnections: Record<
string,
{ deviceWorker: DeviceWorkerConnection; controller: ControllerConnection | null }
> = {};
const unallocatedConnections: string[] = [];
const deviceInformation: Record<string, { lastControllerConnection: number }> = {};

Expand Down Expand Up @@ -230,7 +234,9 @@ wssScanner.on('connection', (ws, req) => {
} else {
const mainDeviceInfo = deviceInformation[mainDeviceId];
if (!mainDeviceInfo) {
log.info(`CONTROLLER: Warning - found ${nextSpareWorkerId} in pool with no record of main device ${mainDeviceId}`);
log.info(
`CONTROLLER: Warning - found ${nextSpareWorkerId} in pool with no record of main device ${mainDeviceId}`,
);
unallocatedConnections.push(nextSpareWorkerId);
nextSpareWorkerId = unallocatedConnections.shift() as string;
} else {
Expand Down Expand Up @@ -322,12 +328,15 @@ setInterval(() => {
Object.entries(controlConnections).forEach(([, connection]) => {
const origin = connection?.origin || 'Unknown';
connectedDevices += 1;
const { memMitm, memDevice, memFree, memStart } = connection.lastMemory;

const validMemFree = Number.isFinite(connection.lastMemory.memFree) ? connection.lastMemory.memFree : 0;
const validMemFree = valueOrZero(memFree);
deviceMemoryFree.labels(origin).set(validMemFree);
const validMemMitm = Number.isFinite(connection.lastMemory.memMitm) ? connection.lastMemory.memMitm : 0;
deviceMemoryMitm.labels(origin).set(validMemMitm);
const validMemStart = Number.isFinite(connection.lastMemory.memStart) ? connection.lastMemory.memStart : 0;

const validMemDevice = valueOrZero(memDevice || memMitm);
deviceMemory.labels(origin).set(validMemDevice);

const validMemStart = valueOrZero(memStart);
deviceMemoryStart.labels(origin).set(validMemStart);
});
// set number of active devices (couldn't get it correct with add/removal signals ; missed some decrement)
Expand Down Expand Up @@ -412,7 +421,7 @@ const routes = async (fastifyInstance: FastifyInstance) => {
const isAllocated = !unallocatedConnections.includes(workerId);

const deviceId =
connection.mitm?.deviceId ??
connection.deviceWorker?.deviceId ??
Object.keys(controlConnections).find((deviceId) => workerId.startsWith(deviceId));

return {
Expand Down
13 changes: 10 additions & 3 deletions packages/server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export const deviceMemoryFree = new Gauge({
registers: [promRegistry],
});

export const deviceMemoryMitm = new Gauge({
name: 'device_memory_mitm',
help: 'Device Memory MITM',
export const deviceMemory = new Gauge({
name: 'device_memory',
help: 'Device Memory',
labelNames: ['origin'],
registers: [promRegistry],
});
Expand All @@ -36,3 +36,10 @@ export const deviceMemoryStart = new Gauge({
labelNames: ['origin'],
registers: [promRegistry],
});

export function valueOrZero(value?: number): number {
if(value===undefined){
return 0;
}
return Number.isNaN(value) ? 0 : value;
}

0 comments on commit f074ac2

Please sign in to comment.