Skip to content

Commit

Permalink
wip: socketio backend changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tabarra committed Nov 23, 2023
1 parent 84bfcc9 commit 4b2849f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 88 deletions.
2 changes: 1 addition & 1 deletion core/components/HealthMonitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class HealthMonitor {

//================================================================
setCurrentStatus(newStatus) {
if(newStatus !== this.currentStatus){
if (newStatus !== this.currentStatus) {
this.currentStatus = newStatus;
globals.discordBot.updateStatus().catch((e) => {});
globals.webServer?.webSocket.pushRefresh('status');
Expand Down
102 changes: 15 additions & 87 deletions core/components/WebServer/wsRooms/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,26 @@ const modulename = 'SocketRoom:Status';
import TxAdmin from "@core/txAdmin";
import { RoomType } from "../webSocket";
import consoleFactory from '@extras/console';
import { GlobalStatusType } from "@shared/socketioTypes";
const console = consoleFactory(modulename);


/**
* Returns the Discord Bot data
*/
const prepareDiscordStatus = (txAdmin: TxAdmin) => {
const wsStatus = txAdmin.discordBot.wsStatus;
const statusCodes = [
['READY', 'success'],
['CONNECTING', 'warning'],
['RECONNECTING', 'warning'],
['IDLE', 'warning'],
['NEARLY', 'warning'],
['DISCONNECTED', 'danger'],
['WAITING_FOR_GUILDS', 'warning'],
['IDENTIFYING', 'warning'],
['RESUMING', 'warning'],
];

if (wsStatus === false) {
return {
status: 'DISABLED',
statusClass: 'secondary',
};
} else if (statusCodes[wsStatus]) {
return {
status: statusCodes[wsStatus][0],
statusClass: statusCodes[wsStatus][1],
};
} else {
return {
status: 'UNKNOWN',
statusClass: 'danger',
};
}
}


/**
* Returns the fxserver's data
*/
const prepareServerStatus = (txAdmin: TxAdmin) => {
const out = {
mutex: txAdmin.fxRunner?.currentMutex,
status: txAdmin.healthMonitor.currentStatus || '??',
process: txAdmin.fxRunner.getStatus(),
name: txAdmin.globalConfig.serverName,
players: txAdmin.playerlistManager.onlineCount,
scheduler: txAdmin.scheduler.getStatus(),
statusClass: 'dark',
};

if (out.status == 'ONLINE') {
out.statusClass = 'success';
} else if (out.status == 'PARTIAL') {
out.statusClass = 'warning';
} else if (out.status == 'OFFLINE') {
out.statusClass = 'danger';
} else {
out.statusClass = 'dark';
}

return out;
}


/**
* Returns the host's usage
*/
const prepareHostData = (txAdmin: TxAdmin) => {
const stats = txAdmin.healthMonitor.hostStats;
if (!stats) {
return {
memory: { pct: 0, text: 'not available' },
cpu: { pct: 0, text: 'not available' },
};
} else {
return {
memory: {
pct: stats.memory.usage,
text: `${stats.memory.usage}% (${stats.memory.used.toFixed(2)}/${stats.memory.total.toFixed(2)} GB)`,
},
cpu: {
pct: stats.cpu.usage,
text: `${stats.cpu.usage}% of ${stats.cpu.count}x ${stats.cpu.speed} MHz`,
},
};
const getinitialData = (txAdmin: TxAdmin): GlobalStatusType => {
return {
// @ts-ignore simplifying the status enum to a string
discord: txAdmin.discordBot.wsStatus, //no push events, only passively updated
server: {
mutex: txAdmin.fxRunner?.currentMutex,
status: txAdmin.healthMonitor.currentStatus || '??',
process: txAdmin.fxRunner.getStatus(),
name: txAdmin.globalConfig.serverName,
players: txAdmin.playerlistManager.onlineCount,
// @ts-ignore scheduler type narrowing id wrong because cant use "as const" in javascript
scheduler: txAdmin.scheduler.getStatus(), //no push events, only passively updated
},
}
}

Expand All @@ -109,10 +41,6 @@ export default (txAdmin: TxAdmin): RoomType => ({
cumulativeBuffer: false,
outBuffer: null,
initialData: () => {
return {
discord: prepareDiscordStatus(txAdmin), //passive update when HostData updates
host: prepareHostData(txAdmin), //push
server: prepareServerStatus(txAdmin),
};
return getinitialData(txAdmin);
},
})
19 changes: 19 additions & 0 deletions shared/socketioTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type GlobalStatusType = {
discord: false | string;
server: {
mutex: string | null;
status: string;
process: string;
name: string;
players: number;
};
scheduler: {
nextRelativeMs: number;
nextSkip: boolean;
nextIsTemp: boolean;
} | {
nextRelativeMs: false;
nextSkip: false;
nextIsTemp: false;
};
}
1 change: 1 addition & 0 deletions web/public/js/txadmin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ const getSocket = (rooms) => {
}

const startMainSocket = () => {
return;
const rooms = isWebInterface ? ['status', 'playerlist'] : ['status'];
const socket = getSocket(rooms);
socket.on('error', (error) => {
Expand Down

0 comments on commit 4b2849f

Please sign in to comment.