-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmantela.js
385 lines (350 loc) · 9.7 KB
/
mantela.js
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
197
198
199
200
201
202
203
204
205
206
207
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
'use strict';
/**
* ノード
* @typedef { object } Node
* @property { string } id - 識別子
* @property { string[] } names - 名前
* @property { string } type - 種別
*/
/**
* エッヂ
* @typedef { object } Edge
* @property { string } from - 対応する Node の id
* @property { string } to - 対応する Node の id
* @property { string } label - 表示するラベル
*/
/**
* グラフ
* @typedef { object } Graph
* @property { Node[] } nodes - Node の列
* @property { Edge[] } edges - Edge の列
*/
/**
* mantela.json からグラフを生成する
* @param { string } firstMantela - 起点の mantela.json の URL
* @param { number } maxNest - 探索する交換局のホップ数
* @param { HTMLElement } elemStat - ステータス表示用の HTML 要素
*/
async function
generateGraph(firstMantela, maxNest = Infinity, elemStat = undefined, elemStatistic = undefined)
{
/* 実行時間を計測するために、開始タイムスタンプを保持する */
const executeStartedTime = performance.now();
/**
* ステータスの更新(指定されていれば)
* @param { string } mesg - 表示するメッセージ
*/
function updateStatus(mesg) {
if (elemStat && 'textContent' in elemStat)
elemStat.textContent = mesg;
}
/**
* 統計情報の更新(指定されていれば)
* @param { object }
*/
function updateStatistics(s) {
if (!elemStatistic)
return;
const liMantela = document.createElement('li');
liMantela.textContent = `Number of Mantelas: ${s.mantelas}`;
const liPbx = document.createElement('li');
liPbx.textContent = `Number of PBXs: ${s.pbxs}`;
const liTerminals = document.createElement('li');
liTerminals.textContent = `Number of terminals: ${s.terminals}`;
const ul = document.createElement('ul');
ul.append(liMantela, liPbx, liTerminals);
const clone = elemStatistic.cloneNode(false);
elemStatistic.parentElement.replaceChild(clone, elemStatistic);
clone.append(ul);
}
/**
* ノードの集合体
* @type { Map<string, Node }
*/
const nodes = new Map();
/**
* エッヂの集合体
* @type { Edge[] }
*/
const edges = [];
/**
* 探索キュー
* @var { object[] }
* @property { string } url - mantela.json の URL
* @property { number } nest - 階層の深さ
*/
const queue = [ { url: firstMantela, nest: 0 } ];
/**
* 訪問済 URL, ID
* @var { Set<string> }
*/
const visited = new Set();
/**
* 統計情報
*/
const statistics = {
mantelas: 0,
pbxs: 0,
terminals: 0,
};
while (queue.length > 0) {
const current = queue.shift();
/* 訪問済 URL や最大深さより深過ぎる場合は辿らない */
if (visited.has(current.url) || current.nest > maxNest)
continue;
/* mantela.json を取得する */
updateStatus(current.url);
const mantela = await fetch(current.url, { mode: 'cors' })
.then(res => res.json())
.catch(err => new Error(err));
if (mantela instanceof Error) {
console.error(mantela, current.url);
updateStatus(mantela + current.url);
continue;
}
visited.add(current.url);
/* 自分の情報を登録する */
if ('aboutMe' in mantela) {
const aboutMe = mantela.aboutMe;
const me = nodes.get(aboutMe.identifier);
/* 既に知られている局の場合、呼び名を追加 */
if (me) {
me.names = [ ...new Set([ ...me.names, aboutMe.name ]) ];
Object.assign(me, aboutMe);
} else {
nodes.set(aboutMe.identifier, {
...aboutMe,
id: aboutMe.identifier,
names: [ aboutMe.name ],
type: 'PBX',
});
statistics.pbxs++;
}
} else {
/* 自分の情報すら名乗れない局の情報は登録できない */
continue;
}
/* 訪問済 ID の場合はここでおしまい */
const curNode = nodes.get(mantela.aboutMe.identifier);
if (visited.has(curNode.id))
continue;
visited.add(curNode.id);
statistics.mantelas++;
/* 内線番号を登録する */
if ('extensions' in mantela)
mantela.extensions.forEach((e, i) => {
const nodeId = `${curNode.id} `
+ `${e.identifier || crypto.randomUUID()}`;
const node = nodes.get(nodeId);
const unavailable = curNode.unavailable || undefined;
/* 既に知られている内線の場合、呼び名を追加 */
if (node)
node.names = [ ...new Set([ ...node.names, e.name ]) ];
else {
nodes.set(nodeId, {
...e,
unavailable,
id: nodeId,
names: [ e.name ],
name: `${curNode.names[0]} ${e.name}`,
});
statistics.terminals++;
}
/* 番号追加 */
edges.push({
unavailable,
from: curNode.id,
to: nodeId,
label: e.extension,
color: '#E87A90',
});
if (e.transferTo) {
e.transferTo.forEach(k => {
const toId = !!~k.indexOf(' ')
? k : `${curNode.id} ${k}`;
edges.push({
unavailable,
from: nodeId,
to: toId,
});
});
}
});
/* 接続局を登録する(接続数を考慮する) */
if ('providers' in mantela && current.nest < maxNest)
mantela.providers.forEach(e => {
const node = nodes.get(e.identifier);
/* 既に知られている局の場合、呼び名を追加 */
if (node) {
node.names = [ ...new Set([ ...node.names, e.name ]) ];
} else {
nodes.set(e.identifier, {
...e,
id: e.identifier,
names: [ e.name ],
type: 'PBX',
});
statistics.pbxs++;
}
/* 番号追加 */
edges.push({
from: curNode.id,
to: e.identifier,
label: e.prefix,
});
/* キュー追加 */
if (e.mantela)
queue.push({
url: e.mantela,
nest: current.nest + 1,
});
});
}
/**
* 最終的に返却するグラフ構造
* @type { Graph }
*/
const graph = {
nodes: Array.from(nodes.values()),
edges: edges,
};
/* 実行時間を計算し、ステータスに結果を反映する */
const executeCompletedTime = performance.now();
const executeLength = executeCompletedTime - executeStartedTime;
updateStatus(`Done. Execution Time: ${executeLength} ms`);
/* 統計情報を表示する */
updateStatistics(statistics);
return graph;
}
/**
* VoIP 網の接続情報を表示する
* @param { HTMLElement } container - 可視化結果を格納する要素
* @param { Graph } graph - 接続情報
*/
function
graph2vis(container, graph)
{
const imgtab = {
alias: './img/alias.svg',
application: './img/application.svg',
cellphone: './img/cellphone.svg',
dialphone: './img/dialphone.svg',
fax: './img/fax.svg',
information: './img/information.svg',
main: './img/main.svg',
modem: './img/modem.svg',
music: './img/music.svg',
other: './img/other.svg',
phone: './img/phone.svg',
pushphone: './img/pushphone.svg',
reserved: './img/reserved.svg',
smartphone: './img/smartphone.svg',
switchboard: './img/switchboard.svg',
unknown: './img/unknown.svg',
unused: './img/unused.svg',
};
const nodes = graph.nodes.map(e => ({
id: e.id,
label: e.names[0],
color: e.type !== 'PBX' && 'orange',
shape: e.type === 'PBX' ? 'circle' : 'image',
image: imgtab[e.type] || imgtab['unknown'],
opacity: e.unavailable ? 0.3 : 1,
}));
const edges = graph.edges.map(e => {
// FIXME: find() のせいでかなり遅くなる
const from = graph.nodes.find(v => v.id === e.from);
const to = graph.nodes.find(v => v.id === e.to);
const unavailable = e.unavailable || from?.unavailable || to?.unavailable;
return {
...e,
color: {
color: e.color,
opacity: unavailable ? 0.3: 1,
},
};
});
const data = {
nodes,
edges,
};
const options = {
edges: {
arrows: 'to',
},
layout: {
improvedLayout: false,
},
physics: {
solver: 'forceAtlas2Based',
},
};
return new vis.Network(container, data, options);
}
/*
* ノードの情報を表示する
*/
const showNodeInfo = node => new Promise(r => {
const dialog = document.createElement('dialog');
dialog.addEventListener('close', _ => {
dialog.parentNode.removeChild(dialog);
r(dialog.returnValue);
});
document.body.append(dialog);
const button = document.createElement('button');
button.textContent = 'OK';
button.addEventListener('click', _ => dialog.close(true));
const div = document.createElement('div');
div.style.textAlign = 'end';
div.append(button);
const code = document.createElement('code');
code.textContent = JSON.stringify(node, null, 4);
const pre = document.createElement('pre');
pre.style.maxWidth = '80vw';
pre.style.maxHeight = '80vh';
pre.overflow = 'scroll';
pre.append(code);
dialog.append(pre, div);
dialog.showModal();
});
/*
* フォーム送信時のイベントハンドラ
* mantela.json を取得し、接続情報を解析し、表示する。
*/
formMantela.addEventListener('submit', async e => {
e.preventDefault();
btnGenerate.disabled = true;
const limit = checkNest.checked ? +numNest.value : Infinity;
const graph = await generateGraph(urlMantela.value, limit, outputStatus, divStatistic);
const network = graph2vis(divMantela, graph);
network.on('doubleClick', async e => {
if (e.nodes.length > 0) {
const node = graph.nodes.find(v => v.id === e.nodes[0]);
await showNodeInfo(node);
}
});
secMandala.scrollIntoView({
behavior: 'smooth',
block: 'start',
});
btnGenerate.disabled = false;
});
/*
* 表示結果を大きく表示するためのハック
*/
const autoFit = new ResizeObserver(entries => {
entries.forEach(e => {
e.target.style.left = null;
const { x } = e.target.getBoundingClientRect();
e.target.style.left = `-${x}px`;
});
});
autoFit.observe(divMantela);
/*
* first のパラメータが指定されているときは自動入力して表示する
*/
const urlSearch = new URLSearchParams(document.location.search);
if (urlSearch.get('first')) {
urlMantela.value = urlSearch.get('first');
btnGenerate.click();
}