Skip to content

Commit

Permalink
✔ Stats Server Moved As Client Event Request Only
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed May 21, 2023
1 parent 7714f1c commit 78e77ab
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 65 deletions.
2 changes: 1 addition & 1 deletion dist/fansubid/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<noscript>🎉 Harap Menyalakan JavaScript Untuk Dapat Mengakses Website Ini ✨</noscript>
</div>
<app-root></app-root>
<script src="runtime.416267b4728342bd.js" type="module"></script><script src="polyfills.325a71d3e423341a.js" type="module"></script><script src="scripts.03e7a8120a25f41a.js" defer></script><script src="main.9219daacd31d1656.js" type="module"></script>
<script src="runtime.416267b4728342bd.js" type="module"></script><script src="polyfills.325a71d3e423341a.js" type="module"></script><script src="scripts.03e7a8120a25f41a.js" defer></script><script src="main.4ad9885133703541.js" type="module"></script>


</body></html>
1 change: 1 addition & 0 deletions dist/fansubid/browser/main.4ad9885133703541.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dist/fansubid/browser/main.9219daacd31d1656.js

This file was deleted.

8 changes: 4 additions & 4 deletions dist/fansubid/browser/ngsw.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"configVersion": 1,
"timestamp": 1684603578404,
"timestamp": 1684654254204,
"index": "/index.html",
"assetGroups": [
{
Expand Down Expand Up @@ -41,7 +41,7 @@
"/common.1ac374edf29bcb18.js",
"/favicon.ico",
"/index.html",
"/main.9219daacd31d1656.js",
"/main.4ad9885133703541.js",
"/manifest.webmanifest",
"/polyfills.325a71d3e423341a.js",
"/runtime.416267b4728342bd.js",
Expand Down Expand Up @@ -114,8 +114,8 @@
"/960.7bac784b7ed9c6de.js": "0d18ab2675b6dd13a83cfa481215b7e8aa33f5f8",
"/common.1ac374edf29bcb18.js": "1972ca7d76be821efc66593d17996f04787660d2",
"/favicon.ico": "071facb8fab2e4b3493dcfbb0b02d7bd21bca97e",
"/index.html": "26a10ee08e93f4aface9c396f8aab76bb1064cf6",
"/main.9219daacd31d1656.js": "f4f82e2ef9dcb7af69d7dac830f90bcd6b43ea4a",
"/index.html": "911461111f30a907df1577bec6dd55260ee3f2bd",
"/main.4ad9885133703541.js": "d2b970ab45aa5920c64e56b2a2e1c43557e16669",
"/manifest.webmanifest": "eafb5426cdc9fd714787e5453af315b9972875a3",
"/polyfills.325a71d3e423341a.js": "18f8a8b77e7d11be1385d438c6d218cc68c0edb3",
"/runtime.416267b4728342bd.js": "d2e6f049940adc905bea6fbeb0e62a8f872f5fc8",
Expand Down
2 changes: 1 addition & 1 deletion dist/fansubid/server/main.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/api/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ import { CryptoService } from './services/crypto.service';
import { DiscordService } from './services/discord.service';
import { GdriveService } from './services/gdrive.service';
import { GlobalService } from './services/global.service';
import { IdCloudHostService } from './services/idcloudhost.service';
import { MailService } from './services/mail.service';
import { MkvExtractService } from './services/mkv-extract.service';
import { QuizService } from './services/quiz.service';
import { SocketIoService } from './services/socket-io.service';

import { RssFeedTasksService } from './scheduler/rss-feed-tasks.service';
import { TrackerStatisticsService } from './scheduler/tracker-statistics-tasks.service';
import { StatsServerService } from './scheduler/stats-server-tasks.service';

import { AnimeService } from './repository/anime.service';
import { ApiKeyService } from './repository/api-key.service';
Expand Down Expand Up @@ -258,14 +258,14 @@ import { UserService } from './repository/user.service';
DiscordService,
GdriveService,
GlobalService,
IdCloudHostService,
MkvExtractService,
MailService,
QuizService,
SocketIoService,
// Service Task Schedulers
RssFeedTasksService,
TrackerStatisticsService,
StatsServerService,
// Service Entities
AnimeService,
ApiKeyService,
Expand Down
9 changes: 7 additions & 2 deletions src/api/gateways/socket-io.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Equal, ILike, IsNull } from 'typeorm';
import { CONSTANTS } from '../../constants';

import { RoleModel } from '../../models/req-res.model';
import { CallbackModel, PayloadModel, PingPongModel, RoomInfoModel } from '../../models/socket-io.model';
import { CallbackModel, PayloadModel, PingPongModel, RoomInfoModel, StatsServerModel } from '../../models/socket-io.model';

import { ConfigService } from '../services/config.service';
import { GlobalService } from '../services/global.service';
Expand Down Expand Up @@ -65,7 +65,12 @@ export class SocketIoGateway implements OnGatewayInit, OnGatewayConnection, OnGa
return {
github: this.cfg.github,
server: this.cfg.serverGet()
}
};
}

@SubscribeMessage('stats-server')
statsServer(client: Socket, payload: PayloadModel): StatsServerModel {
return this.cfg.statsServer;
}

@SubscribeMessage('server-set')
Expand Down
10 changes: 9 additions & 1 deletion src/api/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';

import { environment } from '../../environments/api/environment';

import { ServerInfoModel } from '../../models/socket-io.model';
import { ServerInfoModel, StatsServerModel } from '../../models/socket-io.model';

import { GlobalService } from './global.service';

Expand All @@ -20,6 +20,14 @@ export class ConfigService {

github = null;

statsServer: StatsServerModel = {
cpus: 0,
mem_ram: 0,
disk_io: 0,
net_tx: 0,
net_rx: 0
};

settings: ServerInfoModel = {
isMaintenance: false,
winboxOpenLink: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,16 @@
import { WebSocket } from 'ws';

import { Injectable } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';

import { CONSTANTS } from '../../constants';

import { environment } from '../../environments/api/environment';

import { GlobalService } from '../services/global.service';
import { SocketIoService } from '../services/socket-io.service';
import { GlobalService } from './global.service';
import { ConfigService } from '../services/config.service';

@Injectable()
export class StatsServerService {

statsServer = {
cpus: 0,
mem_ram: 0,
disk_io: 0,
net_tx: 0,
net_rx: 0
};
export class IdCloudHostService {

wssUrl = `
wssStatsUrl = `
${environment.idCloudHost.url}
?subscribe=true
&apikey=${environment.idCloudHost.apiKey}
Expand All @@ -44,14 +33,13 @@ export class StatsServerService {

constructor(
private gs: GlobalService,
private sis: SocketIoService
private cfg: ConfigService
) {
this.ws = new WebSocket(this.wssUrl);
this.connect();
}

onOpen(): void {
this.gs.log('[ID_CLOUD_HOST_SERVICE-ON_OPEN] ⛈', this.wssUrl);
this.gs.log('[ID_CLOUD_HOST_SERVICE-ON_OPEN] ⛈', this.wssStatsUrl);
}

onError(err): void {
Expand All @@ -68,42 +56,28 @@ export class StatsServerService {
this.gs.log('[ID_CLOUD_HOST_SERVICE-ON_MESSAGE] ⛈', message);
const json = JSON.parse(message);
if (json.service === 'libvirt.used_memory_kb') {
this.statsServer.mem_ram = json.metric * 1000;
}
if (json.service === 'libvirt.block_wr_bytes_delta') {
this.statsServer.disk_io = json.metric;
}
if (json.service === 'libvirt.guest_time_per_vcpu_delta') {
this.statsServer.cpus = json.metric * 100;
}
if (json.service === 'libvirt.net_tx_bytes_delta') {
this.statsServer.net_tx = json.metric;
}
if (json.service === 'libvirt.net_rx_bytes_delta') {
this.statsServer.net_rx = json.metric;
this.cfg.statsServer.mem_ram = json.metric * 1000;
} else if (json.service === 'libvirt.block_wr_bytes_delta') {
this.cfg.statsServer.disk_io = json.metric;
} else if (json.service === 'libvirt.guest_time_per_vcpu_delta') {
this.cfg.statsServer.cpus = json.metric * 100;
} else if (json.service === 'libvirt.net_tx_bytes_delta') {
this.cfg.statsServer.net_tx = json.metric;
} else if (json.service === 'libvirt.net_rx_bytes_delta') {
this.cfg.statsServer.net_rx = json.metric;
}
}

connect(): void {
connect(): void {
this.ws = new WebSocket(this.wssStatsUrl);
this.ws.on('open', this.onOpen.bind(this));
this.ws.on('error', this.onError.bind(this));
this.ws.on('close', this.onClose.bind(this));
this.ws.on('message', this.onMessage.bind(this));
}

@Cron(
CronExpression.EVERY_30_SECONDS,
{
name: CONSTANTS.cronStatsServer
}
)
async statistics(): Promise<void> {
const startTime = new Date();
this.gs.log('[CRON_TASK_STATIS_SERVER-START] 🐾', `${startTime}`);
this.sis.emitToRoomOrId(CONSTANTS.socketRoomNameGlobalPublic, 'stats-server', this.statsServer);
const endTime = new Date();
const elapsedTime = endTime.getTime() - startTime.getTime();
this.gs.log('[CRON_TASK_STATIS_SERVER-END] 🐾', `${endTime} @ ${elapsedTime} ms`);
send(data: any) {
this.ws.send(data);
}

}
12 changes: 4 additions & 8 deletions src/app/_shared/services/stats-server.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export class StatsServerService {
this.currentServerSubject.next(response.server);
}
});
this.socketEmitVolatile('stats-server', {}, (response: any) => {
this.gs.log('[SOCKET_STATS_SERVER]', response);
this.statsServer = response;
});
}

socketListen(): void {
Expand Down Expand Up @@ -304,14 +308,6 @@ export class StatsServerService {
this.quizRoom[room_id].options = this.gs.shuffle(this.quizRoom[room_id].options);
}
});
this.mySocket.on('stats-server', (data) => {
this.gs.log('[SOCKET_STATS_SERVER]', data);
this.statsServer.mem_ram = data.mem_ram;
this.statsServer.disk_io = data.disk_io;
this.statsServer.cpus = data.cpus;
this.statsServer.net_tx = data.net_tx;
this.statsServer.net_rx = data.net_rx;
});
}

socketEmit(eventName: string, eventData: any = {}, callback = null): void {
Expand Down
8 changes: 8 additions & 0 deletions src/models/socket-io.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export interface PingPongModel {
server?: ServerInfoModel;
}

export interface StatsServerModel {
cpus?: number,
mem_ram?: number,
disk_io?: number,
net_tx?: number,
net_rx?: number
}

export interface RoomModel {
[roomId: string]: {
[socketId: string]: UserModel;
Expand Down

0 comments on commit 78e77ab

Please sign in to comment.