Skip to content

Commit

Permalink
✔ Saldo Billing + Fix Socket
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed Aug 23, 2023
1 parent fbb1c0d commit 725363e
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 7 deletions.
4 changes: 2 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, StatsServerModel } from '../../models/socket-io.model';
import { VisitorModel, 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 @@ -89,7 +89,7 @@ export class SocketIoGateway implements OnGatewayInit, OnGatewayConnection, OnGa
}

@SubscribeMessage('track-get')
async trackGet(client: Socket, payload: PayloadModel): Promise<CallbackModel> {
async trackGet(client: Socket, payload: PayloadModel): Promise<VisitorModel> {
try {
let selected = null;
const idSlugUsername = payload.idSlugUsername.split('?')[0];
Expand Down
51 changes: 51 additions & 0 deletions src/api/scheduler/vps-billing.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// NodeJS Library
import { URL } from 'node:url';

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

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

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

import { ApiService } from '../services/api.service';
import { GlobalService } from '../services/global.service';
import { ConfigService } from '../services/config.service';

@Injectable()
export class VpsBillingService {

header = { ...environment.nodeJsXhrHeader, apikey: environment.idCloudHost.apiKey };

constructor(
private cfg: ConfigService,
private gs: GlobalService,
private api: ApiService
) {
//
}

@Cron(
CronExpression.EVERY_10_MINUTES,
{
name: CONSTANTS.cronVpsBilling
}
)
async updateBilling(): Promise<void> {
const startTime = new Date();
this.gs.log('[CRON_VPS_BILLING-START] 🐾', `${startTime}`);
try {
const url = new URL(`${environment.idCloudHost.apiUrl}/payment/billing_account/list`)
const res_raw = await this.api.getData(url, this.header);
const res_json: any = await res_raw.json();
const json = res_json[0];
this.cfg.statsServer.billing.ongoing = json.precalc_ongoing || json.running_totals?.ongoing;
} catch (error) {
this.gs.log('[CRON_VPS_BILLING-ERROR] 🐾', error, 'error');
}
const endTime = new Date();
const elapsedTime = endTime.getTime() - startTime.getTime();
this.gs.log('[CRON_VPS_BILLING-END] 🐾', `${endTime} @ ${elapsedTime} ms`);
}

}
3 changes: 3 additions & 0 deletions src/api/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export class ConfigService {
disk_io: 0,
net_tx: 0,
net_rx: 0
},
billing: {
ongoing: 0
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/api/services/idcloudhost.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class IdCloudHostService {

getStatsUrl(loc: string, vmUuid: string): string {
return `
${environment.idCloudHost.url}/${loc}/metrics-ws/index
${environment.idCloudHost.wsUrl}/${loc}/metrics-ws/index
?subscribe=true
&apikey=${environment.idCloudHost.apiKey}
&query=${encodeURIComponent(`
Expand All @@ -42,7 +42,8 @@ export class IdCloudHostService {
service =~ "libvirt.net_rx_bytes_delta"
)
)
`.replace(/\s\s+/g, ' ').trim())}
`.replace(/\s\s+/g, ' ').trim())
}
`.replace(/\s\s+/g, '').trim();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,11 @@ <h2 class="pt-3 border-bottom-dotted">
{{ SS.statsServer.mainSite.net_tx | bytes:2 }} / {{ SS.statsServer.mainSite.net_rx | bytes:2 }}
</span>
</p>
<p class="px-2 text-truncate" matTooltip="Diperbarui 10 Menit Sekali">
Sisa Saldo ::
<span class="text-warning">
{{ SS.statsServer.billing.ongoing | currency:'Rp ':'symbol':'1.2-2' }}
</span>
</p>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { StatsServerComponent } from './stats-server.component';

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

import { SharedMaterialModule } from '../../modules/shared-material.module';

@NgModule({
declarations: [StatsServerComponent],
imports: [
CommonModule,
CustomPipeModule
CustomPipeModule,
SharedMaterialModule
],
exports: [StatsServerComponent]
})
Expand Down
3 changes: 3 additions & 0 deletions src/app/_shared/services/stats-server.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export class StatsServerService {
disk_io: 0,
net_tx: 0,
net_rx: 0
},
billing: {
ongoing: 0
}
};

Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const CONSTANTS = {
cronTrackerStatistics: 'CRON_TRACKER_STATISTICS',
cronSitemap: 'CRON_SITEMAP',
cronStatsServer: 'CRON_STATS_SERVER',
cronVpsBilling: 'CRON_VPS_BILLING',
decoratorFilterApiKeyAccess: 'filter-api-key-access',
decoratorRoles: 'roles',
decoratorVerifiedOnly: 'verified-only',
Expand Down
3 changes: 2 additions & 1 deletion src/environments/api/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ export const environment = {
},
malClientId: SECRETS().MAL_CLIENT_ID, // '',
idCloudHost: {
url: 'wss://api.idcloudhost.com/v1',
apiUrl: 'https://api.idcloudhost.com/v1',
wsUrl: 'wss://api.idcloudhost.com/v1',
apiKey: SECRETS().ID_CLOUD_HOST_API_KEY, // '',
mainSite: "01b00d5a-905d-4328-bc8d-bf748f1fc3dc" // '',
}
Expand Down
5 changes: 4 additions & 1 deletion src/models/socket-io.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface PayloadModel {
[data: string]: any;
}

export interface CallbackModel {
export interface VisitorModel {
unique_ip?: number;
unique_user?: number;
verified_user?: number;
Expand Down Expand Up @@ -49,6 +49,9 @@ export interface StatsServerModel {
net_tx?: number,
net_rx?: number
};
billing?: {
ongoing?: number
};
}

export interface RoomModel {
Expand Down

0 comments on commit 725363e

Please sign in to comment.