overview-monitoring-demo.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <!doctype html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>驾驶舱总体概览:运行监测 Demo</title>
  7. <style>
  8. :root { color-scheme: dark; --panel:#0d1c30; --line:#234969; --cyan:#4dd5ff; --green:#49d17c; --yellow:#ffd166; --red:#ff5f6d; --muted:#8ba7bd; }
  9. * { box-sizing:border-box; }
  10. body { margin:0; font-family:"Microsoft YaHei",Arial,sans-serif; background:#071322; color:#eaf5ff; }
  11. header { padding:18px 24px; border-bottom:1px solid var(--line); display:flex; align-items:center; justify-content:space-between; }
  12. h1 { font-size:22px; margin:0; letter-spacing:2px; }
  13. main { padding:20px; display:grid; grid-template-columns:minmax(310px,430px) 1fr; gap:18px; }
  14. .panel { background:var(--panel); border:1px solid var(--line); border-radius:8px; overflow:hidden; }
  15. .panel > h2 { margin:0; padding:12px 14px; font-size:15px; border-bottom:1px solid var(--line); }
  16. .body { padding:14px; }
  17. .kpis { display:grid; grid-template-columns:repeat(3,1fr); gap:10px; }
  18. .kpi { background:#08182b; border:1px solid var(--line); border-radius:6px; padding:12px; }
  19. .kpi b { display:block; font-size:24px; color:var(--cyan); }
  20. .kpi span { color:var(--muted); font-size:12px; }
  21. .filter { display:flex; gap:8px; margin:14px 0; }
  22. .filter-label { align-self:center; color:var(--muted); font-size:12px; }
  23. button { cursor:pointer; background:#102c44; color:#eaf5ff; border:1px solid var(--line); padding:7px 12px; border-radius:4px; }
  24. button.active { background:#1a5f83; border-color:var(--cyan); }
  25. .status { display:grid; grid-template-columns:repeat(4,1fr); gap:10px; margin-bottom:12px; }
  26. .status div { text-align:center; padding:8px; border-radius:4px; background:#08182b; border:1px solid var(--line); }
  27. .online b { color:var(--green); } .offline b { color:var(--red); } .stale b { color:var(--yellow); }
  28. ul { padding-left:18px; margin:0; color:#d3e9f8; }
  29. li { margin:5px 0; }
  30. .map { position:relative; min-height:470px; background:#020b16; }
  31. .map-grid { position:absolute; inset:0; background-image:linear-gradient(#12324c 1px,transparent 1px),linear-gradient(90deg,#12324c 1px,transparent 1px); background-size:50px 50px; opacity:.45; }
  32. .point { position:absolute; width:26px; height:26px; margin:-13px; border-radius:50%; display:flex; align-items:center; justify-content:center; border:2px solid transparent; cursor:pointer; }
  33. .point.ONLINE { background:#0e4b29; border-color:var(--green); }
  34. .point.OFFLINE { background:#57232a; border-color:var(--red); }
  35. .point.STALE { background:#55431a; border-color:var(--yellow); }
  36. .point.UNKNOWN { background:#283642; border-color:#7e95a8; }
  37. .point small { font-size:10px; font-weight:700; }
  38. .legend, .events { padding:10px 14px; border-top:1px solid var(--line); font-size:12px; color:var(--muted); }
  39. .events button { margin-left:8px; }
  40. .toast { position:absolute; right:14px; bottom:14px; padding:9px 13px; border-radius:4px; background:#12324c; border:1px solid var(--cyan); min-width:220px; }
  41. .hidden { display:none; }
  42. code { color:var(--cyan); }
  43. </style>
  44. </head>
  45. <body>
  46. <header>
  47. <h1>总体概览 / 运行监测</h1>
  48. <div><span id="restState">REST:连接中</span> · <span id="updated">更新时间:--</span></div>
  49. </header>
  50. <main>
  51. <section class="panel">
  52. <h2>监管能力 KPI</h2>
  53. <div class="body">
  54. <div class="kpis">
  55. <div class="kpi"><b id="deviceCount">--</b><span>监测设备</span></div>
  56. <div class="kpi"><b id="pointCount">--</b><span>监测点</span></div>
  57. <div class="kpi"><b id="facilityCount">--</b><span>监测设施</span></div>
  58. <div class="kpi"><b id="onlineRate">--</b><span>设备在线率</span></div>
  59. <div class="kpi"><b id="staleCount">--</b><span>STALE 设备</span></div>
  60. <div class="kpi"><b id="freshness">--</b><span>汇总新鲜度</span></div>
  61. </div>
  62. <div class="filter" id="industries">
  63. <span class="filter-label">行业:</span>
  64. <button data-industry="" class="active">全部</button>
  65. <button data-industry="GAS">燃气</button>
  66. <button data-industry="WATER_SUPPLY">供水</button>
  67. <button data-industry="DRAINAGE">排水</button>
  68. </div>
  69. <div class="filter" id="metrics">
  70. <span class="filter-label">指标:</span>
  71. <button data-metric="" class="active">全部</button>
  72. <button data-metric="ONLINE">在线</button>
  73. <button data-metric="OFFLINE">离线</button>
  74. <button data-metric="STALE">过期</button>
  75. <button data-metric="UNKNOWN">未知</button>
  76. </div>
  77. <div class="status">
  78. <div class="online"><b id="online">--</b><span>在线</span></div>
  79. <div class="offline"><b id="offline">--</b><span>离线</span></div>
  80. <div class="stale"><b id="stale">--</b><span>过期</span></div>
  81. <div><b id="unknown">--</b><span>未知</span></div>
  82. </div>
  83. <h3 style="font-size:14px;margin:14px 0 8px;">类型分布</h3>
  84. <ul id="distribution"></ul>
  85. </div>
  86. </section>
  87. <section class="panel">
  88. <h2>中央一张图联动</h2>
  89. <div class="map" id="map">
  90. <div class="map-grid"></div>
  91. </div>
  92. <div class="legend">模拟点位:绿=ONLINE,红=OFFLINE,黄=STALE,灰=UNKNOWN;行业或指标筛选会过滤地图点位。</div>
  93. <div class="events">
  94. <span>模拟 WebSocket 设备增量:</span>
  95. <button onclick="pushDelta('GAS-002','OFFLINE','2026-09-02T10:05:00+08:00')">GAS-002 离线</button>
  96. <button onclick="pushDelta('GAS-002','ONLINE','2026-09-02T10:06:00+08:00')">GAS-002 在线</button>
  97. <button onclick="pushDelta('WS-001','ONLINE','2026-09-02T10:07:00+08:00')">WS-001 恢复</button>
  98. <button onclick="pushOldDelta()">发送旧消息</button>
  99. </div>
  100. <div class="toast hidden" id="toast"></div>
  101. </section>
  102. </main>
  103. <script>
  104. const devices = [
  105. { code:'GAS-001', industry:'GAS', type:'GAS_SENSOR', state:'ONLINE', x:20, y:25 },
  106. { code:'GAS-002', industry:'GAS', type:'GAS_SENSOR', state:'OFFLINE', x:33, y:41 },
  107. { code:'WS-001', industry:'WATER_SUPPLY', type:'WATER_SUPPLY_SENSOR', state:'STALE', x:52, y:28 },
  108. { code:'WS-002', industry:'WATER_SUPPLY', type:'WATER_SUPPLY_SENSOR', state:'UNKNOWN', x:63, y:47 },
  109. { code:'RAIN-001', industry:'DRAINAGE', type:'RAINWATER_SENSOR', state:'ONLINE', x:76, y:31 },
  110. { code:'SEW-001', industry:'DRAINAGE', type:'SEWAGE_ENVIRONMENT_SENSOR', state:'ONLINE', x:85, y:56 }
  111. ];
  112. const baseCounts = { monitoringPointCount:6, monitoringFacilityCount:9, infrastructureFacilityCount:99 };
  113. let selectedIndustry = '';
  114. let selectedMetric = '';
  115. let latestServerUpdatedAt = '2026-09-02T09:59:50+08:00';
  116. function filteredDevices() {
  117. return devices.filter(device =>
  118. (!selectedIndustry || device.industry === selectedIndustry)
  119. && (!selectedMetric || device.state === selectedMetric));
  120. }
  121. function fmtRate(numerator, denominator) {
  122. return denominator === 0 ? '0.0000' : (numerator / denominator).toFixed(4);
  123. }
  124. function render() {
  125. const visible = filteredDevices();
  126. const online = visible.filter(d => d.state === 'ONLINE').length;
  127. const offline = visible.filter(d => d.state === 'OFFLINE').length;
  128. const stale = visible.filter(d => d.state === 'STALE').length;
  129. const unknown = visible.filter(d => d.state === 'UNKNOWN').length;
  130. const denominator = online + offline + stale;
  131. const freshness = visible.length === 0 ? 'EMPTY' : stale === 0 ? 'CURRENT' : 'STALE';
  132. document.getElementById('deviceCount').textContent = visible.length;
  133. document.getElementById('pointCount').textContent = selectedIndustry ? Math.round(baseCounts.monitoringPointCount / 2) : baseCounts.monitoringPointCount;
  134. document.getElementById('facilityCount').textContent = selectedIndustry ? 3 : baseCounts.monitoringFacilityCount;
  135. document.getElementById('onlineRate').textContent = fmtRate(online, denominator);
  136. document.getElementById('staleCount').textContent = stale;
  137. document.getElementById('freshness').textContent = freshness;
  138. document.getElementById('online').textContent = online;
  139. document.getElementById('offline').textContent = offline;
  140. document.getElementById('stale').textContent = stale;
  141. document.getElementById('unknown').textContent = unknown;
  142. document.getElementById('updated').textContent = '更新时间:' + latestServerUpdatedAt;
  143. const counts = new Map();
  144. visible.forEach(d => counts.set(d.type, (counts.get(d.type) || 0) + 1));
  145. document.getElementById('distribution').innerHTML = [...counts.entries()]
  146. .map(([type, count]) => `<li><code>${type}</code>:${count} 台设备</li>`).join('');
  147. const map = document.getElementById('map');
  148. map.querySelectorAll('.point').forEach(node => node.remove());
  149. visible.forEach(d => {
  150. const point = document.createElement('div');
  151. point.className = 'point ' + d.state;
  152. point.style.left = d.x + '%';
  153. point.style.top = d.y + '%';
  154. point.title = `${d.code}\n${d.industry}\n${d.type}\n${d.state}`;
  155. point.innerHTML = '<small>' + d.code.slice(0, 3) + '</small>';
  156. point.onclick = () => show(d.code + ' / ' + d.state);
  157. map.appendChild(point);
  158. });
  159. }
  160. function show(message) {
  161. const toast = document.getElementById('toast');
  162. toast.textContent = message;
  163. toast.classList.remove('hidden');
  164. setTimeout(() => toast.classList.add('hidden'), 2200);
  165. }
  166. function pushDelta(deviceCode, state, serverUpdatedAt) {
  167. if (serverUpdatedAt <= latestServerUpdatedAt) {
  168. show('旧消息已忽略:' + serverUpdatedAt);
  169. return;
  170. }
  171. const device = devices.find(d => d.code === deviceCode);
  172. if (!device) return;
  173. device.state = state;
  174. latestServerUpdatedAt = serverUpdatedAt;
  175. render();
  176. show('已应用增量:' + deviceCode + ' → ' + state);
  177. }
  178. function pushOldDelta() {
  179. pushDelta('GAS-002', 'ONLINE', '2026-09-02T09:00:00+08:00');
  180. }
  181. function bindFilter(containerId, apply) {
  182. document.getElementById(containerId).addEventListener('click', event => {
  183. const button = event.target.closest('button');
  184. if (!button) return;
  185. document.querySelectorAll('#' + containerId + ' button').forEach(node => node.classList.remove('active'));
  186. button.classList.add('active');
  187. apply(button.dataset);
  188. render();
  189. });
  190. }
  191. bindFilter('industries', dataset => { selectedIndustry = dataset.industry; });
  192. bindFilter('metrics', dataset => { selectedMetric = dataset.metric; });
  193. async function loadRestInitialSummary() {
  194. try {
  195. const response = await fetch('/api/cockpit/v1/overview/monitoring', {
  196. headers: { 'Accept': 'application/json' }
  197. });
  198. if (!response.ok) throw new Error('HTTP ' + response.status);
  199. const payload = await response.json();
  200. if (payload && payload.data && payload.data.updatedAt) {
  201. latestServerUpdatedAt = payload.data.updatedAt;
  202. document.getElementById('restState').textContent = 'REST:已加载初始汇总';
  203. } else {
  204. throw new Error('INVALID_ENVELOPE');
  205. }
  206. } catch (error) {
  207. document.getElementById('restState').textContent = 'REST:接口不可用,使用演示 fixture';
  208. }
  209. render();
  210. }
  211. loadRestInitialSummary();
  212. render();
  213. </script>
  214. </body>
  215. </html>