-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg1.html
196 lines (169 loc) · 5.46 KB
/
g1.html
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Canvas</title>
<link href="style.css" rel="stylesheet">
</head>
<body style="margin: 0; overflow: hidden;">
<canvas class="myCanvas">
<p>浏览器不支持canvas</p>
</canvas>
</body>
<script>
var canvas = document.querySelector(".myCanvas");
var width = (canvas.width = window.innerWidth);
var height = (canvas.height = window.innerHeight);
var ctx = canvas.getContext("2d");
let data = [
{
"name": "node-1",
"pods": [
{
"name": "chief-1",
"role": "chief"
},
{
"name": "worker-1",
"role": "worker"
},
{
"name": "worker-2",
"role": "worker"
},
{
"name": "ps-0",
"role": "ps",
"descs": [
{
"level": "info",
"text": "latency: 10ms"
},
{
"level": "warn",
"text": "memory: 10g"
},
{
"level": "error",
"text": "memory: 100g"
},
]
},
]
},
{
"name": "node-2",
"pods": [
{
"name": "ps-1",
"role": "ps"
},
{
"name": "ps-2",
"role": "ps"
},
]
},
{
"name": "node-3",
"pods": [
{
"name": "driver-1",
"role": "driver"
},
]
}
]
function filter_role(data, roles) {
filtered_nodes = []
data.forEach(node => {
filtered_pods = []
node.pods.forEach(pod => {
if (roles.includes(pod.role)) {
filtered_pods.push(pod)
}
})
filtered_nodes.push({
name: node.name,
pods: filtered_pods,
})
})
return filtered_nodes
}
// data = filter_role(data, ["chief", "worker", "driver"])
let max_node_pods = 0
data.forEach(node => {
if (node.pods.length > max_node_pods) {
max_node_pods = node.pods.length
}
});
const roleColorMap = {
'ps': '#FFD7C4',
'chief': '#7695FF',
'worker': '#9DBDFF',
'driver': '#FFC600',
'node': '#DFF2EB',
}
const descColorMap = {
"info": "green",
"warn": "yellow",
"error": "red",
}
descColorMap['warning'] = descColorMap['warn']
const n_node = data.length
const label_font = "sans-serif"
const label_size = 15
const node_x_padding_ratio = 0.1
const node_x_padding = width * node_x_padding_ratio
const node_y_padding_ratio = 0.05
const node_y_padding = height * node_y_padding_ratio
const node_width = (width - (n_node + 1) * node_x_padding) / n_node
const node_height = height - node_y_padding * 2
const node_pod_x_padding_ratio = 0.2
let node_pod_x_padding = node_width * node_pod_x_padding_ratio
let node_pod_y_padding = 10
// 根据宽度和高度,计算出最佳的pod大小
let pod_size = 0
const pod_size_1 = node_width - node_pod_x_padding * 2
const pod_size_2 = (node_height - node_pod_y_padding * (max_node_pods + 1)) / max_node_pods
if (pod_size_1 >= pod_size_2) {
pod_size = pod_size_2
// 重新计算node_pod_x_padding
node_pod_x_padding = (node_width - pod_size) / 2
}
else if (pod_size_1 < pod_size_2) {
pod_size = pod_size_1
// 重新计算node_pod_y_padding
node_pod_y_padding = (node_height - (pod_size * max_node_pods)) / (max_node_pods + 1)
}
// 有可能会相互覆盖/碰撞,但我们暂不考虑
data.forEach((node, i) => {
node_x = node_x_padding * (i + 1) + node_width * i
node_y = node_y_padding
// fill nodes
ctx.fillStyle = roleColorMap.node;
ctx.fillRect(node_x, node_y,
node_width, node_height);
ctx.fillStyle = "black";
ctx.font = `${label_size}px ${label_font}`;
ctx.fillText(node.name, node_x + 5, node_y + label_size);
// fill pods
node.pods.forEach((pod, j) => {
const pod_x = node_x + node_pod_x_padding
const pod_y = node_y + node_pod_y_padding * (j + 1) + pod_size * j
ctx.fillStyle = roleColorMap[pod.role];
ctx.fillRect(pod_x, pod_y,
pod_size, pod_size);
ctx.fillStyle = "black";
ctx.font = `${label_size}px ${label_font}`;
ctx.fillText(pod.name, pod_x + 5, pod_y + label_size);
pod.descs?.forEach((desc, i) => {
ctx.fillStyle = descColorMap[desc.level];
ctx.font = `${label_size}px ${label_font}`;
ctx.fillText(desc.text, pod_x + 5, pod_y + label_size * (2 + i) + 5);
})
})
})
</script>
</html>