Newer
Older
/**
* Network topology diagram using D3.js - shows coordinator and servers.
*/
import { useEffect, useRef } from 'react';
import * as d3 from 'd3';
import type { SimulationConfig } from '../../types/simulation';
import { useSimulationStore } from '../../store/simulationStore';
import { formatRate } from '../../utils/timeFormat';
interface NetworkDiagramProps {
config: SimulationConfig;
}
interface Node {
id: string;
type: 'coordinator' | 'server' | 'exit';
x: number;
y: number;
label: string;
}
interface Link {
source: string;
target: string;
probability: number;
label: string;
}
export default function NetworkDiagram({ config }: NetworkDiagramProps) {
const svgRef = useRef<SVGSVGElement>(null);
const { timeUnit } = useSimulationStore();
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
useEffect(() => {
if (!svgRef.current) return;
const width = 800;
const height = 500;
const margin = { top: 20, right: 20, bottom: 20, left: 20 };
// Clear previous SVG
d3.select(svgRef.current).selectAll('*').remove();
const svg = d3
.select(svgRef.current)
.attr('width', width)
.attr('height', height)
.attr('viewBox', `0 0 ${width} ${height}`)
.attr('preserveAspectRatio', 'xMidYMid meet');
// Create nodes
const nodes: Node[] = [
{
id: 'coordinator',
type: 'coordinator',
x: width / 2,
y: 100,
label: 'Coordinateur',
},
{
id: 'exit',
type: 'exit',
x: width / 2,
y: height - 80,
label: 'Sortie',
},
];
// Add server nodes
const numServers = config.servers.length;
const serverSpacing = Math.min(150, (width - 2 * margin.left) / (numServers + 1));
const serverY = height / 2;
config.servers.forEach((server, i) => {
const x = margin.left + (i + 1) * serverSpacing + (width - numServers * serverSpacing) / 2;
nodes.push({
id: server.id,
type: 'server',
x,
y: serverY,
label: `Serveur ${i + 1}`,
});
});
// Create links
const links: Link[] = [];
// External arrival to coordinator
links.push({
source: 'arrival',
target: 'coordinator',
probability: config.arrival_rate,
label: `λ = ${formatRate(config.arrival_rate, timeUnit)}`,
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
197
198
199
200
201
202
203
204
205
206
});
// Coordinator to exit
links.push({
source: 'coordinator',
target: 'exit',
probability: config.coordinator_exit_probability,
label: `p = ${config.coordinator_exit_probability.toFixed(2)}`,
});
// Coordinator to servers
config.servers.forEach((server) => {
links.push({
source: 'coordinator',
target: server.id,
probability: server.routing_probability,
label: `q = ${server.routing_probability.toFixed(2)}`,
});
});
// Servers back to coordinator
config.servers.forEach((server) => {
links.push({
source: server.id,
target: 'coordinator',
probability: 1.0,
label: '',
});
});
// Draw links
const linkGroup = svg.append('g').attr('class', 'links');
// Draw coordinator to servers/exit
links
.filter((l) => l.source === 'coordinator')
.forEach((link) => {
const sourceNode = nodes.find((n) => n.id === link.source);
const targetNode = nodes.find((n) => n.id === link.target);
if (!sourceNode || !targetNode) return;
// Draw curved arrow
linkGroup
.append('path')
.attr('d', () => {
const dx = targetNode.x - sourceNode.x;
const dy = targetNode.y - sourceNode.y;
const dr = Math.sqrt(dx * dx + dy * dy) * 0.3;
return `M ${sourceNode.x},${sourceNode.y} Q ${sourceNode.x + dx / 2 + dr},${
sourceNode.y + dy / 2
} ${targetNode.x},${targetNode.y - 30}`;
})
.attr('fill', 'none')
.attr('stroke', link.target === 'exit' ? '#ef4444' : '#3b82f6')
.attr('stroke-width', 2)
.attr('marker-end', 'url(#arrowhead)');
// Add label
if (link.label) {
const midX = sourceNode.x + (targetNode.x - sourceNode.x) / 2;
const midY = sourceNode.y + (targetNode.y - sourceNode.y) / 2;
linkGroup
.append('text')
.attr('x', midX + 20)
.attr('y', midY - 10)
.attr('font-size', '12px')
.attr('fill', '#6b7280')
.text(link.label);
}
});
// Draw servers to coordinator (feedback)
links
.filter((l) => l.source !== 'coordinator' && l.source !== 'arrival')
.forEach((link) => {
const sourceNode = nodes.find((n) => n.id === link.source);
const targetNode = nodes.find((n) => n.id === link.target);
if (!sourceNode || !targetNode) return;
linkGroup
.append('path')
.attr('d', () => {
const dx = targetNode.x - sourceNode.x;
const dy = targetNode.y - sourceNode.y;
const dr = Math.sqrt(dx * dx + dy * dy) * 0.5;
return `M ${sourceNode.x},${sourceNode.y - 30} Q ${sourceNode.x + dx / 2 - dr},${
sourceNode.y + dy / 2
} ${targetNode.x},${targetNode.y + 30}`;
})
.attr('fill', 'none')
.attr('stroke', '#9ca3af')
.attr('stroke-width', 1.5)
.attr('stroke-dasharray', '5,5')
.attr('marker-end', 'url(#arrowhead-gray)');
});
// Draw external arrival
const arrivalX = width / 2 - 100;
linkGroup
.append('line')
.attr('x1', arrivalX)
.attr('y1', 50)
.attr('x2', width / 2 - 40)
.attr('y2', 80)
.attr('stroke', '#10b981')
.attr('stroke-width', 2)
.attr('marker-end', 'url(#arrowhead-green)');
linkGroup
.append('text')
.attr('x', arrivalX - 10)
.attr('y', 45)
.attr('font-size', '12px')
.attr('fill', '#059669')
.text(`λ = ${formatRate(config.arrival_rate, timeUnit)}`);
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Define arrowhead markers
const defs = svg.append('defs');
defs
.append('marker')
.attr('id', 'arrowhead')
.attr('viewBox', '0 0 10 10')
.attr('refX', 9)
.attr('refY', 5)
.attr('markerWidth', 6)
.attr('markerHeight', 6)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 0 0 L 10 5 L 0 10 z')
.attr('fill', '#3b82f6');
defs
.append('marker')
.attr('id', 'arrowhead-gray')
.attr('viewBox', '0 0 10 10')
.attr('refX', 9)
.attr('refY', 5)
.attr('markerWidth', 6)
.attr('markerHeight', 6)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 0 0 L 10 5 L 0 10 z')
.attr('fill', '#9ca3af');
defs
.append('marker')
.attr('id', 'arrowhead-green')
.attr('viewBox', '0 0 10 10')
.attr('refX', 9)
.attr('refY', 5)
.attr('markerWidth', 6)
.attr('markerHeight', 6)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 0 0 L 10 5 L 0 10 z')
.attr('fill', '#10b981');
// Draw nodes
const nodeGroup = svg.append('g').attr('class', 'nodes');
nodes.forEach((node) => {
const group = nodeGroup.append('g');
// Node circle
group
.append('circle')
.attr('cx', node.x)
.attr('cy', node.y)
.attr('r', node.type === 'coordinator' ? 40 : node.type === 'exit' ? 30 : 35)
.attr('fill', node.type === 'coordinator' ? '#3b82f6' : node.type === 'exit' ? '#ef4444' : '#8b5cf6')
.attr('stroke', '#fff')
.attr('stroke-width', 3);
// Node label
group
.append('text')
.attr('x', node.x)
.attr('y', node.y + 5)
.attr('text-anchor', 'middle')
.attr('font-size', '12px')
.attr('font-weight', 'bold')
.attr('fill', '#fff')
.text(node.label);
// Service rate label for servers and coordinator
if (node.type === 'coordinator') {
group
.append('text')
.attr('x', node.x)
.attr('y', node.y + 60)
.attr('text-anchor', 'middle')
.attr('font-size', '11px')
.attr('fill', '#4b5563')
.text(`μc = ${formatRate(config.coordinator_service_rate, timeUnit)}`);
} else if (node.type === 'server') {
const serverIndex = config.servers.findIndex((s) => s.id === node.id);
if (serverIndex >= 0) {
group
.append('text')
.attr('x', node.x)
.attr('y', node.y + 55)
.attr('text-anchor', 'middle')
.attr('font-size', '11px')
.attr('fill', '#4b5563')
.text(`μ = ${formatRate(config.servers[serverIndex].service_rate, timeUnit)}`);
}, [config, timeUnit]);
return (
<div className="bg-white border border-gray-200 rounded-lg p-4">
<h3 className="text-sm font-semibold text-gray-900 mb-3">Topologie du réseau</h3>
<div className="flex justify-center">
<svg ref={svgRef} className="max-w-full h-auto"></svg>
</div>
<div className="mt-4 flex items-center justify-center space-x-6 text-xs text-gray-600">
<div className="flex items-center">
<div className="w-4 h-4 bg-blue-600 rounded-full mr-2"></div>
<span>Coordinateur</span>
</div>
<div className="flex items-center">
<div className="w-4 h-4 bg-purple-600 rounded-full mr-2"></div>
<span>Serveur</span>
</div>
<div className="flex items-center">
<div className="w-4 h-4 bg-red-600 rounded-full mr-2"></div>
<span>Sortie</span>
</div>
</div>
</div>
);
}