diff --git a/main.go b/main.go index 602ce8c..0b7da5f 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ type Task struct { LastCheck time.Time `json:"last_check"` Status string `json:"status"` Notified bool `json:"notified"` + History []HistoryItem `json:"history"` } type Config struct { @@ -36,6 +37,11 @@ type Config struct { NotifyEnabled bool `json:"notify_enabled"` } +type HistoryItem struct { + State string `json:"state"` + Time time.Time `json:"time"` +} + var ( tasks = make(map[string]*Task) config = &Config{Interval: 60, Timeout: 20, NotifyEnabled: true} @@ -248,46 +254,24 @@ func handleIndex(w http.ResponseWriter, r *http.Request) { const token = localStorage.getItem('token') || prompt('请输入访问令牌:'); if(token) localStorage.setItem('token', token); const headers = {'Authorization': 'Bearer ' + token}; - let historyData = {}; - const MAX_BARS = 90; - function initHistory(id) { - if(!historyData[id]) { - historyData[id] = Array(MAX_BARS).fill({state: 'unknown', time: null}); - } - } - - function updateHistory(id, state, time) { - initHistory(id); - historyData[id].shift(); - historyData[id].push({state, time}); - } function loadTasks() { fetch('/api/tasks', {headers}) .then(r => r.json()) .then(tasks => { - tasks.forEach(t => { - initHistory(t.id); - const state = t.status === 'ok' ? (t.in_stock ? 'stock' : 'no-stock') : (t.status === 'error' ? 'error' : 'unknown'); - const lastState = historyData[t.id][MAX_BARS - 1]; - if(!lastState.time || lastState.time !== t.last_check) { - updateHistory(t.id, state, t.last_check || Date.now()); - } - }); - const tbody = document.querySelector('#taskTable tbody'); tbody.innerHTML = tasks.map(t => { const statusClass = t.status || 'checking'; const stockText = t.in_stock ? '有货' : '无货'; const lastCheck = t.last_check ? new Date(t.last_check).toLocaleString('zh-CN') : '-'; - const history = historyData[t.id] || []; + + const history = t.history || []; const uptimeBar = history.map(h => { - const time = h.time ? new Date(h.time).toLocaleString('zh-CN') : '等待检测'; + const time = new Date(h.time).toLocaleString('zh-CN'); const label = h.state === 'stock' ? '有货' : h.state === 'no-stock' ? '无货' : h.state === 'error' ? '错误' : '未知'; return '