app.js 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. /* =====================================================================
  2. L2 Physical Topology — display-only interactive map.
  3. Groups switches into frames (COBCO outer → workshop/DataCenter/Core),
  4. colours links by speed, nodes by latency & alarm, supports dark mode,
  5. terminal show / only-offline, live + historical snapshots, MAC/IP search.
  6. ===================================================================== */
  7. (function () {
  8. 'use strict';
  9. var CFG = window.L2_CFG || {};
  10. var T = window.L2_I18N || {};
  11. var META = window.L2_META || {};
  12. var SNAP = window.L2SNAP || null;
  13. var colors = CFG.colors || {100:'#F39C12',1000:'#2ECC71',10000:'#3498DB',20000:'#9B59B6',unknown:'#E74C3C',down:'#C0392B'};
  14. var LATW = (CFG.latency_warn != null) ? +CFG.latency_warn : 3;
  15. var LATB = (CFG.latency_bad != null) ? +CFG.latency_bad : 5;
  16. var TIER_BG = {core:'#1F4E78', dist:'#2E75B6', access:'#548235'};
  17. var TIER_BG_DARK = {core:'#1B4A72', dist:'#2A5E93', access:'#3E6B2E'};
  18. var SEV_COLORS = ['#97AAB3','#7499FF','#FFC859','#FFA059','#E97659','#E45959'];
  19. var NODE_W = 156, NODE_H = 50, GX = 26, GY = 18, PADX = 20, PADY = 42;
  20. var DGAP_X = 18, DGAP_Y = 26;
  21. var GROUP_GAP_X = 46, GROUP_GAP_Y = 48, MAXW = 2800;
  22. // overview ("全景") tiles
  23. var TILE_W = 232, TILE_H = 116, TILE_GAP_X = 24, TILE_GAP_Y = 22;
  24. var cy = null;
  25. var dark = false;
  26. var showTerm = false;
  27. var onlyOff = false;
  28. var paused = false;
  29. var countdown = 0;
  30. var timer = null;
  31. var lastSnap = null;
  32. var viewGroup = null; // direct-link focus group, never hides the full chart
  33. var detailNet = null; // null/auto, 'SURVE' or 'OFFICE' inside a workshop
  34. var focusGroupLabel = null; // ?g= target, zoomed after the full chart is built
  35. var pathParent = {}; // child -> parent chain for hover highlighting
  36. var lockedPath = null; // switch id whose path is locked by click
  37. function el(id) { return document.getElementById(id); }
  38. function t(k, d) { return (T[k] != null) ? T[k] : (d != null ? d : k); }
  39. /* ---------------- name parsing / grouping ---------------- */
  40. function parseName(name) {
  41. var r = {workshop:'', floor:'', cab:'', u:'', area:'', role:'', ip:''};
  42. if (!name) return r;
  43. var toks = String(name).split('-');
  44. for (var i = 0; i < toks.length; i++) {
  45. var tk = toks[i];
  46. if (/^\d{1,3}(\.\d{1,3}){3}$/.test(tk)) { r.ip = tk; continue; }
  47. if (!r.workshop && /^\d{4}[A-Za-z]?$/.test(tk)) { r.workshop = tk; continue; }
  48. if (!r.floor) {
  49. var fm = /^(GRD|\d{1,2}F)([A-Z])?(\d+)?$/i.exec(tk);
  50. if (fm) { r.floor = fm[1].toUpperCase(); r.cab = fm[2] || ''; r.u = fm[3] || ''; continue; }
  51. }
  52. if (r.floor && !r.u && /^\d{1,3}$/.test(tk)) { r.u = tk; continue; }
  53. if (!r.area && /^(OFFICE|SURVE|GRDA|DC|IDC|LAB|PROD)$/i.test(tk)) { r.area = tk.toUpperCase(); continue; }
  54. if (!r.role && /^(Dist|Access|Core)$/i.test(tk)) { r.role = tk; }
  55. }
  56. return r;
  57. }
  58. function labelFor(n) {
  59. var p = parseName(n.name);
  60. var l1 = [];
  61. if (p.workshop) l1.push(p.workshop);
  62. if (p.area) l1.push(p.area);
  63. if (!l1.length) {
  64. l1.push((n.name || '').split('-')[0] || n.ip);
  65. }
  66. var l2 = [];
  67. if (p.floor) { var loc = p.floor + 'F' + (p.cab || '') + (p.u ? '-' + p.u + 'U' : ''); l1.push(loc); }
  68. if (p.role) l2.push(p.role);
  69. l2.push(n.ip);
  70. return l1.join(' · ') + '\n' + l2.join(' · ');
  71. }
  72. function detailLabel(n) {
  73. var s = labelFor(n);
  74. if (viewGroup) {
  75. var p = viewGroup + ' · ';
  76. if (s.indexOf(p) === 0) s = s.substring(p.length);
  77. }
  78. return s;
  79. }
  80. function rowLabel(n) {
  81. var s = String((n && n.name) || (n && n.ip) || '');
  82. s = s.replace(/^COBCO-/i, '');
  83. if (viewGroup) {
  84. try { s = s.replace(new RegExp('^' + viewGroup + '-', 'i'), ''); } catch (e) {}
  85. }
  86. return s;
  87. }
  88. function rootLabel(n) {
  89. var s = rowLabel(n);
  90. var m = /^(.+?)-(SURVE|OFFICE|OTHER)-(Dist|Access)-(.+)$/i.exec(s);
  91. if (m) return m[2].toUpperCase() + ' · ' + m[1] + '\n' + m[3] + ' · ' + m[4];
  92. return s;
  93. }
  94. function expandLabel(tpl, m) {
  95. return String(tpl).replace(/\$(\d)/g, function (_, d) { return m && m[+d] != null ? m[+d] : ''; });
  96. }
  97. function groupOf(n) {
  98. var name = n.name || n.ip || '';
  99. var rules = CFG.group_rules || [];
  100. for (var i = 0; i < rules.length; i++) {
  101. var r = rules[i];
  102. if (r.match === 'regex') {
  103. var re;
  104. try { re = new RegExp(r.pattern); } catch (e) { continue; }
  105. var m = re.exec(name);
  106. if (m) return {label: expandLabel(r.label, m) || (CFG.other_label || 'Other'), big: !!r.big};
  107. } else {
  108. if (name.toLowerCase().indexOf(String(r.pattern).toLowerCase()) >= 0) return {label: r.label || (CFG.other_label || 'Other'), big: !!r.big};
  109. }
  110. }
  111. return {label: CFG.other_label || 'Other', big: false};
  112. }
  113. /* ---------------- overview layout (workshop tiles) ---------------- */
  114. function groupStats(snap) {
  115. var map = {};
  116. var nodeById = {};
  117. (snap.nodes || []).forEach(function (n) {
  118. nodeById[n.id] = n;
  119. var g = groupOf(n);
  120. var s = map[g.label] || (map[g.label] = {label: g.label, big: g.big, nodes: [], alarms: 0, offline: 0, maxsev: -1, latmax: 0, up: 0});
  121. s.nodes.push(n);
  122. s.offline += (+n.term_offline || 0);
  123. if (n.alarms && n.alarms.length) {
  124. s.alarms += n.alarms.length;
  125. var sev = (n.maxsev != null && +n.maxsev >= 0) ? +n.maxsev : -1;
  126. n.alarms.forEach(function (a) { if (a && a.sev != null && +a.sev > sev) sev = +a.sev; });
  127. if (sev > s.maxsev) s.maxsev = sev;
  128. }
  129. if (n.latency_ms != null && +n.latency_ms > s.latmax) s.latmax = +n.latency_ms;
  130. });
  131. (snap.edges || []).forEach(function (e) {
  132. var a = nodeById[e.source], b = nodeById[e.target];
  133. if (!a || !b) return;
  134. var ga = groupOf(a), gb = groupOf(b);
  135. if (ga.label === gb.label) return;
  136. // count links that leave this workshop towards a core switch
  137. if ((/core/i.test(a.tier || '') || /core/i.test(a.name || '')) && map[gb.label]) map[gb.label].up++;
  138. if ((/core/i.test(b.tier || '') || /core/i.test(b.name || '')) && map[ga.label]) map[ga.label].up++;
  139. });
  140. return map;
  141. }
  142. function groupRank(l) {
  143. if (/core/i.test(l)) return 0;
  144. if (/data\s*center/i.test(l)) return 1;
  145. var m = /^(\d{4})([A-Za-z]?)$/.exec(l);
  146. if (m) return 1000 + parseInt(m[1], 10) * 40 + (m[2] ? m[2].toUpperCase().charCodeAt(0) - 64 : 0);
  147. return 5000;
  148. }
  149. function computeOverview(snap) {
  150. var stats = groupStats(snap);
  151. var labels = Object.keys(stats).sort(function (a, b) { return groupRank(a) - groupRank(b); });
  152. var special = labels.filter(function (l) { return groupRank(l) < 1000; });
  153. var others = labels.filter(function (l) { return groupRank(l) >= 1000; });
  154. var COLS = Math.max(4, Math.min(7, Math.ceil(Math.sqrt(Math.max(1, others.length)))));
  155. var gridW = COLS * TILE_W + (COLS - 1) * TILE_GAP_X;
  156. var pos = {}, tiles = [], y = 0;
  157. if (special.length) {
  158. var sW = special.length * TILE_W + (special.length - 1) * TILE_GAP_X;
  159. var sx = Math.max(0, (gridW - sW) / 2);
  160. special.forEach(function (l) {
  161. tiles.push({label: l, x: sx, y: y, w: TILE_W, h: TILE_H});
  162. pos[l] = {x: sx + TILE_W / 2, y: y + TILE_H / 2};
  163. sx += TILE_W + TILE_GAP_X;
  164. });
  165. y += TILE_H + 46;
  166. }
  167. var maxX = gridW, maxY = y;
  168. for (var i = 0; i < others.length; i += COLS) {
  169. var row = others.slice(i, i + COLS);
  170. var rW = row.length * TILE_W + (row.length - 1) * TILE_GAP_X;
  171. var rx = Math.max(0, (gridW - rW) / 2);
  172. row.forEach(function (l, j) {
  173. tiles.push({label: l, x: rx + j * (TILE_W + TILE_GAP_X), y: y, w: TILE_W, h: TILE_H});
  174. pos[l] = {x: rx + j * (TILE_W + TILE_GAP_X) + TILE_W / 2, y: y + TILE_H / 2};
  175. });
  176. y += TILE_H + TILE_GAP_Y;
  177. maxY = y - TILE_GAP_Y;
  178. maxX = Math.max(maxX, rx + rW);
  179. }
  180. var root = {x: -34, y: -64, w: maxX + 68, h: maxY + 64 + 40};
  181. root.cx = root.x + root.w / 2; root.cy = root.y + root.h / 2;
  182. return {stats: stats, labels: labels, pos: pos, tiles: tiles, root: root};
  183. }
  184. function aggregateEdges(snap) {
  185. var gid = {};
  186. (snap.nodes || []).forEach(function (n) { gid[n.id] = groupOf(n).label; });
  187. var pairs = {};
  188. (snap.edges || []).forEach(function (e) {
  189. var a = gid[e.source], b = gid[e.target];
  190. if (!a || !b || a === b) return;
  191. var k = (a < b) ? (a + '\u0001' + b) : (b + '\u0001' + a);
  192. var p = pairs[k] || (pairs[k] = {a: a, b: b, count: 0, speed: 0, down: false});
  193. p.count++;
  194. if ((parseFloat(e.speed) || 0) > p.speed) p.speed = parseFloat(e.speed) || 0;
  195. if (e.state && e.state !== 'up') p.down = true;
  196. });
  197. return Object.keys(pairs).map(function (k) { return pairs[k]; });
  198. }
  199. /* ---------------- detail layout (one workshop, left-to-right tree) ---------------- */
  200. function floorInfo(n) {
  201. var toks = String((n && n.name) || '').split('-');
  202. for (var i = 0; i < toks.length; i++) {
  203. var tk = toks[i];
  204. var bm = /^B(\d+)$/i.exec(tk);
  205. if (bm) { var bn = parseInt(bm[1], 10); return {key: 'B' + bn, label: 'B' + bn, rank: -200 + bn}; }
  206. if (/^GRD/i.test(tk)) return {key: 'GRD', label: 'GRD', rank: -100};
  207. var fm = /^(\d+(?:\.\d+)?)F/i.exec(tk);
  208. if (fm) {
  209. var num = parseFloat(fm[1]);
  210. var txt = (num % 1 ? num.toFixed(1) : String(num)) + 'F';
  211. return {key: txt, label: txt, rank: num * 10};
  212. }
  213. var cm = /^(\d)([A-Z])(\w+)$/i.exec(tk);
  214. if (cm) {
  215. var n2 = parseInt(cm[1], 10);
  216. return {key: n2 + 'F', label: n2 + 'F', rank: n2 * 10};
  217. }
  218. }
  219. return {key: 'NA', label: t('l2.floor_na', 'Other'), rank: -500};
  220. }
  221. function baseNetworkOf(n) {
  222. var s = String((n && n.name) || '').toUpperCase();
  223. if (s.indexOf('SURVE') >= 0) return 'SURVE';
  224. if (s.indexOf('OFFICE') >= 0) return 'OFFICE';
  225. var ip = String((n && n.ip) || '');
  226. if (/^10\.169\./.test(ip)) return 'SURVE';
  227. if (/^10\.160\./.test(ip)) return 'OFFICE';
  228. return 'OTHER';
  229. }
  230. function networkOf(n) { return baseNetworkOf(n); }
  231. function networkLabel(key) {
  232. if (key === 'SURVE') return t('l2.net_surve', 'SURVE 监控网');
  233. if (key === 'OFFICE') return t('l2.net_office', 'OFFICE 办公网');
  234. return t('l2.net_other', '其他网络');
  235. }
  236. function computeDetail(snap, label) {
  237. var all = snap.nodes || [];
  238. var byId = {}, groupNodes = [];
  239. all.forEach(function (n) { byId[n.id] = n; if (groupOf(n).label === label) groupNodes.push(n); });
  240. // Split one workshop by physical network first: office and surveillance never mix.
  241. var gids = {}, rawAdj = {};
  242. groupNodes.forEach(function (n) { gids[n.id] = true; rawAdj[n.id] = []; });
  243. (snap.edges || []).forEach(function (e) {
  244. if (gids[e.source] && gids[e.target]) { rawAdj[e.source].push(e.target); rawAdj[e.target].push(e.source); }
  245. });
  246. var netMap = {};
  247. groupNodes.forEach(function (n) { netMap[n.id] = baseNetworkOf(n); });
  248. // Unlabelled access switches inherit the network of the distribution they hang from.
  249. for (var pass = 0; pass < 4; pass++) {
  250. groupNodes.forEach(function (n) {
  251. if (netMap[n.id] !== 'OTHER') return;
  252. var votes = {};
  253. rawAdj[n.id].forEach(function (id) {
  254. var k = netMap[id];
  255. if (k && k !== 'OTHER') votes[k] = (votes[k] || 0) + 1;
  256. });
  257. var best = null, bestN = 0;
  258. Object.keys(votes).forEach(function (k) { if (votes[k] > bestN) { best = k; bestN = votes[k]; } });
  259. if (best) netMap[n.id] = best;
  260. });
  261. }
  262. var counts = {}, nets = [];
  263. groupNodes.forEach(function (n) {
  264. var k = netMap[n.id];
  265. if (!counts[k]) { counts[k] = 0; nets.push(k); }
  266. counts[k]++;
  267. });
  268. nets.sort(function (a, b) {
  269. if (a === 'SURVE') return -1;
  270. if (b === 'SURVE') return 1;
  271. if (a === 'OFFICE') return -1;
  272. if (b === 'OFFICE') return 1;
  273. return counts[b] - counts[a];
  274. });
  275. var activeNet = (detailNet && counts[detailNet]) ? detailNet : (nets.length ? nets[0] : 'OTHER');
  276. var nodes = groupNodes.filter(function (n) { return netMap[n.id] === activeNet; });
  277. var ids = {};
  278. nodes.forEach(function (n) { ids[n.id] = true; });
  279. var internal = [], external = [];
  280. (snap.edges || []).forEach(function (e) {
  281. var a = ids[e.source], b = ids[e.target];
  282. if (a && b) internal.push(e);
  283. else if (a || b) external.push(e);
  284. });
  285. var adj = {};
  286. nodes.forEach(function (n) { adj[n.id] = []; });
  287. internal.forEach(function (e) { adj[e.source].push(e.target); adj[e.target].push(e.source); });
  288. // Distribution switches are the left-hand roots of the workshop tree.
  289. var dists = nodes.filter(function (n) { return /dist/i.test(n.tier || '') && /dist/i.test(n.name || ''); })
  290. .sort(function (a, b) { return (adj[b.id].length - adj[a.id].length) || String(a.name).localeCompare(String(b.name)); });
  291. if (!dists.length) {
  292. var cand = nodes.slice().sort(function (a, b) { return adj[b.id].length - adj[a.id].length; })[0];
  293. if (cand) dists = [cand];
  294. }
  295. // One physical root per network tab; peer/redundant distribution switches are listed below it.
  296. if (dists.length > 1) dists = [dists[0]];
  297. // Group all non-distribution switches into floor buckets (GRD lowest, then 1F/2F/3F...).
  298. var floorMap = {};
  299. nodes.forEach(function (n) {
  300. if (dists.indexOf(n) >= 0) return;
  301. if (/core/i.test(n.tier || '')) return;
  302. var fi = floorInfo(n);
  303. var f = floorMap[fi.key] || (floorMap[fi.key] = {key: fi.key, label: fi.label, rank: fi.rank, nodes: []});
  304. f.nodes.push(n);
  305. });
  306. var floors = Object.keys(floorMap).map(function (k) { return floorMap[k]; });
  307. floors.forEach(function (f) {
  308. f.nodes.sort(function (a, b) {
  309. var pa = parseName(a.name), pb = parseName(b.name);
  310. var ca = (pa.cab || '') + (pa.u || ''), cb = (pb.cab || '') + (pb.u || '');
  311. if (ca !== cb) return ca.localeCompare(cb);
  312. return String(a.name).localeCompare(String(b.name));
  313. });
  314. });
  315. // Highest floor at the top, GRD at the bottom.
  316. floors.sort(function (a, b) { return b.rank - a.rank; });
  317. var WORK_L = 300, FLOOR_X = 520, DIST_FRAME_W = 190, TOP_OFF = 34;
  318. var ROW_W = 380, ROW_H = 24, ROW_GAP = 8, FLOOR_HEAD = 28, FLOOR_GAP = 12;
  319. var floorW = ROW_W + PADX * 2 + 110;
  320. floors.forEach(function (f) {
  321. f.cols = 1;
  322. f.rows = f.nodes.length;
  323. f.innerW = ROW_W;
  324. f.h = FLOOR_HEAD + f.rows * ROW_H + Math.max(0, f.rows - 1) * ROW_GAP + 10;
  325. });
  326. var workRight = FLOOR_X + floorW + 14;
  327. var workW = workRight - WORK_L;
  328. var pos = {}, top = 44 + TOP_OFF;
  329. floors.forEach(function (f) {
  330. f.frame = {x: FLOOR_X, y: top, w: floorW, h: f.h};
  331. f.nodes.forEach(function (n, i) {
  332. pos[n.id] = {
  333. x: FLOOR_X + PADX + ROW_W / 2,
  334. y: top + FLOOR_HEAD + i * (ROW_H + ROW_GAP) + ROW_H / 2
  335. };
  336. });
  337. top += f.h + FLOOR_GAP;
  338. });
  339. var workH = Math.max(220, top + 6);
  340. var distFrame = dists.length ? {x: WORK_L + 8, y: 44 + TOP_OFF, w: DIST_FRAME_W, h: workH - (44 + TOP_OFF) - 14} : null;
  341. var distX = WORK_L + 8 + DIST_FRAME_W / 2;
  342. if (dists.length) {
  343. var childYs = floors.reduce(function (acc, f) {
  344. return acc.concat(f.nodes.map(function (n) { return pos[n.id] ? pos[n.id].y : 0; }));
  345. }, []);
  346. var cy = childYs.length ? (Math.min.apply(null, childYs) + Math.max.apply(null, childYs)) / 2 : distFrame.y + distFrame.h / 2;
  347. pos[dists[0].id] = {x: distX, y: cy};
  348. }
  349. var missing = nodes.filter(function (n) { return !pos[n.id]; });
  350. missing.forEach(function (n, i) {
  351. pos[n.id] = {x: distX, y: (distFrame ? distFrame.y + 30 : 80) + i * (NODE_H + 12)};
  352. });
  353. // One ghost core per external core→distribution uplink, aligned left of its distribution switch.
  354. var ghosts = [], ghostSeen = {};
  355. external.forEach(function (e) {
  356. var inside = ids[e.source] ? e.source : e.target;
  357. var outside = ids[e.source] ? e.target : e.source;
  358. if (!pos[inside] || !byId[outside]) return;
  359. // Only a real core uplink gets a ghost; other cross-group edges are not drawn as cores.
  360. if (!/core/i.test(byId[outside].tier || '') && !/core/i.test(byId[outside].name || '')) return;
  361. var key = outside + '|' + inside;
  362. if (ghostSeen[key]) return;
  363. var g = {id: 'gh_' + outside + '_' + inside, to: inside, node: byId[outside], speed: e.speed, state: e.state};
  364. ghostSeen[key] = g;
  365. ghosts.push(g);
  366. });
  367. var ghostAt = {};
  368. ghosts.forEach(function (g) {
  369. var base = pos[g.to];
  370. var k = ghostAt[g.to] || 0;
  371. ghostAt[g.to] = k + 1;
  372. g.x = 92;
  373. g.y = base.y + (k ? (k % 2 ? 1 : -1) * 46 * Math.ceil(k / 2) : 0);
  374. });
  375. // Root each tree at the distribution switch; remember every child's parent for path highlighting.
  376. var parentOf = {}, ghostOfDist = {};
  377. ghosts.forEach(function (g) { if (!ghostOfDist[g.to]) ghostOfDist[g.to] = g.id; });
  378. var seen = {};
  379. dists.forEach(function (d) {
  380. if (seen[d.id]) return;
  381. seen[d.id] = true;
  382. parentOf[d.id] = ghostOfDist[d.id] || null;
  383. var queue = [d.id];
  384. while (queue.length) {
  385. var cur = queue.shift();
  386. adj[cur].forEach(function (nb) {
  387. if (seen[nb]) return;
  388. seen[nb] = true;
  389. parentOf[nb] = cur;
  390. queue.push(nb);
  391. });
  392. }
  393. });
  394. nodes.forEach(function (n) { if (!seen[n.id]) seen[n.id] = true; });
  395. var workshop = {x: WORK_L, y: 0, w: workW, h: workH, label: label + ' · ' + networkLabel(activeNet) + ' · ' + nodes.length + ' ' + t('l2.unit_sw', 'sw')};
  396. var distFrameFull = distFrame ? {
  397. x: distFrame.x, y: distFrame.y, w: distFrame.w, h: distFrame.h,
  398. label: t('l2.dist', 'Distribution') + ' · ' + dists.length
  399. } : null;
  400. floors.forEach(function (f) {
  401. f.frame.label = f.label + ' · ' + f.nodes.length + ' ' + t('l2.unit_sw', 'sw');
  402. });
  403. var root = {x: -20, y: -48, w: workRight + 40, h: workH + 80};
  404. root.cx = root.x + root.w / 2; root.cy = root.y + root.h / 2;
  405. return {
  406. nodes: nodes, byId: byId, pos: pos, ghosts: ghosts, parentOf: parentOf,
  407. dists: dists, floors: floors, frames: {workshop: workshop, dist: distFrameFull},
  408. root: root, totalW: workRight, ids: ids, internal: internal,
  409. nets: nets.map(function (k) { return {key: k, count: counts[k]}; }), activeNet: activeNet, totalNodes: groupNodes.length
  410. };
  411. }
  412. /* ---------------- one-chart layout: fixed columns by tier ----------------
  413. Left to right: physical core → each network's main switch → every
  414. workshop distribution switch → access switches. A workshop frame spans
  415. the distribution + access columns; each floor is a small frame inside it.
  416. Same workshop on two networks is always two separate frames. */
  417. var CORE_X = 130, MAIN_X = 430, DIST_X = 790, ACCESS_X = 1300;
  418. var CORE_W = 200, MAIN_W = 300, NODE_W3 = 300;
  419. var WORK_LEFT = 610, WORK_RIGHT = 1530;
  420. var FLOOR_LEFT = 1130, FLOOR_RIGHT = 1525;
  421. var CHART_W = 1560;
  422. var SEC_HEAD = 46, CORE_ROW = 92, WORK_TOP = 148;
  423. var ROW_H = 24, ROW_GAP = 3, WS_GAP = 10, SEC_GAP = 34;
  424. function makeNetworks() {
  425. var raw = CFG.networks;
  426. if (!raw || !raw.length) {
  427. raw = [
  428. {key: 'SURVE', label: t('l2.net_surve', 'SURVE 监控网'), keywords: ['SURVE', '10.169.'], color: '#2E75B6'},
  429. {key: 'OFFICE', label: t('l2.net_office', 'OFFICE 办公网'), keywords: ['OFFICE', '10.160.'], color: '#548235'}
  430. ];
  431. }
  432. return raw.map(function (n, i) {
  433. var kws = n.keywords;
  434. if (!(kws instanceof Array)) kws = String(kws || '').split(/[,,;;\s]+/);
  435. return {
  436. key: String(n.key || ('NET' + i)).trim().toUpperCase(),
  437. label: String(n.label || n.key || ('NET' + i)),
  438. keywords: kws.map(function (s) { return String(s).trim(); }).filter(Boolean),
  439. color: /^#[0-9a-fA-F]{6}$/.test(String(n.color || '')) ? n.color : '#607D8B'
  440. };
  441. });
  442. }
  443. var NETWORKS = makeNetworks();
  444. function netLabel(key) {
  445. for (var i = 0; i < NETWORKS.length; i++) if (NETWORKS[i].key === key) return NETWORKS[i].label;
  446. return key === 'OTHER' ? t('l2.net_other', '其他网络') : String(key || '');
  447. }
  448. function netColor(key) {
  449. for (var i = 0; i < NETWORKS.length; i++) if (NETWORKS[i].key === key) return NETWORKS[i].color;
  450. return '#607D8B';
  451. }
  452. function rawNetOf(n) {
  453. var hay = (String((n && n.name) || '') + ' ' + String((n && n.ip) || '')).toUpperCase();
  454. for (var i = 0; i < NETWORKS.length; i++) {
  455. for (var j = 0; j < NETWORKS[i].keywords.length; j++) {
  456. if (hay.indexOf(String(NETWORKS[i].keywords[j]).toUpperCase()) >= 0) return NETWORKS[i].key;
  457. }
  458. }
  459. return 'OTHER';
  460. }
  461. // Core/root switch rules are configured from the L2 tab, one row per core.
  462. // Multiple cores in the same network are supported and all of them are
  463. // placed in the left-most core column.
  464. function coreRules() {
  465. var r = CFG.core_rules;
  466. return (r instanceof Array) ? r : [];
  467. }
  468. function coreRuleHit(rule, n) {
  469. var pattern = String(rule.pattern || '');
  470. if (!pattern) return false;
  471. var net = String(rule.net || '').toUpperCase();
  472. if (net) {
  473. var raw = rawNetOf(n);
  474. // A core with no recognisable network keyword may still be assigned by
  475. // the rule; a switch already known to belong to another network must
  476. // not be stolen by a broad pattern.
  477. if (raw !== 'OTHER' && raw !== net) return false;
  478. }
  479. var hay = String((n && n.name) || '') + ' ' + String((n && n.ip) || '');
  480. if (String(rule.match || 'contains') === 'regex') {
  481. try { return new RegExp(pattern, 'i').test(hay); } catch (e) { return false; }
  482. }
  483. return hay.toLowerCase().indexOf(pattern.toLowerCase()) >= 0;
  484. }
  485. function configuredCore(n) {
  486. var rules = coreRules();
  487. for (var i = 0; i < rules.length; i++) if (coreRuleHit(rules[i], n)) return true;
  488. return false;
  489. }
  490. function configuredCoreNet(n) {
  491. var rules = coreRules();
  492. for (var i = 0; i < rules.length; i++) {
  493. if (coreRuleHit(rules[i], n) && String(rules[i].net || '').trim() !== '') return String(rules[i].net).toUpperCase();
  494. }
  495. return '';
  496. }
  497. // Network is decided first by name/IP keyword, then by the rest of the
  498. // workshop, then by graph adjacency. No switch is ever shown under the
  499. // wrong upstream network.
  500. function assignNetworks(snap) {
  501. var nodes = snap.nodes || [], byId = {}, net = {}, adj = {};
  502. nodes.forEach(function (n) {
  503. byId[n.id] = n; net[n.id] = rawNetOf(n); adj[n.id] = [];
  504. var cn = configuredCoreNet(n);
  505. if (cn) net[n.id] = cn;
  506. });
  507. (snap.edges || []).forEach(function (e) {
  508. if (adj[e.source] && adj[e.target]) { adj[e.source].push(e.target); adj[e.target].push(e.source); }
  509. });
  510. var groupVote = {};
  511. nodes.forEach(function (n) {
  512. var k = groupOf(n).label, v = net[n.id];
  513. if (v && v !== 'OTHER') (groupVote[k] = groupVote[k] || {})[v] = true;
  514. });
  515. nodes.forEach(function (n) {
  516. if (net[n.id] !== 'OTHER') return;
  517. var v = groupVote[groupOf(n).label], keys = v ? Object.keys(v) : [];
  518. if (keys.length === 1) net[n.id] = keys[0];
  519. });
  520. for (var pass = 0; pass < 8; pass++) {
  521. var changed = false;
  522. nodes.forEach(function (n) {
  523. if (net[n.id] !== 'OTHER') return;
  524. var votes = {};
  525. adj[n.id].forEach(function (id) { if (net[id] && net[id] !== 'OTHER') votes[net[id]] = (votes[net[id]] || 0) + 1; });
  526. var best = null, bn = 0;
  527. Object.keys(votes).forEach(function (k) { if (votes[k] > bn) { best = k; bn = votes[k]; } });
  528. if (best) { net[n.id] = best; changed = true; }
  529. });
  530. if (!changed) break;
  531. }
  532. return net;
  533. }
  534. function roleOf(n) {
  535. var p = parseName(n.name || '');
  536. var r = String(p.role || '').toLowerCase();
  537. var tier = String(n.tier || '').toLowerCase();
  538. if (configuredCore(n)) return 'core';
  539. // Backward-compatible fallback only when the core list is absent (old
  540. // config). An explicitly empty list means the administrator wants no
  541. // configured core column.
  542. if (!(CFG.core_rules instanceof Array) && (/^core(\s|-)/i.test(String(n.name || '')) || groupOf(n).label === 'Core')) return 'core';
  543. if (tier === 'core' || r === 'core') return 'main';
  544. if (r === 'dist') return 'dist';
  545. if (r === 'access') return 'access';
  546. if (tier.indexOf('dist') === 0) return 'dist';
  547. return 'access';
  548. }
  549. function roleName(r) {
  550. if (r === 'core') return t('l2.core', '核心');
  551. if (r === 'main') return t('l2.main', '主交换');
  552. if (r === 'dist') return t('l2.dist', '汇聚');
  553. return t('l2.access', '接入');
  554. }
  555. function shortLoc(n) {
  556. var p = parseName(n.name || '');
  557. if (!p.floor) return '';
  558. return p.floor + (p.cab || '') + (p.u ? '-' + p.u : '');
  559. }
  560. function switchLabel(n, role, netKey) {
  561. var g = groupOf(n).label;
  562. if (role === 'core') return roleName('core') + ' · ' + (netKey || rawNetOf(n)) + ' · ' + (n.ip || '');
  563. if (role === 'main') return roleName('main') + ' · ' + g + ' · ' + (n.ip || '');
  564. var loc = shortLoc(n);
  565. return [g, loc, roleName(role)].filter(Boolean).join(' · ') + ' · ' + (n.ip || '');
  566. }
  567. // A workshop is the physical area served by one distribution switch, not
  568. // the four-digit prefix printed on each access switch. The same dist can
  569. // serve access switches in several rooms/buildings; they must share one
  570. // frame, otherwise their uplink line would cross unrelated frames.
  571. function workshopKeys(snap) {
  572. var byId = {}, adj = {}, keys = {};
  573. (snap.nodes || []).forEach(function (n) { byId[n.id] = n; adj[n.id] = []; });
  574. (snap.edges || []).forEach(function (e) {
  575. if (adj[e.source] && adj[e.target]) { adj[e.source].push(e.target); adj[e.target].push(e.source); }
  576. });
  577. (snap.nodes || []).forEach(function (n) {
  578. var r = roleOf(n);
  579. if (r === 'core' || r === 'main') return;
  580. if (r === 'dist') { keys[n.id] = groupOf(n).label; return; }
  581. var ds = (adj[n.id] || []).map(function (id) { return byId[id]; })
  582. .filter(function (d) { return d && roleOf(d) === 'dist'; });
  583. var same = ds.filter(function (d) { return groupOf(d).label === groupOf(n).label; });
  584. var pick = same[0] || ds[0];
  585. keys[n.id] = pick ? groupOf(pick).label : groupOf(n).label;
  586. });
  587. return keys;
  588. }
  589. function workshopList(snap, netMap, netKey, wsKeys) {
  590. var map = {}, order = [];
  591. (snap.nodes || []).forEach(function (n) {
  592. if (netMap[n.id] !== netKey) return;
  593. var r = roleOf(n);
  594. if (r === 'core' || r === 'main') return;
  595. var g = (wsKeys && wsKeys[n.id]) || groupOf(n).label;
  596. if (!map[g]) { map[g] = {key: g, label: g, extra: [], net: netKey, dists: [], access: [], all: []}; order.push(g); }
  597. map[g].all.push(n);
  598. if (r === 'dist') map[g].dists.push(n); else map[g].access.push(n);
  599. });
  600. return order.map(function (g) { return map[g]; }).filter(function (w) { return w.all.length > 0; })
  601. .map(function (w) {
  602. w.access.forEach(function (a) {
  603. var raw = groupOf(a).label;
  604. if (raw !== w.key && w.extra.indexOf(raw) < 0) w.extra.push(raw);
  605. });
  606. w.label = w.key + (w.extra.length ? ' + ' + w.extra.slice(0, 3).join('/') + (w.extra.length > 3 ? '…' : '') : '');
  607. return w;
  608. });
  609. }
  610. function layoutWorkshop(w, top) {
  611. var rowH = ROW_H + (showTerm ? 24 : 0);
  612. var floorMap = {}, floors = [];
  613. w.access.forEach(function (n) {
  614. var fi = floorInfo(n);
  615. var k = fi.key;
  616. if (!floorMap[k]) { floorMap[k] = {key: k, label: fi.label, rank: (fi.key === 'NA' ? 999 : fi.rank), nodes: [], rows: []}; floors.push(floorMap[k]); }
  617. floorMap[k].nodes.push(n);
  618. });
  619. floors.sort(function (a, b) { return a.rank - b.rank || String(a.label).localeCompare(String(b.label)); });
  620. var accessH = 0;
  621. floors.forEach(function (f) {
  622. f.nodes.sort(function (a, b) { return String(a.name).localeCompare(String(b.name)); });
  623. f.h = 22 + f.nodes.length * rowH + 6;
  624. accessH += f.h;
  625. });
  626. if (floors.length > 1) accessH += (floors.length - 1) * 5;
  627. var distH = w.dists.length ? (w.dists.length * 64 + (w.dists.length - 1) * 8) : 0;
  628. var inner = Math.max(accessH, distH, 54);
  629. var h = 38 + inner + 10;
  630. w.top = top; w.h = h; w.rowH = rowH;
  631. // Floors are stacked bottom-up: GRD lowest, then 1F → 3F → 4F → 7F.
  632. var cursor = top + h - 8;
  633. floors.forEach(function (f) {
  634. cursor -= f.h;
  635. f.frame = {x: FLOOR_LEFT, y: cursor, w: FLOOR_RIGHT - FLOOR_LEFT, h: f.h};
  636. f.nodes.forEach(function (n, i) {
  637. f.rows.push({id: n.id, x: ACCESS_X, y: cursor + 22 + rowH / 2 + i * rowH});
  638. });
  639. cursor -= 5;
  640. });
  641. var dstart = top + 38 + (inner - distH) / 2;
  642. w.distPos = w.dists.map(function (n, i) {
  643. return {id: n.id, x: DIST_X, y: dstart + 32 + i * 72};
  644. });
  645. w.floors = floors;
  646. w.title = w.label + ' · ' + netLabel(w.net) + ' · ' + w.all.length + ' ' + t('l2.unit_sw', '台');
  647. // A workshop without a distribution switch only needs a box around its
  648. // access column; keeping the full dist-column width would leave a large
  649. // empty rectangle and make the chart look sparse.
  650. var frameLeft = w.dists.length ? WORK_LEFT : FLOOR_LEFT - 40;
  651. w.frame = {x: frameLeft, y: top, w: WORK_RIGHT - frameLeft, h: h};
  652. return h;
  653. }
  654. function computeFull(snap) {
  655. var nodes = snap.nodes || [];
  656. var netMap = assignNetworks(snap);
  657. var sections = [], placements = [], frames = [], roleMap = {};
  658. var wsKeys = workshopKeys(snap);
  659. var y = 100;
  660. NETWORKS.forEach(function (net) {
  661. var secNodes = nodes.filter(function (n) { return netMap[n.id] === net.key; });
  662. if (!secNodes.length) return;
  663. var cores = [], main = null;
  664. secNodes.forEach(function (n) {
  665. roleMap[n.id] = roleOf(n);
  666. if (roleMap[n.id] === 'core') cores.push(n);
  667. if (roleMap[n.id] === 'main' && !main) main = n;
  668. });
  669. var ws = workshopList(snap, netMap, net.key, wsKeys);
  670. var sec = {
  671. key: net.key, label: netLabel(net.key) + ' · ' + secNodes.length + ' ' + t('l2.unit_sw', '台'),
  672. x: 10, y: y, w: CHART_W - 20, h: 0, color: netColor(net.key),
  673. core: cores[0] || null, cores: cores, main: main, workshops: ws
  674. };
  675. var cur = y + WORK_TOP;
  676. ws.forEach(function (w) {
  677. layoutWorkshop(w, cur);
  678. cur += w.h + WS_GAP;
  679. frames.push({id: 'ws_' + net.key + '_' + w.key, type: 'wsframe', x: w.frame.x, y: w.frame.y, w: w.frame.w, h: w.frame.h,
  680. label: w.title, group: w.key, net: net.key, color: net.color});
  681. w.floors.forEach(function (f, fi) {
  682. frames.push({id: 'ff_' + net.key + '_' + w.key + '_' + fi, type: 'floorframe', x: f.frame.x, y: f.frame.y,
  683. w: f.frame.w, h: f.frame.h, label: f.label + ' · ' + f.nodes.length + ' ' + t('l2.unit_sw', '台'), net: net.key, color: net.color});
  684. });
  685. w.distPos.forEach(function (p) {
  686. var n = nodes.filter(function (x) { return x.id === p.id; })[0];
  687. if (n) placements.push({node: n, role: 'dist', x: p.x, y: p.y, w: NODE_W3, h: 26, net: net.key, group: w.key, ws: net.key + '|' + w.key});
  688. });
  689. w.floors.forEach(function (f) {
  690. f.rows.forEach(function (p) {
  691. var n = nodes.filter(function (x) { return x.id === p.id; })[0];
  692. if (n) placements.push({node: n, role: 'access', x: p.x, y: p.y, w: NODE_W3, h: 26, net: net.key, group: w.key, ws: net.key + '|' + w.key});
  693. });
  694. });
  695. });
  696. sec.h = (cur - y) + 16;
  697. // The network main switch stays at the top of its tier column (its access
  698. // children sit directly under it, as in the reference chart); the core
  699. // sits lower in the column so its uplinks fan symmetrically.
  700. sec.mainY = y + SEC_HEAD + 46;
  701. sec.coreY = y + Math.max(CORE_ROW + SEC_HEAD, Math.round(sec.h * 0.55));
  702. if (cores.length) {
  703. var coreStep = 64;
  704. var coreTop = sec.coreY - ((cores.length - 1) * coreStep) / 2;
  705. cores.forEach(function (cn, ci) {
  706. placements.push({node: cn, role: 'core', x: CORE_X, y: coreTop + ci * coreStep, w: CORE_W, h: 54, net: net.key, group: 'Core'});
  707. });
  708. }
  709. if (main) placements.push({node: main, role: 'main', x: MAIN_X, y: sec.mainY, w: MAIN_W, h: 46, net: net.key, group: groupOf(main).label});
  710. sections.push(sec);
  711. y += sec.h + SEC_GAP;
  712. });
  713. var root = {x: 0, y: 0, w: CHART_W, h: y + 6};
  714. return {sections: sections, placements: placements, frames: frames, netMap: netMap, roleMap: roleMap, root: root};
  715. }
  716. // Immediate upstream parent per switch: access -> its workshop dist;
  717. // dist -> core when directly attached, otherwise the network main switch.
  718. function buildParents(snap, L) {
  719. var byId = {}, adj = {}, edgeSpeed = {};
  720. L.placements.forEach(function (p) { byId[p.node.id] = p; });
  721. (snap.nodes || []).forEach(function (n) { adj[n.id] = []; });
  722. (snap.edges || []).forEach(function (e) {
  723. if (!adj[e.source] || !adj[e.target]) return;
  724. adj[e.source].push(e.target); adj[e.target].push(e.source);
  725. edgeSpeed[e.source + '|' + e.target] = edgeSpeed[e.target + '|' + e.source] = parseFloat(e.speed) || 0;
  726. });
  727. var parent = {};
  728. Object.keys(byId).forEach(function (id) {
  729. var role = L.roleMap[id] || 'access';
  730. if (role === 'core') return;
  731. var p = byId[id], cands = (adj[id] || []).map(function (x) { return {id: x, role: L.roleMap[x] || 'access', p: byId[x]}; })
  732. .filter(function (c) { return c.p; });
  733. var want = role === 'main' ? ['core'] : (role === 'dist' ? ['core', 'main'] : ['dist', 'main', 'core']);
  734. var best = null;
  735. for (var wi = 0; wi < want.length && !best; wi++) {
  736. var list = cands.filter(function (c) {
  737. if (c.role !== want[wi]) return false;
  738. if (role === 'access' && want[wi] === 'dist' && c.p.ws && p.ws && c.p.ws !== p.ws) return false;
  739. return true;
  740. });
  741. if (list.length) {
  742. list.sort(function (a, b) { return (edgeSpeed[id + '|' + b.id] || 0) - (edgeSpeed[id + '|' + a.id] || 0); });
  743. best = list[0];
  744. }
  745. }
  746. if (best) parent[id] = best.id;
  747. });
  748. return parent;
  749. }
  750. /* ---------------- speed colour ---------------- */
  751. function speedColor(speed, state) {
  752. if (state && state !== 'up') return colors.down;
  753. var s = parseFloat(speed);
  754. if (!isFinite(s) || s <= 0) return colors.unknown;
  755. if (s <= 100) return colors['100'];
  756. if (s <= 1000) return colors['1000'];
  757. if (s <= 10000) return colors['10000'];
  758. return colors['20000'];
  759. }
  760. function speedWidth(speed) {
  761. var s = parseFloat(speed) || 0;
  762. if (s >= 20000) return 4.5;
  763. if (s >= 10000) return 3.6;
  764. if (s >= 1000) return 2.6;
  765. return 1.8;
  766. }
  767. function speedText(speed) {
  768. var s = parseFloat(speed) || 0;
  769. if (!s) return '?';
  770. if (s >= 1000) return (s / 1000) + 'G';
  771. return s + 'M';
  772. }
  773. /* ---------------- style ---------------- */
  774. function styleFor(d) {
  775. var bg = d ? TIER_BG_DARK : TIER_BG;
  776. return [
  777. {selector: 'node', style: {
  778. 'width': 'data(w)', 'height': 'data(h)', 'shape': 'round-rectangle',
  779. 'background-color': 'data(bg)', 'border-width': 2, 'border-color': 'data(bd)',
  780. 'label': 'data(label)', 'color': d ? '#eaf1fb' : '#ffffff',
  781. 'font-size': 11.5, 'font-weight': 600, 'text-valign': 'center', 'text-halign': 'center',
  782. 'text-wrap': 'wrap', 'text-max-width': NODE_W - 16, 'padding': '2px',
  783. 'overlay-opacity': 0, 'z-index-compare': 'manual', 'z-index': 20
  784. }},
  785. {selector: 'node[type="frame"]', style: {
  786. 'background-opacity': d ? 0.05 : 0.035, 'background-color': d ? '#5b7bb0' : '#8fa6c4',
  787. 'border-width': 1.6, 'border-style': 'dashed', 'border-color': d ? '#3f5377' : '#c2ccdb',
  788. 'label': 'data(label)', 'text-valign': 'top', 'text-halign': 'center',
  789. 'font-size': 'data(fs)', 'font-weight': 800, 'color': d ? '#aebfd8' : '#526178',
  790. 'text-margin-y': 8, 'padding': '0px', 'events': 'no', 'z-index-compare': 'manual', 'z-index': 2
  791. }},
  792. {selector: 'node[type="netframe"]', style: {
  793. 'background-opacity': d ? 0.03 : 0.02, 'background-color': d ? '#7b91b8' : '#adc0d8',
  794. 'border-width': 3, 'border-style': 'solid', 'border-color': 'data(bd)',
  795. 'label': 'data(label)', 'text-valign': 'top', 'text-halign': 'center',
  796. 'font-size': 22, 'font-weight': 900, 'color': d ? '#dbe9ff' : '#29425f',
  797. 'text-margin-y': 8, 'padding': '0px', 'events': 'no',
  798. 'z-index-compare': 'manual', 'z-index': 1
  799. }},
  800. {selector: 'node[type="wsframe"]', style: {
  801. 'background-opacity': d ? 0.02 : 0.014, 'background-color': d ? '#6f8fc4' : '#8fa6c4',
  802. 'border-width': 2.2, 'border-style': 'solid', 'border-color': 'data(bd)',
  803. 'label': 'data(label)', 'text-valign': 'top', 'text-halign': 'center', 'text-wrap': 'none',
  804. 'font-size': 15.5, 'font-weight': 900, 'color': d ? '#d7e6ff' : '#2f4560',
  805. 'text-margin-x': 0, 'text-margin-y': 7, 'padding': '0px', 'events': 'no',
  806. 'z-index-compare': 'manual', 'z-index': 2
  807. }},
  808. {selector: 'node[type="floorframe"]', style: {
  809. 'background-opacity': d ? 0.04 : 0.025, 'background-color': d ? '#5b7bb0' : '#8fa6c4',
  810. 'border-width': 1.1, 'border-style': 'dashed', 'border-color': 'data(bd)',
  811. 'label': 'data(label)', 'text-valign': 'top', 'text-halign': 'center', 'text-wrap': 'none',
  812. 'font-size': 11.5, 'font-weight': 800, 'color': d ? '#a9c1e3' : '#425a78',
  813. 'text-margin-x': 0, 'text-margin-y': 6, 'padding': '0px', 'events': 'no',
  814. 'z-index-compare': 'manual', 'z-index': 3
  815. }},
  816. {selector: 'node[type="root"]', style: {
  817. 'background-opacity': d ? 0.02 : 0.012, 'background-color': d ? '#7aa0d8' : '#8ea6c2',
  818. 'border-width': 2.2, 'border-style': 'solid', 'border-color': d ? '#476492' : '#9fb0c6',
  819. 'label': 'data(label)', 'text-valign': 'top', 'text-halign': 'center',
  820. 'font-size': 28, 'font-weight': 900, 'color': d ? '#cfe0ff' : '#33465f',
  821. 'text-margin-y': 10, 'events': 'no', 'z-index-compare': 'manual', 'z-index': 0
  822. }},
  823. {selector: 'node[type="roottitle"]', style: {
  824. 'width': 'data(w)', 'height': 20, 'shape': 'rectangle', 'background-opacity': 0, 'border-width': 0,
  825. 'label': 'data(label)', 'text-valign': 'center', 'text-halign': 'center', 'text-wrap': 'none',
  826. 'font-size': 24, 'font-weight': 900, 'color': d ? '#cfe0ff' : '#33465f', 'events': 'no',
  827. 'z-index-compare': 'manual', 'z-index': 5
  828. }},
  829. {selector: 'node[type="nettitle"]', style: {
  830. 'width': 'data(w)', 'height': 20, 'shape': 'rectangle', 'background-opacity': 0, 'border-width': 0,
  831. 'label': 'data(label)', 'text-valign': 'center', 'text-halign': 'center', 'text-wrap': 'none',
  832. 'font-size': 20, 'font-weight': 900, 'color': d ? '#dbe9ff' : '#29425f', 'events': 'no',
  833. 'z-index-compare': 'manual', 'z-index': 5
  834. }},
  835. {selector: 'node[type="wslabel"]', style: {
  836. 'width': 'data(w)', 'height': 16, 'shape': 'rectangle', 'background-opacity': 0, 'border-width': 0,
  837. 'label': 'data(label)', 'text-valign': 'center', 'text-halign': 'center', 'text-wrap': 'none',
  838. 'font-size': 15, 'font-weight': 900, 'color': d ? '#d7e6ff' : '#2f4560', 'events': 'no',
  839. 'z-index-compare': 'manual', 'z-index': 5
  840. }},
  841. {selector: 'node[type="floorlabel"]', style: {
  842. 'width': 'data(w)', 'height': 16, 'shape': 'rectangle', 'background-opacity': 0, 'border-width': 0,
  843. 'label': 'data(label)', 'text-valign': 'center', 'text-halign': 'center', 'text-wrap': 'none',
  844. 'font-size': 11, 'font-weight': 800, 'color': d ? '#a9c1e3' : '#425a78', 'events': 'no',
  845. 'z-index-compare': 'manual', 'z-index': 5
  846. }},
  847. {selector: 'node[type="tile"]', style: {
  848. 'width': 'data(w)', 'height': 'data(h)', 'shape': 'round-rectangle',
  849. 'background-color': 'data(bg)', 'background-opacity': 0.97,
  850. 'border-width': 2.5, 'border-color': 'data(bd)',
  851. 'label': 'data(label)', 'color': 'data(fg)',
  852. 'font-size': 16, 'font-weight': 700, 'text-valign': 'center', 'text-halign': 'center',
  853. 'text-wrap': 'wrap', 'text-max-width': TILE_W - 26, 'line-height': 1.45,
  854. 'padding': '6px', 'overlay-opacity': 0, 'z-index-compare': 'manual', 'z-index': 20
  855. }},
  856. {selector: 'node[type="tile"].alarm', style: {'border-width': 5}},
  857. {selector: 'node[type="tile"].core', style: {'border-width': 3.5}},
  858. {selector: 'node[type="ghost"]', style: {
  859. 'width': 190, 'height': 40, 'shape': 'round-rectangle',
  860. 'background-color': d ? '#52667f' : '#c8d3e2', 'background-opacity': 0.55,
  861. 'border-width': 1.2, 'border-style': 'dashed', 'border-color': d ? '#7d93ad' : '#8fa3bc',
  862. 'label': 'data(label)', 'color': d ? '#dce8fa' : '#3b4f68',
  863. 'font-size': 11, 'font-weight': 600, 'text-wrap': 'wrap', 'text-max-width': 176,
  864. 'z-index-compare': 'manual', 'z-index': 15
  865. }},
  866. {selector: 'node[type="edgepoint"]', style: {
  867. 'width': 1, 'height': 1, 'opacity': 0, 'events': 'no'
  868. }},
  869. {selector: 'node[type="term"]', style: {
  870. 'width': 7, 'height': 7, 'shape': 'ellipse', 'background-color': 'data(bg)',
  871. 'border-width': 1, 'border-color': d ? '#2a3648' : '#cfd8e3', 'label': '',
  872. 'z-index-compare': 'manual', 'z-index': 30
  873. }},
  874. {selector: 'edge[type="tilelink"]', style: {
  875. 'width': 'data(w)', 'line-color': 'data(color)', 'opacity': 0.55,
  876. 'curve-style': 'bezier', 'target-arrow-shape': 'none',
  877. 'z-index-compare': 'manual', 'z-index': 5
  878. }},
  879. {selector: 'edge[type="ghost"]', style: {
  880. 'width': 1.2, 'line-color': d ? '#6b809b' : '#a9b6c8', 'line-style': 'dashed', 'opacity': 0.7,
  881. 'curve-style': 'bezier', 'target-arrow-shape': 'none',
  882. 'z-index-compare': 'manual', 'z-index': 8
  883. }},
  884. {selector: 'edge[type="uplink"]', style: {
  885. 'width': 'data(w)', 'line-color': 'data(color)', 'line-style': 'data(ls)', 'opacity': 0.95,
  886. 'curve-style': 'bezier', 'target-arrow-shape': 'none',
  887. 'label': 'data(lbl)', 'font-size': 10, 'font-weight': 700,
  888. 'color': d ? '#dce8fa' : '#25405f',
  889. 'text-background-color': d ? '#0d1b2a' : '#ffffff', 'text-background-opacity': 0.88,
  890. 'text-background-padding': '2px', 'text-rotation': 'autorotate',
  891. 'z-index-compare': 'manual', 'z-index': 40
  892. }},
  893. {selector: 'edge', style: {
  894. 'width': 'data(w)', 'line-color': 'data(color)', 'line-style': 'data(ls)',
  895. 'curve-style': 'bezier', 'opacity': 0.92, 'target-arrow-shape': 'none',
  896. 'z-index-compare': 'manual', 'z-index': 12
  897. }},
  898. {selector: 'edge[type="link"]', style: {
  899. 'width': 'data(w)', 'line-color': 'data(color)', 'line-style': 'data(ls)',
  900. 'opacity': 0.62, 'curve-style': 'straight',
  901. 'source-endpoint': '0% 50%', 'target-endpoint': '0% 50%',
  902. 'target-arrow-shape': 'none', 'z-index-compare': 'manual', 'z-index': 1
  903. }},
  904. {selector: 'edge[type="term"]', style: {
  905. 'width': 0.7, 'line-color': d ? '#3b4a5e' : '#c5cfdb', 'opacity': 0.55,
  906. 'curve-style': 'bezier', 'z-index-compare': 'manual', 'z-index': 11
  907. }},
  908. {selector: 'node.alarm', style: {'border-width': 6}},
  909. {selector: 'node.dim', style: {'opacity': 0.12}},
  910. {selector: 'edge.dim', style: {'opacity': 0.08}},
  911. {selector: 'edge.hl', style: {'width': 5, 'line-color': '#FFD54A', 'opacity': 1, 'z-index-compare': 'manual', 'z-index': 45}},
  912. {selector: 'node.lat-warn', style: {'border-color': '#F39C12'}},
  913. {selector: 'node.lat-bad', style: {'border-color': '#E74C3C', 'border-width': 6}},
  914. {selector: 'node.hl', style: {'border-color': '#ff0', 'border-width': 7, 'overlay-color': '#ff0', 'overlay-opacity': 0.25, 'overlay-padding': 6}},
  915. {selector: 'node.pathhl', style: {'border-width': 5, 'border-color': '#FFD54A', 'overlay-color': '#FFD54A', 'overlay-opacity': 0.2, 'overlay-padding': 4}},
  916. {selector: 'edge.pathhl', style: {'width': 5.5, 'line-color': '#FFD54A', 'opacity': 1}},
  917. {selector: 'edge.detail', style: {'opacity': 0.72, 'curve-style': 'straight', 'source-endpoint': '0% 50%', 'target-endpoint': '0% 50%'}},
  918. {selector: 'node[type="switch"]', style: {
  919. 'background-color': function (e) { return e.data('bg'); },
  920. 'text-max-width': 'data(tw)', 'text-wrap': 'none',
  921. 'text-halign': 'center', 'font-size': 10.5
  922. }},
  923. {selector: 'node[type="switch"].root', style: {'text-wrap': 'wrap'}}
  924. ];
  925. }
  926. /* ---------------- render ---------------- */
  927. // One tall column chart cannot be squeezed into a wide window without
  928. // wasting half the canvas. The initial view fits the chart WIDTH and
  929. // anchors at the top (core switches first); the user can pan down or press
  930. // "适应窗口" to see the entire graph. This keeps labels readable.
  931. function initialView() {
  932. if (!cy) return;
  933. var bb = cy.elements().boundingBox();
  934. if (!bb || !bb.w || !bb.h) return;
  935. var zw = (cy.width() - 30) / bb.w;
  936. var zh = (cy.height() - 30) / bb.h;
  937. var tall = (bb.h * zw > cy.height() * 1.5);
  938. var z = tall ? zw * 0.95 : Math.min(zw, zh);
  939. cy.zoom({level: z});
  940. if (tall) {
  941. cy.pan({x: 15 - bb.x * z, y: 28 - bb.y * z});
  942. } else {
  943. cy.pan({x: (cy.width() - bb.w * z) / 2 - bb.x * z, y: (cy.height() - bb.h * z) / 2 - bb.y * z});
  944. }
  945. }
  946. function applyElements(els, keepView) {
  947. if (!cy) {
  948. cy = cytoscape({
  949. container: el('l2-cy'),
  950. elements: els,
  951. style: styleFor(dark),
  952. layout: {name: 'preset', fit: false},
  953. wheelSensitivity: 0.2,
  954. minZoom: 0.02, maxZoom: 4
  955. });
  956. bindCy();
  957. setTimeout(initialView, 0);
  958. } else {
  959. var z = cy.zoom(), p = cy.pan();
  960. cy.elements().remove();
  961. cy.add(els);
  962. cy.style(styleFor(dark));
  963. cy.layout({name: 'preset', fit: false}).run();
  964. if (keepView) { cy.zoom(z); cy.pan(p); }
  965. else { initialView(); }
  966. }
  967. }
  968. function fitGroup(label) {
  969. if (!cy || !label) return;
  970. var eles = cy.nodes().filter(function (n) {
  971. return n.data('blockGroup') === label || n.data('group') === label;
  972. });
  973. if (eles.length) cy.fit(eles, 70);
  974. }
  975. function render(snap) {
  976. if (!snap || !snap.nodes) {
  977. el('l2-cy').innerHTML = '<div class="l2-empty">' + t('l2.no_data', 'No data') + '</div>';
  978. return;
  979. }
  980. lastSnap = snap;
  981. renderFull(snap);
  982. updateStats(snap);
  983. buildLegend();
  984. buildAlarms(snap);
  985. buildOffline(snap);
  986. }
  987. function renderFull(snap) {
  988. var L = computeFull(snap), els = [];
  989. if (!L.placements.length) { applyElements([], false); return; }
  990. var byId = {};
  991. L.placements.forEach(function (p) { byId[p.node.id] = p; });
  992. els.push({group: 'nodes', data: {id: '__root', type: 'root', label: '', w: L.root.w, h: L.root.h},
  993. position: {x: L.root.w / 2, y: L.root.h / 2}, selectable: false, grabbable: false});
  994. els.push({group: 'nodes', data: {id: '__roottitle', type: 'roottitle', label: CFG.root_label || 'COBCO', w: L.root.w, h: 20},
  995. position: {x: L.root.w / 2, y: 22}, selectable: false, grabbable: false});
  996. L.sections.forEach(function (sec, si) {
  997. els.push({group: 'nodes', data: {
  998. id: '__net_' + si, type: 'netframe', label: '', w: sec.w, h: sec.h, net: sec.key, bd: sec.color
  999. }, position: {x: sec.x + sec.w / 2, y: sec.y + sec.h / 2}, selectable: false, grabbable: false});
  1000. els.push({group: 'nodes', data: {id: '__nettitle_' + si, type: 'nettitle', label: sec.label, w: sec.w, h: 20},
  1001. position: {x: sec.x + sec.w / 2, y: sec.y + 21}, selectable: false, grabbable: false});
  1002. });
  1003. L.frames.forEach(function (f) {
  1004. els.push({group: 'nodes', data: {
  1005. id: f.id, type: f.type, label: '', w: f.w, h: f.h, group: f.group || '', net: f.net, bd: f.color || netColor(f.net)
  1006. }, position: {x: f.x + f.w / 2, y: f.y + f.h / 2}, selectable: false, grabbable: false});
  1007. var tt = f.type === 'floorframe' ? 'floorlabel' : 'wslabel';
  1008. var ty = f.type === 'floorframe' ? 11 : 18;
  1009. els.push({group: 'nodes', data: {id: 'label_' + f.id, type: tt, label: f.label, w: f.w, h: 16},
  1010. position: {x: f.x + f.w / 2, y: f.y + ty}, selectable: false, grabbable: false});
  1011. });
  1012. L.placements.forEach(function (p) {
  1013. var n = p.node, cls = [];
  1014. var bg = p.role === 'core' ? TIER_BG.core : (p.role === 'main' ? '#2E75B6' : (p.role === 'dist' ? TIER_BG.dist : TIER_BG.access));
  1015. var bd = bg;
  1016. if (n.alarms && n.alarms.length) { cls.push('alarm'); bd = '#E45959'; }
  1017. if (n.latency_ms != null) {
  1018. if (+n.latency_ms > LATB) cls.push('lat-bad');
  1019. else if (+n.latency_ms > LATW) cls.push('lat-warn');
  1020. }
  1021. if (p.role === 'core') cls.push('root');
  1022. els.push({group: 'nodes', data: {
  1023. id: n.id, type: 'switch', name: n.name, ip: n.ip, tier: n.tier, depth: n.depth, hostid: n.hostid, oper: n.oper,
  1024. maxsev: n.maxsev, alarms: n.alarms || [], term_online: n.term_online, term_offline: n.term_offline, latency_ms: n.latency_ms,
  1025. label: switchLabel(n, p.role, p.net), tw: p.w - 12, bg: bg, bd: bd, w: p.w, h: p.h,
  1026. group: p.group + ' · ' + netLabel(p.net), blockGroup: p.group, net: p.net, role: p.role
  1027. }, position: {x: p.x, y: p.y}, classes: cls.join(' ')});
  1028. });
  1029. // Every protocol-discovered link = one straight segment. Endpoints sit at
  1030. // the label left-edge midpoint; the labels paint on top, so a parent line
  1031. // starts under the parent label and ends exactly at the child's left edge.
  1032. pathParent = buildParents(snap, L);
  1033. (snap.edges || []).forEach(function (e, i) {
  1034. if (!byId[e.source] || !byId[e.target]) return;
  1035. var col = speedColor(e.speed, e.state);
  1036. els.push({group: 'edges', data: {
  1037. id: 'e_' + i, type: 'link', source: e.source, target: e.target, p: e.source, c: e.target,
  1038. speed: e.speed, state: e.state, a_port: e.a_port, b_port: e.b_port,
  1039. color: col, ls: (e.state && e.state !== 'up') ? 'dashed' : 'solid', w: Math.min(4, speedWidth(e.speed))
  1040. }});
  1041. });
  1042. if (showTerm) addTerminals(L, byId, els);
  1043. applyElements(els, true);
  1044. if (focusGroupLabel) {
  1045. setTimeout(function () { fitGroup(focusGroupLabel); }, 80);
  1046. focusGroupLabel = null;
  1047. }
  1048. }
  1049. function addTerminals(L, byId, els) {
  1050. var bySw = {};
  1051. ((lastSnap && lastSnap.terminals) || []).forEach(function (tm) {
  1052. if (!byId[tm.sw_ip]) return;
  1053. if (onlyOff && tm.online) return;
  1054. (bySw[tm.sw_ip] = bySw[tm.sw_ip] || []).push(tm);
  1055. });
  1056. Object.keys(bySw).forEach(function (sw) {
  1057. var p = byId[sw]; if (!p || p.role === 'core' || p.role === 'main') return;
  1058. bySw[sw].sort(function (a, b) { return (a.online === b.online) ? 0 : (a.online ? -1 : 1); });
  1059. bySw[sw].slice(0, 24).forEach(function (tm, k) {
  1060. var col = k % 12, row = Math.floor(k / 12);
  1061. var x = p.x - p.w / 2 + 16 + col * 9;
  1062. var y = p.y + 16 + row * 9;
  1063. els.push({group: 'nodes', data: {id: 't_' + tm.mac, type: 'term', bg: tm.online ? '#2ECC71' : '#95a5a6', mac: tm.mac, ip: tm.ip, online: tm.online},
  1064. position: {x: x, y: y}, selectable: false});
  1065. els.push({group: 'edges', data: {id: 'te_' + tm.mac, type: 'term', source: sw, target: 't_' + tm.mac}});
  1066. });
  1067. });
  1068. }
  1069. function renderOverview(snap) {
  1070. var O = computeOverview(snap), els = [], idx = {};
  1071. var nb = el('l2-net-tabs'); if (nb) { nb.style.display = 'none'; nb.innerHTML = ''; }
  1072. if (O.root) {
  1073. els.push({group: 'nodes', data: {id: '__root', type: 'root', label: CFG.root_label || 'COBCO', w: O.root.w, h: O.root.h},
  1074. position: {x: O.root.cx, y: O.root.cy}, selectable: false, grabbable: false});
  1075. }
  1076. O.tiles.forEach(function (tile, i) {
  1077. var s = O.stats[tile.label] || {nodes: [], alarms: 0, offline: 0, maxsev: -1};
  1078. idx[tile.label] = 'grp_' + i;
  1079. var isCore = /core/i.test(tile.label) || /data\s*center/i.test(tile.label);
  1080. var bd = s.alarms ? '#E45959' : (dark ? '#41597c' : '#b9c8dc');
  1081. var bg = isCore ? TIER_BG.core : (dark ? '#22344e' : '#eef3fb');
  1082. var fg = isCore ? '#ffffff' : (dark ? '#dfe9fb' : '#1e3352');
  1083. var lbl = tile.label + '\n' + s.nodes.length + ' ' + t('l2.unit_sw', 'sw') +
  1084. (s.up ? ' ↑ ' + s.up : '') + (s.alarms ? ' ⚠ ' + s.alarms : '') + (s.offline ? ' ◌ ' + s.offline : '');
  1085. var cls = [];
  1086. if (s.alarms) cls.push('alarm');
  1087. if (isCore) cls.push('core');
  1088. els.push({group: 'nodes', data: {
  1089. id: idx[tile.label], type: 'tile', group: tile.label,
  1090. n: s.nodes.length, alarms: s.alarms, offline: s.offline, maxsev: s.maxsev, up: s.up,
  1091. label: lbl, bg: bg, fg: fg, bd: bd, w: TILE_W, h: TILE_H
  1092. }, position: {x: tile.x + tile.w / 2, y: tile.y + tile.h / 2}, classes: cls.join(' ')});
  1093. });
  1094. applyElements(els);
  1095. }
  1096. function renderNetTabs(D) {
  1097. var box = el('l2-net-tabs');
  1098. if (!box) return;
  1099. box.innerHTML = '';
  1100. if (!D.nets || D.nets.length < 2) { box.style.display = 'none'; return; }
  1101. box.style.display = 'flex';
  1102. D.nets.forEach(function (n) {
  1103. var b = document.createElement('button');
  1104. b.type = 'button';
  1105. b.className = 'l2-net-tab' + (n.key === D.activeNet ? ' active' : '');
  1106. b.textContent = networkLabel(n.key) + ' · ' + n.count + ' ' + t('l2.unit_sw', 'sw');
  1107. b.addEventListener('click', function () { detailNet = n.key; render(lastSnap); });
  1108. box.appendChild(b);
  1109. });
  1110. }
  1111. function renderDetail(snap) {
  1112. var D = computeDetail(snap, viewGroup), els = [];
  1113. renderNetTabs(D);
  1114. function frameEl(id, type, fr) {
  1115. els.push({group: 'nodes', data: {id: id, type: type, label: fr.label, w: fr.w, h: fr.h},
  1116. position: {x: fr.x + fr.w / 2, y: fr.y + fr.h / 2}, selectable: false, grabbable: false});
  1117. }
  1118. frameEl('__ws', 'wsframe', {x: D.frames.workshop.x, y: 36, w: D.frames.workshop.w, h: D.frames.workshop.h + 36, label: D.frames.workshop.label});
  1119. if (D.frames.dist) frameEl('__dist', 'floorframe', D.frames.dist);
  1120. D.floors.forEach(function (f, i) { frameEl('__floor_' + i, 'floorframe', f.frame); });
  1121. (D.ghosts || []).forEach(function (g) {
  1122. var nm = (g.node && g.node.name) || g.id;
  1123. var ip = (g.node && g.node.ip) || '';
  1124. var tail = String(nm).split(/[-\s]+/).filter(Boolean).slice(-1)[0] || '';
  1125. var short = (tail ? tail + ' · ' : '') + ip;
  1126. els.push({group: 'nodes', data: {id: g.id, type: 'ghost', label: short}, position: {x: g.x, y: g.y}, selectable: false, grabbable: false});
  1127. els.push({group: 'edges', data: {
  1128. id: 'ghe_' + g.id, type: 'uplink', source: g.id, target: g.to,
  1129. speed: g.speed, state: g.state, lbl: speedText(g.speed),
  1130. color: speedColor(g.speed, g.state), ls: (g.state && g.state !== 'up') ? 'dashed' : 'solid',
  1131. w: Math.max(2.6, speedWidth(g.speed))
  1132. }});
  1133. });
  1134. var switchIds = {};
  1135. D.nodes.forEach(function (n, ni) {
  1136. switchIds[n.id] = true;
  1137. var p = D.pos[n.id] || {x: 0, y: 0};
  1138. var tier = n.tier || 'access';
  1139. var tkey = (tier.indexOf('dist') === 0) ? 'dist' : (tier.indexOf('core') === 0 ? 'core' : 'access');
  1140. var isRoot = D.dists && D.dists.length && n.id === D.dists[0].id;
  1141. var cls = [];
  1142. var bd = (dark ? TIER_BG_DARK[tkey] : TIER_BG[tkey]) || '#555';
  1143. var bg = (dark ? TIER_BG_DARK[tkey] : TIER_BG[tkey]) || '#777';
  1144. if (!isRoot && ni % 2 === 1) bg = dark ? '#4f8a3a' : '#6ca75a';
  1145. if (n.alarms && n.alarms.length) {
  1146. cls.push('alarm');
  1147. bd = '#E45959';
  1148. }
  1149. if (n.latency_ms != null) {
  1150. if (+n.latency_ms > LATB) cls.push('lat-bad');
  1151. else if (+n.latency_ms > LATW) cls.push('lat-warn');
  1152. }
  1153. if (isRoot) cls.push('root');
  1154. els.push({group: 'nodes', data: {
  1155. id: n.id, type: 'switch', name: n.name, ip: n.ip, tier: tier, depth: n.depth,
  1156. hostid: n.hostid, oper: n.oper, maxsev: n.maxsev, alarms: n.alarms || [],
  1157. term_online: n.term_online, term_offline: n.term_offline, latency_ms: n.latency_ms,
  1158. label: isRoot ? rootLabel(n) : rowLabel(n), tw: isRoot ? 150 : 360,
  1159. bg: bg, bd: isRoot ? bd : '#25501e',
  1160. w: isRoot ? 170 : 380, h: isRoot ? 50 : 24, group: viewGroup
  1161. }, position: p, classes: cls.join(' ')});
  1162. });
  1163. D.internal.forEach(function (e, i) {
  1164. var root = D.dists && D.dists[0];
  1165. if (root && (e.source === root.id || e.target === root.id)) {
  1166. var childId = e.source === root.id ? e.target : e.source;
  1167. var pp = D.pos[root.id], cp = D.pos[childId];
  1168. if (pp && cp) {
  1169. var sp = 'sp_' + i, tp = 'tp_' + i;
  1170. // Exact left-edge midpoints, straight line. The parent label is
  1171. // allowed to cover the line where it exits the parent.
  1172. els.push({group: 'nodes', data: {id: sp, type: 'edgepoint'}, position: {x: pp.x - 86, y: pp.y}, selectable: false, grabbable: false});
  1173. els.push({group: 'nodes', data: {id: tp, type: 'edgepoint'}, position: {x: cp.x - 191, y: cp.y}, selectable: false, grabbable: false});
  1174. els.push({group: 'edges', data: {
  1175. id: 'e_' + i, type: 'detail', source: sp, target: tp, p: root.id, c: childId,
  1176. speed: e.speed, state: e.state, a_port: e.a_port, b_port: e.b_port,
  1177. color: speedColor(e.speed, e.state), ls: (e.state && e.state !== 'up') ? 'dashed' : 'solid',
  1178. w: Math.min(2.4, speedWidth(e.speed))
  1179. }, classes: 'detail'});
  1180. return;
  1181. }
  1182. }
  1183. els.push({group: 'edges', data: {
  1184. id: 'e_' + i, source: e.source, target: e.target, speed: e.speed, state: e.state,
  1185. a_port: e.a_port, b_port: e.b_port,
  1186. color: speedColor(e.speed, e.state), ls: (e.state && e.state !== 'up') ? 'dashed' : 'solid',
  1187. w: Math.min(2.4, speedWidth(e.speed))
  1188. }, classes: 'detail'});
  1189. });
  1190. if (showTerm) {
  1191. var bySw = {};
  1192. (snap.terminals || []).forEach(function (tm) {
  1193. if (!switchIds[tm.sw_ip]) return;
  1194. if (onlyOff && tm.online) return;
  1195. (bySw[tm.sw_ip] = bySw[tm.sw_ip] || []).push(tm);
  1196. });
  1197. Object.keys(bySw).forEach(function (sw) {
  1198. var base = D.pos[sw]; if (!base) return;
  1199. bySw[sw].sort(function (a, b) { return (a.online === b.online) ? 0 : (a.online ? -1 : 1); });
  1200. bySw[sw].forEach(function (tm, k) {
  1201. var col = k % 9, row = Math.floor(k / 9);
  1202. var x = base.x - 54 + col * 13;
  1203. var y = base.y + NODE_H / 2 + 16 + row * 13;
  1204. els.push({group: 'nodes', data: {id: 't_' + tm.mac, type: 'term', bg: tm.online ? '#2ECC71' : '#95a5a6', mac: tm.mac, ip: tm.ip, online: tm.online},
  1205. position: {x: x, y: y}, selectable: false});
  1206. els.push({group: 'edges', data: {id: 'te_' + tm.mac, type: 'term', source: sw, target: 't_' + tm.mac}});
  1207. });
  1208. });
  1209. }
  1210. pathParent = D.parentOf || {};
  1211. lockedPath = null;
  1212. applyElements(els);
  1213. }
  1214. /* ---------------- side panels & stats ---------------- */
  1215. function updateStats(snap) {
  1216. var s = snap.stats || {};
  1217. var txt = '● ' + (s.switches || 0) + ' | ⇄ ' + (s.edges || 0) + ' | ' +
  1218. (s.term_total || 0) + '(' + (s.term_online || 0) + '/' + (s.term_offline || 0) + ') | ⚠ ' + (s.alarms || 0);
  1219. var up = snap.generated_iso ? (' · ' + t('l2.updated', 'Updated') + ' ' + snap.generated_iso) : '';
  1220. el('l2-stats').textContent = txt + up;
  1221. }
  1222. function buildLegend() {
  1223. var h = '';
  1224. function sw(c, label, dashed) { return '<div class="lg-row"><span class="lg-line' + (dashed ? ' lg-dash' : '') + '" style="border-top-color:' + c + '"></span>' + label + '</div>'; }
  1225. h += '<div class="lg-title">' + t('l2.legend_speed', 'Link speed') + '</div>';
  1226. h += sw(colors['100'], '100M') + sw(colors['1000'], '1G') + sw(colors['10000'], '10G') + sw(colors['20000'], '20G+');
  1227. h += sw(colors.unknown, t('lbl.color_unknown', 'Unknown'));
  1228. h += sw(colors.down, t('lbl.color_down', 'Down'), true);
  1229. h += '<div class="lg-title">' + t('l2.legend_tier', 'Tier') + '</div>';
  1230. h += '<div class="lg-row"><span class="lg-box" style="background:' + TIER_BG.core + '"></span>' + t('l2.core', 'Core') +
  1231. '<span class="lg-box" style="background:#2E75B6;margin-left:8px"></span>' + t('l2.main', 'Main switch') +
  1232. '<span class="lg-box" style="background:' + TIER_BG.dist + ';margin-left:8px"></span>' + t('l2.dist', 'Distribution') +
  1233. '<span class="lg-box" style="background:' + TIER_BG.access + ';margin-left:8px"></span>' + t('l2.access', 'Access') + '</div>';
  1234. h += '<div class="lg-title">' + t('l2.legend_lat', 'Latency') + '</div>';
  1235. h += '<div class="lg-row"><span class="lg-box" style="background:#F39C12;border-radius:2px"></span>&gt;' + LATW + 'ms' +
  1236. '<span class="lg-box" style="background:#E74C3C;border-radius:2px;margin-left:8px"></span>&gt;' + LATB + 'ms</div>';
  1237. h += '<div class="lg-row"><span class="lg-box" style="background:#E45959;border-radius:2px"></span>' + t('l2.alarms', 'Alarms') + '</div>';
  1238. h += '<div class="lg-row"><span class="lg-box" style="background:#2ECC71;border-radius:50%"></span>' + t('l2.term_on', 'Online') +
  1239. '<span class="lg-box" style="background:#95a5a6;border-radius:50%;margin-left:8px"></span>' + t('l2.term_off', 'Offline') + '</div>';
  1240. h += '<div class="lg-row" style="opacity:.72;margin-top:4px">' + t('l2.click_hint', 'Hover a switch to highlight its full upstream path') + '</div>';
  1241. el('l2-legend').innerHTML = h;
  1242. }
  1243. function buildAlarms(snap) {
  1244. var items = [];
  1245. (snap.nodes || []).forEach(function (n) {
  1246. if (n.maxsev != null && n.maxsev >= 0 && n.alarms && n.alarms.length) {
  1247. n.alarms.forEach(function (a) {
  1248. items.push({sev: n.maxsev, title: (a.name || a.description || ''), host: n.name, ip: n.ip, id: n.id, hostid: n.hostid});
  1249. });
  1250. }
  1251. });
  1252. el('l2-alarm-count').textContent = items.length;
  1253. if (!items.length) { el('l2-alarm-list').innerHTML = '<div class="panel-empty">' + t('l2.alarm_empty', 'No active alarms.') + '</div>'; return; }
  1254. items.sort(function (a, b) { return b.sev - a.sev; });
  1255. var h = '';
  1256. items.forEach(function (it) {
  1257. var col = SEV_COLORS[Math.max(0, Math.min(5, it.sev))] || '#999';
  1258. h += '<div class="alarm-row" data-id="' + (it.id || '') + '" data-hostid="' + (it.hostid || '') + '">' +
  1259. '<span class="alarm-dot" style="background:' + col + '"></span>' +
  1260. '<div class="alarm-main"><div class="alarm-title">' + esc(it.title) + '</div>' +
  1261. '<div class="alarm-sub">' + esc(it.host || '') + (it.ip ? ' · ' + esc(it.ip) : '') + '</div></div></div>';
  1262. });
  1263. var box = el('l2-alarm-list');
  1264. box.innerHTML = h;
  1265. box.querySelectorAll('.alarm-row').forEach(function (r) {
  1266. r.addEventListener('click', function () {
  1267. var id = r.getAttribute('data-id');
  1268. if (id) revealById(id);
  1269. else { var hid = r.getAttribute('data-hostid'); if (hid) window.open(META.zabbixHostUrl.replace('__HOSTID__', hid), '_blank'); }
  1270. });
  1271. });
  1272. }
  1273. function buildOffline(snap) {
  1274. var off = (snap.terminals || []).filter(function (tm) { return !tm.online; });
  1275. el('l2-offline-count').textContent = off.length;
  1276. if (!off.length) { el('l2-offline-list').innerHTML = '<div class="panel-empty">' + t('l2.offline_empty', 'No offline terminals.') + '</div>'; return; }
  1277. var h = '';
  1278. off.forEach(function (tm) {
  1279. h += '<div class="off-row" data-mac="' + esc(tm.mac) + '">' +
  1280. '<div class="off-main"><div class="off-mac">' + esc(tm.mac) + '</div>' +
  1281. '<div class="off-sub">' + esc(tm.sw_name || tm.sw_ip) + ' · ' + esc(tm.port || '') + '</div></div></div>';
  1282. });
  1283. var box = el('l2-offline-list');
  1284. box.innerHTML = h;
  1285. box.querySelectorAll('.off-row').forEach(function (r) {
  1286. r.addEventListener('click', function () {
  1287. var mac = r.getAttribute('data-mac');
  1288. revealById('t_' + mac);
  1289. });
  1290. });
  1291. }
  1292. function esc(s) { return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) { return ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[c]; }); }
  1293. /* ---------------- interactions ---------------- */
  1294. function focusNode(id) {
  1295. if (!cy) return;
  1296. var n = cy.getElementById(id);
  1297. if (!n || n.empty() || n.length === 0) return;
  1298. cy.elements().removeClass('hl');
  1299. n.addClass('hl');
  1300. cy.animate({center: {eles: n}, zoom: Math.max(cy.zoom(), 1.1), duration: 380});
  1301. setTimeout(function () { n.removeClass('hl'); }, 2600);
  1302. }
  1303. function groupOfNodeId(id) {
  1304. var hit = (lastSnap && lastSnap.nodes || []).filter(function (n) { return n.id === id; })[0];
  1305. return hit ? groupOf(hit).label : null;
  1306. }
  1307. // Make sure a switch/terminal is visible (auto-opens its workshop from overview).
  1308. function revealById(id) {
  1309. if (!lastSnap) return;
  1310. id = String(id);
  1311. if (id.indexOf('t_') === 0) {
  1312. var tm = (lastSnap.terminals || []).filter(function (x) { return 't_' + x.mac === id; })[0];
  1313. if (!tm) return;
  1314. showTerm = true;
  1315. if (el('l2-showterm')) el('l2-showterm').checked = true;
  1316. if (!cy || cy.getElementById(id).empty()) render(lastSnap);
  1317. setTimeout(function () { focusNode(id); }, 90);
  1318. } else {
  1319. focusNode(id);
  1320. }
  1321. }
  1322. function clearPath() {
  1323. if (!cy) return;
  1324. cy.elements().removeClass('dim pathhl');
  1325. lockedPath = null;
  1326. }
  1327. function highlightPath(nodeId) {
  1328. if (!cy) return;
  1329. var chain = [], id = nodeId, guard = 0;
  1330. while (id && guard++ < 40) {
  1331. chain.unshift(id);
  1332. id = pathParent[id] || null;
  1333. }
  1334. cy.elements('node[type="switch"], node[type="ghost"], edge').addClass('dim').removeClass('pathhl');
  1335. var nodes = cy.collection();
  1336. chain.forEach(function (cid) { var n = cy.getElementById(cid); if (n && n.length) nodes = nodes.union(n); });
  1337. nodes.removeClass('dim').addClass('pathhl');
  1338. var edges = cy.edges().filter(function (e) {
  1339. var p = e.data('p'), c = e.data('c');
  1340. if (p && c) return chain.indexOf(p) >= 0 && chain.indexOf(c) >= 0;
  1341. var s = e.data('source'), t = e.data('target');
  1342. return chain.indexOf(s) >= 0 && chain.indexOf(t) >= 0;
  1343. });
  1344. edges.removeClass('dim').addClass('pathhl');
  1345. }
  1346. function bindCy() {
  1347. cy.on('mouseover', 'node[type="switch"]', function (e) {
  1348. highlightPath(e.target.id());
  1349. var d = e.target.data();
  1350. var arr = d.alarms || [];
  1351. var sev = (d.maxsev != null && d.maxsev >= 0) ? ('<span style="color:' + SEV_COLORS[d.maxsev] + '">● ' + SEV_NAMES[d.maxsev] + '</span>') : '—';
  1352. var lat = (d.latency_ms != null) ? (d.latency_ms + ' ms') : '—';
  1353. var html = '<b>' + esc(d.name) + '</b><br>' + esc(d.ip) + ' · ' + esc(d.group || '') + '<br>' +
  1354. 'Tier: ' + esc(d.tier) + ' · ' + (d.oper || '') + '<br>' +
  1355. 'Latency: ' + lat + '<br>' +
  1356. 'Terminals: ' + (d.term_online || 0) + ' on / ' + (d.term_offline || 0) + ' off<br>' +
  1357. 'Alarm: ' + sev;
  1358. if (arr.length) html += '<hr>' + arr.map(function (a) { return esc(a.name || a.description || ''); }).join('<br>');
  1359. showTip(e, html);
  1360. });
  1361. cy.on('mouseout', 'node[type="switch"]', function () {
  1362. if (!lockedPath) clearPath();
  1363. hideTip();
  1364. });
  1365. cy.on('mouseover', 'edge:not([type="term"])', function (e) {
  1366. var d = e.target.data();
  1367. var sp = (parseFloat(d.speed) || 0) >= 1000 ? ((parseFloat(d.speed) / 1000) + 'G') : (d.speed + 'M');
  1368. showTip(e, 'Link ' + esc(d.a_port || '') + ' ↔ ' + esc(d.b_port || '') + '<br>' + sp + ' · ' + esc(d.state || ''));
  1369. });
  1370. cy.on('mouseout', 'edge', hideTip);
  1371. cy.on('tap', 'node[type="switch"]', function (e) {
  1372. lockedPath = e.target.id();
  1373. highlightPath(lockedPath);
  1374. });
  1375. cy.on('tap', 'node[type="wsframe"]', function (e) {
  1376. cy.fit(e.target, 60);
  1377. });
  1378. cy.on('tap', function (e) {
  1379. if (e.target === cy) clearPath();
  1380. });
  1381. cy.on('dbltap', 'node[type="switch"]', function (e) {
  1382. var hid = e.target.data('hostid');
  1383. if (hid) window.open(META.zabbixHostUrl.replace('__HOSTID__', hid), '_blank');
  1384. });
  1385. }
  1386. var SEV_NAMES = ['未分类','信息','警告','一般严重','严重','灾难'];
  1387. function showTip(e, html) {
  1388. var tip = el('l2-tip');
  1389. tip.innerHTML = html;
  1390. tip.style.display = 'block';
  1391. var p = e.renderedPosition || {x: 0, y: 0};
  1392. var box = el('l2-cy').getBoundingClientRect();
  1393. var x = box.left + p.x + 16, y = box.top + p.y + 16;
  1394. tip.style.left = x + 'px'; tip.style.top = y + 'px';
  1395. }
  1396. function hideTip() { el('l2-tip').style.display = 'none'; }
  1397. /* ---------------- search ---------------- */
  1398. function buildIndex(snap) {
  1399. var idx = [];
  1400. (snap.nodes || []).forEach(function (n) { idx.push({id: n.id, label: n.name, sub: n.ip, kind: 'sw'}); });
  1401. (snap.terminals || []).forEach(function (tm) { idx.push({id: 't_' + tm.mac, label: tm.ip || tm.mac, sub: tm.mac + ' · ' + (tm.sw_name || ''), kind: 'term', mac: tm.mac}); });
  1402. return idx;
  1403. }
  1404. var INDEX = [];
  1405. function doSearch(q) {
  1406. var box = el('l2-sug');
  1407. q = (q || '').trim().toLowerCase();
  1408. if (!q) { box.style.display = 'none'; box.innerHTML = ''; return; }
  1409. var hits = [];
  1410. for (var i = 0; i < INDEX.length && hits.length < 14; i++) {
  1411. var it = INDEX[i];
  1412. if ((it.label && it.label.toLowerCase().indexOf(q) >= 0) || (it.sub && it.sub.toLowerCase().indexOf(q) >= 0)) hits.push(it);
  1413. }
  1414. if (!hits.length) { box.innerHTML = '<div class="sug-empty">—</div>'; box.style.display = 'block'; return; }
  1415. box.innerHTML = hits.map(function (h) {
  1416. return '<div class="sug-row" data-id="' + esc(h.id) + '"><span class="sug-kind ' + h.kind + '">' + (h.kind === 'sw' ? 'SW' : 'PC') + '</span>' +
  1417. '<span class="sug-lbl">' + esc(h.label) + '</span><span class="sug-sub">' + esc(h.sub || '') + '</span></div>';
  1418. }).join('');
  1419. box.style.display = 'block';
  1420. box.querySelectorAll('.sug-row').forEach(function (r) {
  1421. r.addEventListener('click', function () {
  1422. var id = r.getAttribute('data-id');
  1423. revealById(id);
  1424. box.style.display = 'none';
  1425. });
  1426. });
  1427. }
  1428. /* ---------------- toolbar wiring ---------------- */
  1429. function isZabbixDark() {
  1430. var html = document.documentElement;
  1431. var cs = String(html.getAttribute('color-scheme') || '').toLowerCase();
  1432. if (cs === 'dark') return true;
  1433. if (cs === 'light') return false;
  1434. var body = document.body;
  1435. var theme = [
  1436. html.getAttribute('theme'), html.getAttribute('data-theme'), html.className,
  1437. body ? body.getAttribute('theme') : '', body ? body.getAttribute('data-theme') : '',
  1438. body ? body.className : ''
  1439. ].join(' ');
  1440. return /dark/i.test(theme);
  1441. }
  1442. function applyTheme() {
  1443. var nd = isZabbixDark();
  1444. var changed = nd !== dark;
  1445. dark = nd;
  1446. var wrap = el('l2-wrap');
  1447. if (wrap) wrap.classList.toggle('l2-dark', dark);
  1448. if (changed && cy) cy.style(styleFor(dark));
  1449. }
  1450. function fitViewport() {
  1451. var wrap = el('l2-wrap');
  1452. if (!wrap) return;
  1453. var top = Math.max(0, wrap.getBoundingClientRect().top);
  1454. var h = Math.max(520, window.innerHeight - top - 8);
  1455. wrap.style.height = h + 'px';
  1456. if (cy) cy.resize();
  1457. }
  1458. function setupControls() {
  1459. applyTheme();
  1460. if (window.MutationObserver) {
  1461. var mo = new MutationObserver(applyTheme);
  1462. mo.observe(document.documentElement, {attributes: true, attributeFilter: ['theme', 'data-theme', 'color-scheme', 'class']});
  1463. if (document.body) {
  1464. mo.observe(document.body, {attributes: true, attributeFilter: ['theme', 'data-theme', 'color-scheme', 'class']});
  1465. }
  1466. }
  1467. var back = el('l2-back');
  1468. if (back) back.style.display = 'none';
  1469. el('l2-showterm').addEventListener('change', function () { showTerm = el('l2-showterm').checked; render(lastSnap); });
  1470. el('l2-onlyoff').addEventListener('change', function () {
  1471. onlyOff = el('l2-onlyoff').checked;
  1472. if (onlyOff && !showTerm) { showTerm = true; el('l2-showterm').checked = true; }
  1473. render(lastSnap);
  1474. });
  1475. el('l2-zin').addEventListener('click', function () { if (cy) cy.zoom({level: cy.zoom() * 1.25, renderedPosition: {x: cy.width() / 2, y: cy.height() / 2}}); });
  1476. el('l2-zout').addEventListener('click', function () { if (cy) cy.zoom({level: cy.zoom() / 1.25, renderedPosition: {x: cy.width() / 2, y: cy.height() / 2}}); });
  1477. el('l2-fit').addEventListener('click', function () { if (cy) cy.fit(cy.elements(), 50); });
  1478. el('l2-refresh-btn').addEventListener('click', function () { reload(true); });
  1479. window.addEventListener('resize', fitViewport);
  1480. var s = el('l2-search');
  1481. s.addEventListener('input', function () { doSearch(s.value); });
  1482. document.addEventListener('click', function (ev) {
  1483. if (!ev.target.closest('.l2-search-wrap')) el('l2-sug').style.display = 'none';
  1484. });
  1485. el('l2-ts').addEventListener('change', function () {
  1486. var v = el('l2-ts').value;
  1487. var u = '/zabbix.php?action=l2.topology.view';
  1488. if (v) u += '&ts=' + encodeURIComponent(v);
  1489. window.location = u;
  1490. });
  1491. el('l2-pause').addEventListener('click', function () {
  1492. paused = !paused;
  1493. el('l2-pause').classList.toggle('on', paused);
  1494. el('l2-pause').textContent = paused ? '▶' : '❚❚';
  1495. el('l2-pause').title = paused ? t('l2.resume', 'Resume') : t('l2.pause', 'Pause');
  1496. });
  1497. }
  1498. function reload(manual) {
  1499. if (!META.isLive) return;
  1500. fetch(META.snapshotUrl + '?t=' + Date.now(), {cache: 'no-store'})
  1501. .then(function (r) { return r.json(); })
  1502. .then(function (j) { if (j && j.nodes) { SNAP = j; INDEX = buildIndex(j); render(j); } })
  1503. .catch(function () {})
  1504. .then(function () { if (manual) resetCountdown(); });
  1505. }
  1506. function resetCountdown() { countdown = META.refreshSeconds || 300; }
  1507. function startTimer() {
  1508. if (!META.isLive) return;
  1509. resetCountdown();
  1510. timer = setInterval(function () {
  1511. if (paused) { el('l2-countdown').textContent = '⏸'; return; }
  1512. countdown--;
  1513. if (countdown <= 0) { countdown = META.refreshSeconds || 300; reload(false); }
  1514. var m = Math.floor(countdown / 60), s = countdown % 60;
  1515. el('l2-countdown').textContent = '↻ ' + m + ':' + (s < 10 ? '0' + s : s);
  1516. }, 1000);
  1517. }
  1518. /* ---------------- boot ---------------- */
  1519. function boot() {
  1520. setupControls();
  1521. fitViewport();
  1522. try {
  1523. var qs = new URLSearchParams(window.location.search);
  1524. var qg = qs.get('g');
  1525. if (qg) focusGroupLabel = qg;
  1526. } catch (e) {}
  1527. if (!SNAP || !SNAP.nodes) {
  1528. el('l2-cy').innerHTML = '<div class="l2-empty">' + t('l2.no_data', 'No data') + '</div>';
  1529. } else {
  1530. INDEX = buildIndex(SNAP);
  1531. render(SNAP);
  1532. }
  1533. setTimeout(fitViewport, 60);
  1534. if (META.isLive) startTimer();
  1535. else { el('l2-pause').disabled = true; el('l2-countdown').textContent = t('l2.snapshot', 'Snapshot'); }
  1536. }
  1537. if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot);
  1538. else boot();
  1539. })();