1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
| const maxDays = 30;
async function genReportLog(container, key, siteData) { const { url, records } = siteData; let statusLines = records .map((entry) => `${entry.dateTime}, ${entry.result}`) .join("\n");
const normalized = normalizeData(statusLines); const statusStream = constructStatusStream(key, url, normalized); container.appendChild(statusStream); }
function constructStatusStream(key, url, uptimeData) { let streamContainer = templatize("statusStreamContainerTemplate"); for (let ii = maxDays - 1; ii >= 0; ii--) { let line = constructStatusLine(key, ii, uptimeData[ii]); streamContainer.appendChild(line); }
let totalSuccess = 0, totalFailure = 0; for (let i = 0; i < maxDays; i++) { if (uptimeData[i]) { totalSuccess += uptimeData[i].success; totalFailure += uptimeData[i].failure; } } const lastSet = uptimeData[0]; const color = getColor(lastSet); const container = templatize("statusContainerTemplate", { title: key, url: url, color: color, status: getStatusText(color), upTime: uptimeData.upTime, success: totalSuccess, failure: totalFailure });
const placeholder = container.querySelector("#statusStreamPlaceholder"); placeholder.appendChild(streamContainer); return container; }
function constructStatusLine(key, relDay, upTimeData) { let date = new Date(); date.setDate(date.getDate() - relDay); return constructStatusSquare(key, date, upTimeData); }
function getColor(uptimeVal) { let avg = uptimeVal == null ? null : (typeof uptimeVal === 'object' ? uptimeVal.avg : uptimeVal); return avg == null ? "nodata" : avg == 1 ? "success" : avg < 0.3 ? "failure" : "partial"; }
function constructStatusSquare(key, date, uptimeVal) { const color = getColor(uptimeVal); let square = templatize("statusSquareTemplate", { color: color, tooltip: getTooltip(key, date, color, uptimeVal) });
const show = () => { showTooltip(square, key, date, color, uptimeVal); }; square.addEventListener("mouseover", show); square.addEventListener("mousedown", show); square.addEventListener("mouseout", hideTooltip); return square; }
let cloneId = 0; function templatize(templateId, parameters) { let clone = document.getElementById(templateId).cloneNode(true); clone.id = "template_clone_" + cloneId++; if (!parameters) { return clone; } applyTemplateSubstitutions(clone, parameters); return clone; }
function applyTemplateSubstitutions(node, parameters) { const attributes = node.getAttributeNames(); for (let ii = 0; ii < attributes.length; ii++) { const attr = attributes[ii]; const attrVal = node.getAttribute(attr); node.setAttribute(attr, templatizeString(attrVal, parameters)); }
if (node.childElementCount === 0) { node.innerText = templatizeString(node.innerText, parameters); } else { const children = Array.from(node.children); children.forEach((n) => { applyTemplateSubstitutions(n, parameters); }); } }
function templatizeString(text, parameters) { if (parameters) { for (const [key, val] of Object.entries(parameters)) { text = text.replaceAll("$" + key, val); } } return text; }
function getStatusText(color) { return color === "nodata" ? "暂无数据" : color === "success" ? "运行正常" : color === "failure" ? "完全中断" : color === "partial" ? "部分中断" : "未知状态"; }
function getStatusDescriptiveText(color) { return color === "nodata" ? "暂无数据:未执行健康检查。" : color === "success" ? "当天未记录到任何停机。" : color === "failure" ? "当天记录到严重故障。" : color === "partial" ? "当天记录到部分服务中断。" : "未知状态"; }
function getTooltip(key, date, color, uptimeVal) { let statusText = getStatusText(color); let countsText = ""; if (uptimeVal && typeof uptimeVal === "object") { countsText = ` 成功: ${uptimeVal.success}, 失败: ${uptimeVal.failure}`; } return `${key} | ${date.toDateString()} : ${statusText}${countsText}`; }
function showTooltip(element, key, date, color, uptimeVal) { clearTimeout(tooltipTimeout); const toolTipDiv = document.getElementById("tooltip");
document.getElementById("tooltipDateTime").innerText = date.toDateString(); let description = getStatusDescriptiveText(color); if (uptimeVal && typeof uptimeVal === "object") { description += ` 成功: ${uptimeVal.success}, 失败: ${uptimeVal.failure}`; } document.getElementById("tooltipDescription").innerText = description;
const statusDiv = document.getElementById("tooltipStatus"); statusDiv.innerText = getStatusText(color); statusDiv.className = color;
toolTipDiv.style.top = element.offsetTop + element.offsetHeight + 10 + "px"; toolTipDiv.style.left = element.offsetLeft + element.offsetWidth / 2 - toolTipDiv.offsetWidth / 2 + "px"; toolTipDiv.style.opacity = "1"; }
function hideTooltip() { tooltipTimeout = setTimeout(() => { const toolTipDiv = document.getElementById("tooltip"); toolTipDiv.style.opacity = "0"; }, 1000); }
function normalizeData(statusLines) { const rows = statusLines.split("\n"); const dateNormalized = splitRowsByDate(rows); let relativeDateMap = {}; const now = Date.now(); for (const [key, val] of Object.entries(dateNormalized)) { if (key === "upTime") continue; const relDays = getRelativeDays(now, new Date(key).getTime()); relativeDateMap[relDays] = { avg: val.total ? (val.success / val.total) : null, success: val.success, failure: val.total - val.success }; } relativeDateMap.upTime = dateNormalized.upTime; return relativeDateMap; }
function splitRowsByDate(rows) { let dateValues = {}; let totalSuccess = 0, totalCount = 0; for (let ii = 0; ii < rows.length; ii++) { const row = rows[ii]; if (!row) continue; const [dateTimeStr, resultStr] = row.split(",", 2); const dateTime = new Date(Date.parse(dateTimeStr.replace(/-/g, "/") + " GMT")); const dateStr = dateTime.toDateString();
if (!dateValues[dateStr]) { dateValues[dateStr] = { total: 0, success: 0 }; }
let isSuccess = resultStr.trim() === "success" ? 1 : 0; dateValues[dateStr].total++; dateValues[dateStr].success += isSuccess; totalCount++; totalSuccess += isSuccess; }
const upTime = totalCount ? ((totalSuccess / totalCount) * 100).toFixed(2) + "%" : "--%"; dateValues.upTime = upTime; return dateValues; }
function getRelativeDays(date1, date2) { return Math.floor(Math.abs((date1 - date2) / (24 * 3600 * 1000))); }
let tooltipTimeout = null;
document.addEventListener("DOMContentLoaded", initSiteStatus); document.addEventListener("pjax:complete", initSiteStatus);
async function initSiteStatus() { try { const responseLog = await fetch(siteStatusJson); let allData = {}; if (responseLog.ok) { allData = await responseLog.json(); }
const reportsDiv = document.getElementById("reports"); reportsDiv.innerHTML = ""; for (const [siteName, siteData] of Object.entries(allData)) { await genReportLog(reportsDiv, siteName, siteData); } } catch (error) { console.error("Failed to initialize site status:", error); } }
|