1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
import {Component, OnDestroy, OnInit} from '@angular/core';
import {AuthModule, tr} from 'globunet-angular/core';
import {TranslateService} from '@ngx-translate/core';
import {OgCommonService} from '../../service/og-common.service';
import {StatusService} from '../../api/status.service';
import { Chart } from 'chart.js';
class Info {
cpu: any = {
usage: 0,
model: '-'
};
ram: any = {
total: 0,
used: 0,
units: '-'
};
disk: any = {};
network: any = {
inBytes: 0,
outBytes: 0,
card: '-'
};
ogServices: any[] = [];
}
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit, OnDestroy {
private timers: any;
maxLength = 50;
currentPos = 0;
info: Info;
diskIndex = 0;
status: any;
chart: Chart;
constructor(private authModule: AuthModule, private translate: TranslateService, private ogCommonService: OgCommonService, private statusService: StatusService) {
this.status = {
datasets: [
{
label: translate.instant('memory'),
data: [],
color: '#3c8dbc'
},
{
label: translate.instant('cpu'),
data: [],
color: '#00FF00'
}
],
xData: [],
options: {
grid: {
borderColor: '#f3f3f3',
borderWidth: 1,
tickColor: '#f3f3f3'
},
series: {
shadowSize: 0, // Drawing is faster without shadows
color: '#3c8dbc'
},
lines: {
fill: true, // Converts the line chart to area chart
color: '#3c8dbc'
},
yaxis: {
min: 0,
max: 100,
show: true
},
xaxis: {
show: true
}
}
};
this.info = new Info();
}
ngOnInit() {
this.chart = new Chart('canvas', this.status);
// La primera vez que entra en dashboard
this.ogCommonService.loadEngineConfig().subscribe(
(config) => {
this.timers = config.timers;
if (this.timers.serverStatusInterval.object == null) {
this.updateStatus();
this.timers.serverStatusInterval.object = setInterval(() => {
this.updateStatus();
}, this.timers.serverStatusInterval.tick);
}
},
(error) => {
}
);
}
ngOnDestroy(): void {
if (this.timers && this.timers.serverStatusInterval) {
clearInterval(this.timers.serverStatusInterval.object);
}
}
updateStatus() {
this.statusService.list().subscribe(
(response: any) => {
response = response[0];
response.ogServices = response.ogServices || [];
for (let index = 0; index < response.ogServices.length; index++) {
response.ogServices[index].etime = response.ogServices[index].etime.replace('-', ' d, ');
}
// Pasar de bytes a KB, MB o GB dependiendo del caso
response.network.inBytes = this.ogCommonService.getUnits(response.network.inBytes);
response.network.outBytes = this.ogCommonService.getUnits(response.network.outBytes);
this.info = {
cpu: response.cpu,
ram: {
total: Math.round((response.memInfo.total / (1024 * 1024))),
used: Math.round((response.memInfo.used / (1024 * 1024)) * 100) / 100,
units: 'GB'
},
disk: response.disk,
network: response.network,
ogServices: response.ogServices
};
// Calcular porcentaje de memoria
const mem = Math.round(((response.memInfo.used * 100) / response.memInfo.total) * 100) / 100;
let index = 0;
if (this.status.datasets[0].data.length > 0) {
index = this.status.xData[this.status.datasets[0].data.length - 1] + 1;
}
this.status.datasets[0].data.push(mem);
this.status.datasets[1].data.push(response.cpu.usage);
this.status.xData.push(index);
if (this.status.datasets[0].data.length > this.maxLength) {
this.status.datasets[0].data.shift();
this.status.datasets[1].data.shift();
}
},
(error) => {
alert(error);
}
);
}
changeDiskIndex() {
this.diskIndex = (this.diskIndex + 1) % this.info.disk.length;
}
}
|