Skip to content

Commit

Permalink
✔ Stats Server Info
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed May 20, 2023
1 parent ba81b64 commit 12c4dc2
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/api/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ 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 @@ -264,6 +265,7 @@ import { UserService } from './repository/user.service';
// Service Task Schedulers
RssFeedTasksService,
TrackerStatisticsService,
StatsServerService,
// Service Entities
AnimeService,
ApiKeyService,
Expand Down
109 changes: 109 additions & 0 deletions src/api/scheduler/stats-server-tasks.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 3rd Party Library
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';

@Injectable()
export class StatsServerService {

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

wssUrl = `
${environment.idCloudHost.url}
?subscribe=true
&apikey=${environment.idCloudHost.apiKey}
&query=${encodeURIComponent(`
(
(host =~ "${environment.idCloudHost.uuid}") and
(
service =~ "libvirt.guest_time_per_vcpu_delta" or
service =~ "libvirt.used_memory_kb" or
service =~ "libvirt.block_wr_bytes_delta" or
service =~ "libvirt.net_tx_bytes_delta" or
service =~ "libvirt.net_rx_bytes_delta"
)
)
`.replace(/\s\s+/g, ' ').trim())}
`.replace(/\s\s+/g, '').trim();

ws: WebSocket;

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

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

onError(err): void {
this.gs.log('[ID_CLOUD_HOST_SERVICE-ON_ERROR] ⛈', err, 'error');
}

onClose(code, data): void {
const reason = data.toString();
this.gs.log('[ID_CLOUD_HOST_SERVICE-ON_CLOSE] ⛈', { code, reason });
}

onMessage(data, isBinary): void {
const message = isBinary ? data : data.toString();
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;
}
}

connect(): void {
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`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,29 @@ <h2 class="pt-3 border-bottom-dotted">
{{ SS.visitor }} Koneksi
</span>
</p>
<p class="px-2 text-truncate">
CPUs Load ::
<span class="text-warning">
{{ SS.statsServer.cpus | number:'1.2-2' }} %
</span>
</p>
<p class="px-2 text-truncate">
RAM Usage ::
<span class="text-warning">
{{ SS.statsServer.mem_ram | bytes:2 }}
</span>
</p>
<p class="px-2 text-truncate">
Disk IO (/s) ::
<span class="text-warning">
{{ SS.statsServer.disk_io | bytes:2 }}
</span>
</p>
<p class="px-2 text-truncate">
Network UD (/s) ::
<span class="text-warning">
{{ SS.statsServer.net_tx | bytes:2 }} / {{ SS.statsServer.net_rx | bytes:2 }}
</span>
</p>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { CommonModule } from '@angular/common';

import { StatsServerComponent } from './stats-server.component';

import { CustomPipeModule } from '../../pipes/custom-pipe.module';

@NgModule({
declarations: [StatsServerComponent],
imports: [
CommonModule
CommonModule,
CustomPipeModule
],
exports: [StatsServerComponent]
})
Expand Down
16 changes: 16 additions & 0 deletions src/app/_shared/services/stats-server.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export class StatsServerService {

latency = 0;

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

messageChatUnreadCount = 0;

badgeNews = [];
Expand Down Expand Up @@ -296,6 +304,14 @@ 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
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const CONSTANTS = {
],
cronFansubRssFeed: 'CRON_FANSUB_RSS_FEED',
cronTrackerStatistics: 'CRON_TRACKER_STATISTICS',
cronStatsServer: 'CRON_STATS_SERVER',
decoratorFilterApiKeyAccess: 'filter-api-key-access',
decoratorRoles: 'roles',
decoratorVerifiedOnly: 'verified-only',
Expand Down
5 changes: 5 additions & 0 deletions src/environments/api/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,9 @@ export const environment = {
],
},
malClientId: SECRETS().MAL_CLIENT_ID, // '',
idCloudHost: {
url: 'wss://api.idcloudhost.com/v1/jkt01/metrics-ws/index',
apiKey: SECRETS().ID_CLOUD_HOST_API_KEY, // '',
uuid: "01b00d5a-905d-4328-bc8d-bf748f1fc3dc" // '',
}
};
1 change: 1 addition & 0 deletions src/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface SECRETS_DATA_TYPE {
DISCORD_CLIENT_SECRET: string;
DISCORD_BOT_LOGIN_TOKEN: string;
MAL_CLIENT_ID: string;
ID_CLOUD_HOST_API_KEY: string;
};

let SECRETS_DATA: SECRETS_DATA_TYPE = null;
Expand Down

0 comments on commit 12c4dc2

Please sign in to comment.